cmd_led.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 (*on)(void); /* Optional fucntion for turning LED on */
  36. void (*off)(void); /* Optional fucntion 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;
  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. if (led_commands[i].on) {
  93. if (state) {
  94. led_commands[i].on();
  95. } else {
  96. led_commands[i].off();
  97. }
  98. } else {
  99. __led_set(led_commands[i].mask, state);
  100. }
  101. break;
  102. }
  103. }
  104. /* If we ran out of matches, print Usage */
  105. if (!led_commands[i].string && !(strcmp("all", argv[1]) == 0)) {
  106. return cmd_usage(cmdtp);
  107. }
  108. return 0;
  109. }
  110. U_BOOT_CMD(
  111. led, 3, 1, do_led,
  112. "led\t- ["
  113. #ifdef CONFIG_BOARD_SPECIFIC_LED
  114. #ifdef STATUS_LED_BIT
  115. "0|"
  116. #endif
  117. #ifdef STATUS_LED_BIT1
  118. "1|"
  119. #endif
  120. #ifdef STATUS_LED_BIT2
  121. "2|"
  122. #endif
  123. #ifdef STATUS_LED_BIT3
  124. "3|"
  125. #endif
  126. #endif
  127. #ifdef STATUS_LED_GREEN
  128. "green|"
  129. #endif
  130. #ifdef STATUS_LED_YELLOW
  131. "yellow|"
  132. #endif
  133. #ifdef STATUS_LED_RED
  134. "red|"
  135. #endif
  136. #ifdef STATUS_LED_BLUE
  137. "blue|"
  138. #endif
  139. "all] [on|off]\n",
  140. "led [led_name] [on|off] sets or clears led(s)\n"
  141. );