Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

USB: cxacru: Cleanup sysfs attribute code

This changes the format of unknown status values to be less verbose and
uses an array instead of several different snprintf calls. Since only
enum values are assigned to it, poll_state is changed from int to enum.
Use abs() for dB values instead of two almost identical return lines.

Signed-off-by: Simon Arlott <simon@fire.lp0.eu>
Acked-by: Duncan Sands <duncan.sands@math.u-psud.fr>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Simon Arlott and committed by
Greg Kroah-Hartman
87e71b47 6dbd682b

+31 -42
+31 -42
drivers/usb/atm/cxacru.c
··· 171 171 struct delayed_work poll_work; 172 172 u32 card_info[CXINF_MAX]; 173 173 struct mutex poll_state_serialize; 174 - int poll_state; 174 + enum cxacru_poll_state poll_state; 175 175 176 176 /* contol handles */ 177 177 struct mutex cm_serialize; ··· 226 226 227 227 static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf) 228 228 { 229 - if (unlikely(value < 0)) { 230 - return snprintf(buf, PAGE_SIZE, "%d.%02u\n", 231 - value / 100, -value % 100); 232 - } else { 233 - return snprintf(buf, PAGE_SIZE, "%d.%02u\n", 234 - value / 100, value % 100); 235 - } 229 + return snprintf(buf, PAGE_SIZE, "%d.%02u\n", 230 + value / 100, abs(value) % 100); 236 231 } 237 232 238 233 static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf) 239 234 { 240 - switch (value) { 241 - case 0: return snprintf(buf, PAGE_SIZE, "no\n"); 242 - case 1: return snprintf(buf, PAGE_SIZE, "yes\n"); 243 - default: return 0; 244 - } 235 + static char *str[] = { "no", "yes" }; 236 + if (unlikely(value >= ARRAY_SIZE(str))) 237 + return snprintf(buf, PAGE_SIZE, "%u\n", value); 238 + return snprintf(buf, PAGE_SIZE, "%s\n", str[value]); 245 239 } 246 240 247 241 static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf) 248 242 { 249 - switch (value) { 250 - case 1: return snprintf(buf, PAGE_SIZE, "not connected\n"); 251 - case 2: return snprintf(buf, PAGE_SIZE, "connected\n"); 252 - case 3: return snprintf(buf, PAGE_SIZE, "lost\n"); 253 - default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value); 254 - } 243 + static char *str[] = { NULL, "not connected", "connected", "lost" }; 244 + if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL)) 245 + return snprintf(buf, PAGE_SIZE, "%u\n", value); 246 + return snprintf(buf, PAGE_SIZE, "%s\n", str[value]); 255 247 } 256 248 257 249 static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf) 258 250 { 259 - switch (value) { 260 - case 0: return snprintf(buf, PAGE_SIZE, "down\n"); 261 - case 1: return snprintf(buf, PAGE_SIZE, "attempting to activate\n"); 262 - case 2: return snprintf(buf, PAGE_SIZE, "training\n"); 263 - case 3: return snprintf(buf, PAGE_SIZE, "channel analysis\n"); 264 - case 4: return snprintf(buf, PAGE_SIZE, "exchange\n"); 265 - case 5: return snprintf(buf, PAGE_SIZE, "up\n"); 266 - case 6: return snprintf(buf, PAGE_SIZE, "waiting\n"); 267 - case 7: return snprintf(buf, PAGE_SIZE, "initialising\n"); 268 - default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value); 269 - } 251 + static char *str[] = { "down", "attempting to activate", 252 + "training", "channel analysis", "exchange", "up", 253 + "waiting", "initialising" 254 + }; 255 + if (unlikely(value >= ARRAY_SIZE(str))) 256 + return snprintf(buf, PAGE_SIZE, "%u\n", value); 257 + return snprintf(buf, PAGE_SIZE, "%s\n", str[value]); 270 258 } 271 259 272 260 static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf) 273 261 { 274 - switch (value) { 275 - case 0: return 0; 276 - case 1: return snprintf(buf, PAGE_SIZE, "ANSI T1.413\n"); 277 - case 2: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.1 (G.DMT)\n"); 278 - case 3: return snprintf(buf, PAGE_SIZE, "ITU-T G.992.2 (G.LITE)\n"); 279 - default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value); 280 - } 262 + static char *str[] = { 263 + NULL, 264 + "ANSI T1.413", 265 + "ITU-T G.992.1 (G.DMT)", 266 + "ITU-T G.992.2 (G.LITE)" 267 + }; 268 + if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL)) 269 + return snprintf(buf, PAGE_SIZE, "%u\n", value); 270 + return snprintf(buf, PAGE_SIZE, "%s\n", str[value]); 281 271 } 282 272 283 273 /* ··· 298 308 struct cxacru_data *instance = usbatm_instance->driver_data; 299 309 u32 value = instance->card_info[CXINF_LINE_STARTABLE]; 300 310 301 - switch (value) { 302 - case 0: return snprintf(buf, PAGE_SIZE, "running\n"); 303 - case 1: return snprintf(buf, PAGE_SIZE, "stopped\n"); 304 - default: return snprintf(buf, PAGE_SIZE, "unknown (%u)\n", value); 305 - } 311 + static char *str[] = { "running", "stopped" }; 312 + if (unlikely(value >= ARRAY_SIZE(str))) 313 + return snprintf(buf, PAGE_SIZE, "%u\n", value); 314 + return snprintf(buf, PAGE_SIZE, "%s\n", str[value]); 306 315 } 307 316 308 317 static ssize_t cxacru_sysfs_store_adsl_state(struct device *dev,