From 94b80ba13567379ef2fc4e7efd7ac3d87515f242 Mon Sep 17 00:00:00 2001 From: Tzafrir Cohen Date: Wed, 23 May 2012 12:36:42 +0000 Subject: sysfs: refactor lineconfig string representation Signed-off-by: Shaun Ruffell Acked-by: Tzafrir Cohen git-svn-id: http://svn.asterisk.org/svn/dahdi/linux/trunk@10685 a0bf4364-ded3-4de4-8d8a-66a801d63aff --- drivers/dahdi/dahdi-base.c | 62 ++++++++++++++++++++++++++++++++------------- drivers/dahdi/dahdi-sysfs.c | 20 +-------------- include/dahdi/kernel.h | 1 + 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/drivers/dahdi/dahdi-base.c b/drivers/dahdi/dahdi-base.c index 367b951..85dafd0 100644 --- a/drivers/dahdi/dahdi-base.c +++ b/drivers/dahdi/dahdi-base.c @@ -774,6 +774,48 @@ const char *dahdi_lineconfig_bit_name(int lineconfig_bit) } EXPORT_SYMBOL(dahdi_lineconfig_bit_name); +ssize_t lineconfig_str(int lineconfig, char buf[], size_t size) +{ + int framing_bit = 0; + int coding_bit = 0; + int crc4_bit = 0; + int len = 0; + int bit; + + for (bit = 4; bit <= 12; bit++) { + int mask = (1 << bit); + if (!(lineconfig & mask)) + continue; + if (!framing_bit) { + switch (mask) { + case DAHDI_CONFIG_B8ZS: + case DAHDI_CONFIG_AMI: + case DAHDI_CONFIG_HDB3: + framing_bit = bit; + len += snprintf(buf + len, size, "%s/", + dahdi_lineconfig_bit_name(bit)); + } + } + if (!coding_bit) { + switch (mask) { + case DAHDI_CONFIG_ESF: + case DAHDI_CONFIG_D4: + case DAHDI_CONFIG_CCS: + coding_bit = bit; + len += snprintf(buf + len, size, "%s", + dahdi_lineconfig_bit_name(bit)); + } + } + if (!crc4_bit && mask == DAHDI_CONFIG_CRC4) { + crc4_bit = bit; + len += snprintf(buf + len, size, "/%s", + dahdi_lineconfig_bit_name(bit)); + } + } + return len; +} +EXPORT_SYMBOL(lineconfig_str); + #ifdef CONFIG_PROC_FS static const char *sigstr(int sig) { @@ -873,23 +915,9 @@ static int dahdi_seq_show(struct seq_file *sfile, void *v) seq_printf(sfile, " (MASTER)"); if (s->lineconfig) { - /* framing first */ - if (s->lineconfig & DAHDI_CONFIG_B8ZS) - seq_printf(sfile, " B8ZS/"); - else if (s->lineconfig & DAHDI_CONFIG_AMI) - seq_printf(sfile, " AMI/"); - else if (s->lineconfig & DAHDI_CONFIG_HDB3) - seq_printf(sfile, " HDB3/"); - /* then coding */ - if (s->lineconfig & DAHDI_CONFIG_ESF) - seq_printf(sfile, "ESF"); - else if (s->lineconfig & DAHDI_CONFIG_D4) - seq_printf(sfile, "D4"); - else if (s->lineconfig & DAHDI_CONFIG_CCS) - seq_printf(sfile, "CCS"); - /* E1's can enable CRC checking */ - if (s->lineconfig & DAHDI_CONFIG_CRC4) - seq_printf(sfile, "/CRC4"); + char tmpbuf[20]; + lineconfig_str(s->lineconfig, tmpbuf, sizeof(tmpbuf)); + seq_printf(sfile, " %s", tmpbuf); } seq_printf(sfile, " "); diff --git a/drivers/dahdi/dahdi-sysfs.c b/drivers/dahdi/dahdi-sysfs.c index d7bf608..1715f8f 100644 --- a/drivers/dahdi/dahdi-sysfs.c +++ b/drivers/dahdi/dahdi-sysfs.c @@ -179,25 +179,7 @@ static BUS_ATTR_READER(lineconfig_show, dev, buf) int len = 0; span = dev_to_span(dev); - if (span->lineconfig) { - /* framing first */ - if (span->lineconfig & DAHDI_CONFIG_B8ZS) - len += sprintf(buf + len, "B8ZS/"); - else if (span->lineconfig & DAHDI_CONFIG_AMI) - len += sprintf(buf + len, "AMI/"); - else if (span->lineconfig & DAHDI_CONFIG_HDB3) - len += sprintf(buf + len, "HDB3/"); - /* then coding */ - if (span->lineconfig & DAHDI_CONFIG_ESF) - len += sprintf(buf + len, "ESF"); - else if (span->lineconfig & DAHDI_CONFIG_D4) - len += sprintf(buf + len, "D4"); - else if (span->lineconfig & DAHDI_CONFIG_CCS) - len += sprintf(buf + len, "CCS"); - /* E1's can enable CRC checking */ - if (span->lineconfig & DAHDI_CONFIG_CRC4) - len += sprintf(buf + len, "/CRC4"); - } + len += lineconfig_str(span->lineconfig, buf, 20); len += sprintf(buf + len, "\n"); return len; } diff --git a/include/dahdi/kernel.h b/include/dahdi/kernel.h index 08c1c41..c452270 100644 --- a/include/dahdi/kernel.h +++ b/include/dahdi/kernel.h @@ -807,6 +807,7 @@ enum spantypes { const char *dahdi_spantype2str(enum spantypes st); enum spantypes dahdi_str2spantype(const char *name); const char *dahdi_lineconfig_bit_name(int lineconfig_bit); +ssize_t lineconfig_str(int lineconfig, char buf[], size_t size); struct file; -- cgit v1.2.3