]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_led.c
Corrected LED name match finding avoiding extraneous Usage printouts
[people/ms/u-boot.git] / common / cmd_led.c
1 /*
2 * (C) Copyright 2010
3 * Jason Kridner <jkridner@beagleboard.org>
4 *
5 * Based on cmd_led.c patch from:
6 * http://www.mail-archive.com/u-boot@lists.denx.de/msg06873.html
7 * (C) Copyright 2008
8 * Ulf Samuelsson <ulf.samuelsson@atmel.com>
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29 #include <common.h>
30 #include <config.h>
31 #include <command.h>
32 #include <status_led.h>
33
34 struct led_tbl_s {
35 char *string; /* String for use in the command */
36 led_id_t mask; /* Mask used for calling __led_set() */
37 void (*on)(void); /* Optional fucntion for turning LED on */
38 void (*off)(void); /* Optional fucntion for turning LED on */
39 };
40
41 typedef struct led_tbl_s led_tbl_t;
42
43 static const led_tbl_t led_commands[] = {
44 #ifdef CONFIG_BOARD_SPECIFIC_LED
45 #ifdef STATUS_LED_BIT
46 { "0", STATUS_LED_BIT, NULL, NULL },
47 #endif
48 #ifdef STATUS_LED_BIT1
49 { "1", STATUS_LED_BIT1, NULL, NULL },
50 #endif
51 #ifdef STATUS_LED_BIT2
52 { "2", STATUS_LED_BIT2, NULL, NULL },
53 #endif
54 #ifdef STATUS_LED_BIT3
55 { "3", STATUS_LED_BIT3, NULL, NULL },
56 #endif
57 #endif
58 #ifdef STATUS_LED_GREEN
59 { "green", STATUS_LED_GREEN, green_LED_off, green_LED_on },
60 #endif
61 #ifdef STATUS_LED_YELLOW
62 { "yellow", STATUS_LED_YELLOW, yellow_LED_off, yellow_LED_on },
63 #endif
64 #ifdef STATUS_LED_RED
65 { "red", STATUS_LED_RED, red_LED_off, red_LED_on },
66 #endif
67 #ifdef STATUS_LED_BLUE
68 { "blue", STATUS_LED_BLUE, blue_LED_off, blue_LED_on },
69 #endif
70 { NULL, 0, NULL, NULL }
71 };
72
73 int str_onoff (char *var)
74 {
75 if (strcmp(var, "off") == 0) {
76 return 0;
77 }
78 if (strcmp(var, "on") == 0) {
79 return 1;
80 }
81 return -1;
82 }
83
84 int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
85 {
86 int state, i, match = 0;
87
88 /* Validate arguments */
89 if ((argc != 3)) {
90 return cmd_usage(cmdtp);
91 }
92
93 state = str_onoff(argv[2]);
94 if (state < 0) {
95 return cmd_usage(cmdtp);
96 }
97
98 for (i = 0; led_commands[i].string; i++) {
99 if ((strcmp("all", argv[1]) == 0) ||
100 (strcmp(led_commands[i].string, argv[1]) == 0)) {
101 match = 1;
102 if (led_commands[i].on) {
103 if (state) {
104 led_commands[i].on();
105 } else {
106 led_commands[i].off();
107 }
108 } else {
109 __led_set(led_commands[i].mask, state);
110 }
111 break;
112 }
113 }
114
115 /* If we ran out of matches, print Usage */
116 if (!match) {
117 return cmd_usage(cmdtp);
118 }
119
120 return 0;
121 }
122
123 U_BOOT_CMD(
124 led, 3, 1, do_led,
125 "led\t- ["
126 #ifdef CONFIG_BOARD_SPECIFIC_LED
127 #ifdef STATUS_LED_BIT
128 "0|"
129 #endif
130 #ifdef STATUS_LED_BIT1
131 "1|"
132 #endif
133 #ifdef STATUS_LED_BIT2
134 "2|"
135 #endif
136 #ifdef STATUS_LED_BIT3
137 "3|"
138 #endif
139 #endif
140 #ifdef STATUS_LED_GREEN
141 "green|"
142 #endif
143 #ifdef STATUS_LED_YELLOW
144 "yellow|"
145 #endif
146 #ifdef STATUS_LED_RED
147 "red|"
148 #endif
149 #ifdef STATUS_LED_BLUE
150 "blue|"
151 #endif
152 "all] [on|off]\n",
153 "led [led_name] [on|off] sets or clears led(s)\n"
154 );