14 static int verbose = 0;
16 off=0, on=1, blink, unknown, transition=unknown
23 if ((leds = open("/dev/leds", O_RDWR)) < 0) {
27 fprintf(stderr,"Error: Could not open LEDS device file '/dev/leds' : %s\n",
30 fprintf(stderr,"Run as root\n");
36 printf("leds: initialized.\n");
39 void led_ioctl( int cmd, int num )
43 if (ioctl(leds, cmd, num) < 0) {
45 fprintf(stderr, "leds: ioctl(%d,%d): failed to set leds: %s\n",
46 cmd, num, strerror(e1));
51 void led_set( int led, int state )
54 case off: if (!reset) led_ioctl(N2_LM_OFF, led); break;
55 case on: led_ioctl(N2_LM_ON, led); break;
56 case blink: /* Ensure any previous timer gets deleted first and that
57 * the LED is in a well known state.
59 if (!reset) led_ioctl(N2_LM_OFF, led);
60 led_ioctl(N2_LM_BLINK, led); break;
66 case 'r': return LED_RS_RED;
67 case 'g': return LED_RS_GRN;
68 case '1': return LED_DISK1;
69 case '2': return LED_DISK2;
70 case 'A': reset = 1; return LED_ALL;
71 default: fprintf(stderr, "leds: %c: unknown LED (use r,g,0,1 or A)\n", ch);
76 int main( int argc, char **argv )
78 /* Default: switch green on, red off (-A +g). */
82 led_ioctl(N2_LM_ALL_OFF, 0);
83 led_ioctl(N2_LM_ON, LED_RS_GRN);
85 int i, alt=0, state[PHYS_LEDS];
86 for(i=0; i<PHYS_LEDS; ++i)
93 if (strcmp(arg, "-v") == 0) {
99 case '+': st = on; break;
100 case '-': st = off; break;
101 case '!': st = blink; break;
102 case '/': st = transition; break;
103 default: fprintf(stderr, "leds: %c: unknown option\n", *arg);
107 if (st != transition) {
111 for (i=0; i<PHYS_LEDS; ++i) state[i] = st;
116 int done, newstate[PHYS_LEDS];
117 for(i=0; i<PHYS_LEDS; ++i)
122 for (i=0; i<PHYS_LEDS; ++i) newstate[i] = on;
127 /* Merge the newstate back in. This sets 'alt' if going
128 * from an old state of just red to a new of just green
129 * or vice versa (and this is the only way of getting
132 /* Blink anything which changes from off to on or from
133 * on to off (this ignores anything already blinking).
135 for (done=i=0; i<PHYS_LEDS; ++i) {
136 if (state[i] == !newstate[i]) {
142 /* Is anything (new) blinking? If it is then deal
143 * with the red/green case - blinking red,green is
144 * amber, is that what we want? This could be
145 * improved by a better kernel interface - it would
146 * be nice just to specify on/off times and a start
150 if (state[LED_RS_RED] == blink && state[LED_RS_GRN] == blink &&
151 newstate[LED_RS_RED] == !newstate[LED_RS_GRN]) {
152 /* Kernel bug: must switch off r and g first. */
156 for (i=0; i<PHYS_LEDS; ++i) {
157 if (newstate[i] == on) {
165 /* Go through the list making the required settings. 'alt' is
166 * special. 'reset' means A was given and all the settings are
171 led_ioctl(N2_LM_ALL_OFF, 0);
173 /* Turn the leds off first to get to a known state. */
174 led_set(LED_RS_GRN, off);
175 led_set(LED_RS_RED, off);
176 led_ioctl(N2_LM_ALT, LED_RS_RED);
178 /* KERNEL BUG: setting the green timer zaps the red behaviour
179 * to toggle the green, therefore if red blink is set before
180 * green blink no blink will happen!
182 led_set(LED_RS_GRN, state[LED_RS_GRN]);
183 led_set(LED_RS_RED, state[LED_RS_RED]);
185 led_set(LED_DISK1, state[LED_DISK1]);
186 led_set(LED_DISK2, state[LED_DISK2]);