LEDs on NSA325v2
January 27, 2021 05:52AM
Hello,

I am running Debian with 5.2.9 kirkwood kernel on my device which I installed from this awesome website. I was wondering if there is any way to interact with the LEDs and speaker on the device (e.g. I want my sata1/sata2 leds turn red and play a beep sound when there is a disk failure -> mdadm detects a degraded array). I found some files in /sys/class/leds but tbh I dont really know how to continue from there. Can I just set the value in the brightness file to max (255) ?

For the beeping,I tried the "beep" package but it does not produce any sound.

I dont want to break anything so I figured it is better to ask here first.
Re: LEDs on NSA325v2
January 27, 2021 06:11PM
Dyce,

> For the beeping,I tried the "beep" package but it
> does not produce any sound.

I don't think the buzzer works on this box.

> I was wondering if there is any way to
> interact with the LEDs and speaker on the device
> (e.g. I want my sata1/sata2 leds turn red and play
> a beep sound when there is a disk failure -> mdadm
> detects a degraded array).

> I found some files in
> /sys/class/leds but tbh I dont really know how to
> continue from there. Can I just set the value in
> the brightness file to max (255) ?
>

Look in your rootfs /etc/rc.local for example how to manipulate the GPIO.

In the 3 examples below, the first echo sets the Sys LED to green. The last 2 set the HDD1 and HDD2 to a trigger that will flash the LED whenever there is disk access.

if [ -d /sys/class/leds/nsa325:green:sys ]; then
   echo default-on  > /sys/class/leds/nsa325:green:sys/trigger 
   echo none        > /sys/class/leds/nsa325:orange:sys/trigger
fi

if [ -d /sys/class/leds/nsa325:green:sata1 ]; then
   echo ide-disk1  > /sys/class/leds/nsa325:green:sata1/trigger 
fi

if [ -d /sys/class/leds/nsa325:green:sata2 ]; then
   echo ide-disk2  > /sys/class/leds/nsa325:green:sata2/trigger 
fi

And for this box:

Quote
ls -l /sys/class/leds/
total 0
drwxr-xr-x 2 root root 0 Jan 27 15:58 .
drwxr-xr-x 49 root root 0 Jan 27 15:56 ..
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:green:copy -> ../../devices/platform/gpio-leds/leds/nsa325:green:copy
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:green:sata1 -> ../../devices/platform/gpio-leds/leds/nsa325:green:sata1
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:green:sata2 -> ../../devices/platform/gpio-leds/leds/nsa325:green:sata2
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:green:sys -> ../../devices/platform/gpio-leds/leds/nsa325:green:sys
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:green:usb -> ../../devices/platform/gpio-leds/leds/nsa325:green:usb
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:orange:sys -> ../../devices/platform/gpio-leds/leds/nsa325:orange:sys
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:red:copy -> ../../devices/platform/gpio-leds/leds/nsa325:red:copy
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:red:sata1 -> ../../devices/platform/gpio-leds/leds/nsa325:red:sata1
lrwxrwxrwx 1 root root 0 Jan 27 15:58 nsa325:red:sata2 -> ../../devices/platform/gpio-leds/leds/nsa325:red:sata2

So to turn on the red LED flashing

echo none > /sys/class/leds/nsa325:green:sata1/trigger 
echo ide-disk1  > /sys/class/leds/nsa325:red:sata1/trigger
or to make it stay solid red:
echo default-on > /sys/class/leds/nsa325:red:sata1/trigger

And for brightness, usually you can echo a value to brightness (0-255). But IIRC, this box does not allow LED brightness change. You can play with it to see if it works.

Quote
ls -l /sys/class/leds/nsa325\:red\:sata1/
total 0
drwxr-xr-x 3 root root 0 Jan 27 16:03 .
drwxr-xr-x 11 root root 0 Jan 27 16:03 ..
-rw-r--r-- 1 root root 4096 Jan 27 16:03 brightness
lrwxrwxrwx 1 root root 0 Jan 27 16:03 device -> ../../../gpio-leds
-r--r--r-- 1 root root 4096 Jan 27 16:03 max_brightness
drwxr-xr-x 2 root root 0 Jan 27 16:03 power
lrwxrwxrwx 1 root root 0 Jan 27 16:03 subsystem -> ../../../../../class/leds
-rw-r--r-- 1 root root 0 Jan 27 16:03 trigger
-rw-r--r-- 1 root root 4096 Jan 27 16:03 uevent

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: LEDs on NSA325v2
January 28, 2021 01:25AM
Hello,

> I don't think the buzzer works on this box.

Strange, because IIRC it worked when I was using stock OS long time ago (it played beep sound when I long-pressed the power button to shut the box down), but it's not a problem, it is a small price to pay for getting rid of the original OS.


> Look in your rootfs /etc/rc.local for example how
> to manipulate the GPIO.

Thank you, there are many other examples in this file. I figured not all are for my system, but looking at files in /sys/class/leds it is easy to figure out what examples apply to me.


> And for brightness, usually you can echo a value
> to brightness (0-255). But IIRC, this box does
> not allow LED brightness change. You can play with
> it to see if it works.

Yeah it does not support brightness change, but that was expected (it was a cheap box). I just thought I have to manually set the brightness in those files in order to turn on leds.

Thank you again for helping me out, and thank you for the awesome work you are doing here!
Re: LEDs on NSA325v2
January 28, 2021 07:02AM
One last question came to my mind. My power button is not working when the device is running. Is there a way to execute shutdown command when I press (or hold) the power button?
Re: LEDs on NSA325v2
January 28, 2021 03:49PM
Dyce,

> One last question came to my mind. My power button
> is not working when the device is running. Is
> there a way to execute shutdown command when I
> press (or hold) the power button?


See the Wiki topic


Quote

Key daemon (button control)

Multimedia keyboard daemon for Linux
How to use set up button to reboot or shutdown with Key Daemon (esekeyd)

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: LEDs on NSA325v2
January 31, 2021 04:46AM
Sorry for the late reply.

bodhi Wrote:
> See
> the
> Wiki topic

>
>
>
Quote

Key daemon (button control)
>
> Multimedia keyboard daemon for Linux
> How to use set up button to reboot or shutdown
> with Key Daemon (esekeyd)


I will look into it, thank you very much!
Re: LEDs on NSA325v2
February 02, 2021 09:42AM
Hi guys!
@Dyce have you found out how to report degraded raid using red leds?
Please share it.
Thank you.

Ivan
Re: LEDs on NSA325v2
April 12, 2021 01:53PM
I know it's not Christmas, but I accidentally wrote this script after reading this thread. It will randomly switch on and off random LEDs.

#!/bin/bash

cd /sys/class/leds

LEDS=(*)
ONOFF=('none' 'default-on')

while true ; do
  LED=${LEDS[$((0 + $RANDOM % ${#LEDS[@]} ))]}
  VALUE=${ONOFF[$RANDOM % 2]}
  echo $VALUE > ${LED}/trigger
  sleep 0.1
done
Re: LEDs on NSA325v2
April 13, 2021 02:27AM
antrew Wrote:
-------------------------------------------------------
> I know it's not Christmas, but I accidentally
> wrote this script after reading this thread. It
> will randomly switch on and off random LEDs.
>
>
> #!/bin/bash
> 
> cd /sys/class/leds
> 
> LEDS=(*)
> ONOFF=('none' 'default-on')
> 
> while true ; do
>   LED=${LEDS[$((0 + $RANDOM % ${#LEDS[@]} ))]}
>   VALUE=${ONOFF[$RANDOM % 2]}
>   echo $VALUE > ${LED}/trigger
>   sleep 0.1
> done
>

Cool :)

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: LEDs on NSA325v2
December 27, 2021 12:11PM
Is it possible to emulate stock led behavior as follows?

SATA:
* if disk absent -> off
* if disk present -> on
* if activity -> blink
Re: LEDs on NSA325v2
December 27, 2021 03:56PM
> Is it possible to emulate stock led behavior as
> follows?
>
> SATA:
> * if disk absent -> off
> * if disk present -> on
> * if activity -> blink

It is not possible with the current Linux mainline driver, because the blink always ends with the LED off state (stock is a custom Linux kernel, so what they do only apply to that specific box). I could modify the driver to add another state (disk present) to set the LED that way. But it will change the default behavior for all Kirkwood boxes, which is not good because it will break the scripts everybody already has in their boxes (that expects the SATA LED to be off as default).

We could write a script that monitors the disk present (like antrew did for fun above), or piggyback on the SMART tools cron job, to set the LED periodically. But I think that's a bit much for a cosmetic purpose.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: LEDs on NSA325v2
December 28, 2021 04:44AM
Thanks for your reply! I agree, it's not really worth it.

While I was there, I was also trying to change the USB led behavior from usb-host to usbport. I have my rootfs on usb1-port1 (back port nearest to the fan, kernel 5.15.5), and I wanted the USB led to be solid on when a device is plugged in one of the other ports. I was able to set it up, but there is something I don't understand in the mapping of the usb ports:

root@nsa325:/sys/devices/platform/gpio-leds/leds/nsa325:green:usb/ports# ls
.  ..  1-1-port1  1-1-port2  1-1-port3  1-1-port4  usb1-port1  usb2-port1  usb2-port2  usb3-port1  usb3-port2

# /etc/rc.local
...
if [ -d /sys/class/leds/nsa325:green:usb ]; then
#   echo usb-host > /sys/class/leds/nsa325\:green\:usb/trigger    # Initial config
   echo usbport > /sys/class/leds/nsa325\:green\:usb/trigger
   echo 0 > /sys/class/leds/nsa325\:green\:usb/ports/1-1-port1     # Back, near the fan
   echo 1 > /sys/class/leds/nsa325\:green\:usb/ports/1-1-port2     # Back, farthest from the fan
#   echo 0 > /sys/class/leds/nsa325\:green\:usb/ports/usb1-port1
#   echo 1 > /sys/class/leds/nsa325\:green\:usb/ports/usb1-port2
   echo 1 > /sys/class/leds/nsa325\:green\:usb/ports/usb3-port1   # Front
fi
...

From experimentation, /sys/class/leds/nsa325\:green\:usb/ports/usb1-port1 behaves exactly like /sys/class/leds/nsa325\:green\:usb/ports/1-1-port1 but while 1-1-* exposes port2, usb1-* does not.
usb2-* seems not related to any external port.
Maybe there is some kind of duplication/naming issue going on? Is there anything I can check to better understand this behavior?

Another small question: would it be possible to simply invert the leds (i.e. default on but flashing off)? I tried researching this and it seems that it would be enough to echo 1 > /sys/class/leds/[led]/invert but the invert attribute is not present.
Re: LEDs on NSA325v2
December 28, 2021 03:38PM
The LEDs in /sys/class/leds/ are populated by the box DTS. So it is easiest to see when you look at the source code patches in the release thread: linux-5.15.5-kirkwood-tld-1.patch. Scroll down to:

diff -Naur --no-dereference a/arch/arm/boot/dts/kirkwood-nsa325.dts b/arch/arm/boot/dts/kirkwood-nsa325.dts

I don't recall we have any usb-port individually defined, and no "invert" either. The LED trigger is only for USB host in general.. So that why they did not show up in /sys/class/leds.

And notice that when you plug in a serial module converter to be serial console, the USB LED is flashing intermittenly even without USB disk access.

Currently, some Kirkwood boxes don't have correct Ethernet port LED behavior, either. They are working perfectly fine, but you don't see Green for 1Gbs and Amber for slower speed.

This is something I can look into and improve the LED controls in the DTS for future releases. But it's cosmetic so I have not thought about them at all.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: LEDs on NSA325v2
February 10, 2022 02:20AM
Completely unnecessary ;-) But I wanted some visual confirmation when I pushed the buttons. So I did these very simple scripts.

/etc/esekeyd.conf
#For testing
#KEY_116:/usr/bin/logger -s -i "POWER button pushed"
#RESTART:/usr/bin/logger -s -i "RESTART button pushed"
#KEY_133:/usr/bin/logger -s -i "COPY Button pushed"
#The real stuff
KEY_116:/root/run_when_button_power_is_used.sh
KEY_133:/root/run_when_button_copy_is_used.sh

/root/run_when_button_power_is_used.sh is for shutting down
#!/bin/bash
#

# First all LEDs on, and wait 1 second
echo default-on  > /sys/class/leds/nsa326:white:sys/trigger
echo default-on  > /sys/class/leds/nsa326:red:sys/trigger
echo default-on  > /sys/class/leds/nsa326:white:sata1/trigger
echo default-on  > /sys/class/leds/nsa326:white:sata2/trigger
echo default-on  > /sys/class/leds/nsa326:white:usb/trigger
sleep 1

# Turn LEDs off one at a time, from down to up, wait 0.5 seconds between
echo none  > /sys/class/leds/nsa326:white:usb/trigger
sleep 0.5
echo none  > /sys/class/leds/nsa326:white:sata2/trigger
sleep 0.5
echo none  > /sys/class/leds/nsa326:white:sata1/trigger
sleep 0.5
echo none  > /sys/class/leds/nsa326:white:sys/trigger
sleep 1

# Set all LEDs but red to normal and then shut down
echo default-on  > /sys/class/leds/nsa326:white:sys/trigger
#echo none        > /sys/class/leds/nsa326:red:sys/trigger
echo ide-disk1  > /sys/class/leds/nsa326:white:sata1/trigger
echo ide-disk2  > /sys/class/leds/nsa326:white:sata2/trigger
echo usb-host > /sys/class/leds/nsa326:white:usb/trigger

/sbin/shutdown -h now
/root/run_when_button_copy_is_used.sh is almost the same as for power button above, but with the change that /sbin/shutdown with copy button restarts at the end
/sbin/shutdown -r now
Re: LEDs on NSA325v2
February 10, 2022 04:19PM
rafee,

Neat :)

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: