cmd_led.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <common.h>
  29. #include <config.h>
  30. #include <command.h>
  31. #include <status_led.h>
  32. struct led_tbl_s {
  33. char *string; /* String for use in the command */
  34. led_id_t mask; /* Mask used for calling __led_set() */
  35. void (*off)(void); /* Optional function for turning LED off */
  36. void (*on)(void); /* Optional function for turning LED on */
  37. };
  38. typedef struct led_tbl_s led_tbl_t;
  39. static const led_tbl_t led_commands[] = {
  40. #ifdef CONFIG_BOARD_SPECIFIC_LED
  41. #ifdef STATUS_LED_BIT
  42. { "0", STATUS_LED_BIT, NULL, NULL },
  43. #endif
  44. #ifdef STATUS_LED_BIT1
  45. { "1", STATUS_LED_BIT1, NULL, NULL },
  46. #endif
  47. #ifdef STATUS_LED_BIT2
  48. { "2", STATUS_LED_BIT2, NULL, NULL },
  49. #endif
  50. #ifdef STATUS_LED_BIT3
  51. { "3", STATUS_LED_BIT3, NULL, NULL },
  52. #endif
  53. #endif
  54. #ifdef STATUS_LED_GREEN
  55. { "green", STATUS_LED_GREEN, green_LED_off, green_LED_on },
  56. #endif
  57. #ifdef STATUS_LED_YELLOW
  58. { "yellow", STATUS_LED_YELLOW, yellow_LED_off, yellow_LED_on },
  59. #endif
  60. #ifdef STATUS_LED_RED
  61. { "red", STATUS_LED_RED, red_LED_off, red_LED_on },
  62. #endif
  63. #ifdef STATUS_LED_BLUE
  64. { "blue", STATUS_LED_BLUE, blue_LED_off, blue_LED_on },
  65. #endif
  66. { NULL, 0, NULL, NULL }
  67. };
  68. int str_onoff (char *var)
  69. {
  70. if (strcmp(var, "off") == 0) {
  71. return 0;
  72. }
  73. if (strcmp(var, "on") == 0) {
  74. return 1;
  75. }
  76. return -1;
  77. }
  78. int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  79. {
  80. int state, i, match = 0;
  81. /* Validate arguments */
  82. if ((argc != 3)) {
  83. return cmd_usage(cmdtp);
  84. }
  85. state = str_onoff(argv[2]);
  86. if (state < 0) {
  87. return cmd_usage(cmdtp);
  88. }
  89. for (i = 0; led_commands[i].string; i++) {
  90. if ((strcmp("all", argv[1]) == 0) ||
  91. (strcmp(led_commands[i].string, argv[1]) == 0)) {
  92. match = 1;
  93. if (led_commands[i].on) {
  94. if (state) {
  95. led_commands[i].on();
  96. } else {
  97. led_commands[i].off();
  98. }
  99. } else {
  100. __led_set(led_commands[i].mask, state);
  101. }
  102. break;
  103. }
  104. }
  105. /* If we ran out of matches, print Usage */
  106. if (!match) {
  107. return cmd_usage(cmdtp);
  108. }
  109. return 0;
  110. }
  111. U_BOOT_CMD(
  112. led, 3, 1, do_led,
  113. "led\t- ["
  114. #ifdef CONFIG_BOARD_SPECIFIC_LED
  115. #ifdef STATUS_LED_BIT
  116. "0|"
  117. #endif
  118. #ifdef STATUS_LED_BIT1
  119. "1|"
  120. #endif
  121. #ifdef STATUS_LED_BIT2
  122. "2|"
  123. #endif
  124. #ifdef STATUS_LED_BIT3
  125. "3|"
  126. #endif
  127. #endif
  128. #ifdef STATUS_LED_GREEN
  129. "green|"
  130. #endif
  131. #ifdef STATUS_LED_YELLOW
  132. "yellow|"
  133. #endif
  134. #ifdef STATUS_LED_RED
  135. "red|"
  136. #endif
  137. #ifdef STATUS_LED_BLUE
  138. "blue|"
  139. #endif
  140. "all] [on|off]\n",
  141. "led [led_name] [on|off] sets or clears led(s)\n"
  142. );