DSM-G600, DNS-3xx and NSA-220 Hack Forum

Unfortunately no one can be told what fun_plug is - you have to see it for yourself.

You are not logged in.

Announcement

#26 2008-02-04 14:34:53

fonz
Member / Developer
From: Berlin
Registered: 2007-02-06
Posts: 1716
Website

Re: chkbutton, buttons and leds

orbitaudio wrote:

What do you know about the ioctl() interface?  I'm kind of confused as to how everything is connected.  From what i understand, a program calls ioctl() with the right parameters via a socket to egiga0.  I'm confused as to what happens after that.  I know the buttons and leds are connected to GPIO somewhere, but which chip's GPIO are the connected to?  The ethernet chips GPIO?

I've disassembled chkbutton but that didn't really get me far as there are too many assembly subroutines going on for functions that i'm not really interested in.  My ARM assembly is also not very good.  I tried to run strace on it but chkbutton doesn't seem to run with my new debian install hmm

Most of what I know is written down in my dns323-utils. Sources are available here: http://www.inreto.de/dns323/utils/
The GPIO connections are listed in the orion git tree (link from earlier post).
The egiga0 ioctl is a dirty hack to make stuff available to user space, there's no relation between the network interface and the functions triggered via the ioctls. My fun_plug has an strace included that works well (afaik).

Offline

 

#27 2008-02-05 13:34:44

orbitaudio
Member
From: Brisbane, Australia
Registered: 2008-01-23
Posts: 25

Re: chkbutton, buttons and leds

Well after a fair bit more trial and error i've got some code for an equivalent chkbutton for the DNS323 using ioctl().  For anyone who has a DNS323 and does not want to run chkbutton, but wants to have the functionality of SAFELY shutting down their NAS with the front panel button, I have documented what to do:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

/*

ioctl SIOCDEVPRIVATE = 0x89f0

call SIOCDEVPRIVATE + 8 to read a register

*** Before ioctl() is called ***
data[0] = 0x10110 - the gpio input register
data[1] = doesn't matter

*** After ioctl() is called ***
data[0] = input register value
data[1] = undefined

*/


#define BTN_POWER 0x0200

int main(int argc, char **argv)
{
    int fd, i;
    struct ifreq ifr;
    unsigned long data[2] = { 0, 0 };
    const char *ifname = "egiga0";

    fd = socket(AF_INET, SOCK_DGRAM, 0);

    /* poll the input register every second*/

    unsigned char pressed = 0;
    while(1)
    {
        memset(&ifr, 0, sizeof(ifr));
        strcpy(ifr.ifr_name, ifname);
        data[0] = 0x10110;
        ifr.ifr_data = (void *)data;
        if (ioctl(fd, 0x89f8, &ifr) == -1)
        {
                perror("ioctl");
        }
        else
        {
            if (!(data[0] & BTN_POWER)) //active low
            {
                /* if it is active, see if it has been active previously */
                if (pressed)
                {
                    printf("Shutting down now...\n");
                    pressed = 0; // shutdown
                    if ( system("/sbin/shutdown -h now") == -1)
                    {
                        printf("error shutting down\n");
                    }
                }
                /* wait for 3 seconds and go around the loop again */
                pressed = 1;
                sleep(3);
            }
            else
            {
                /* not pressed, check the button status every second */
                pressed = 0;
                sleep(1);
            }

        }
    }
}

1. I compiled chkbutton2 (the above code) by adding it to fonz's dns323-utils Makefile and put it in /usr/local/sbin along with all the other dns323-util binaries.
2. I added the following lines to /etc/rc.local

Code:

echo "Setting power led state"
/usr/local/sbin/dns323-leds 0x00

echo "Starting chkbutton replacement"
/usr/local/sbin/chkbutton2 &

3. I added /usr/local/sbin/dns323-poweroff to /etc/init.d/halt on the line after halt is called. (this is on my debian etch system)
4. I added K10ledflash to link to /etc/init.d/ledflash
5. I wrote the script /etc/init.d/ledflash to call /usr/local/sbin/dns323-leds 0x10 to flash the power led when the NAS is shutting down (see code below)

Code:

#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin
EXECUTABLE=dns323-leds

do_stop () {
        if [ -x "/usr/local/sbin/$EXECUTABLE" ]
        then
                /usr/local/sbin/$EXECUTABLE 0x10

        else
                echo "No $EXECUTABLE binary installed in /usr/local/sbin"
        fi
}

case "$1" in
  start)
        # No-op
        ;;
  restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
  stop)
        do_stop
        ;;
  *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

This gives back the old functionality of shutting down the NAS safely after the power button has been held in for 3 seconds (which could be changed in the above code - change sleep(3) to sleep(num_of_secs)) to anyone who wants it for a DNS323. 

Thanks go out to fonz for a) his awesome dns323-utils which helped me along quite a bit (which i basically copied to make the chkbutton prog) and b) for his actual replies in this thread!

Hope this helps someone.

orbit

Last edited by orbitaudio (2008-02-05 13:43:17)

Offline

 

#28 2008-10-17 00:53:26

puterboy
Member
Registered: 2008-09-18
Posts: 306

Re: chkbutton, buttons and leds

fonz wrote:

mykroft wrote:

This LED util still avail - old directory no longer exists....

Sources are in "utils" directory now. I don't have binaries handy now. If you can't compile yourself, let me know, but it may take a few days until I have time...

Fonz - what is the status of the LED utilities?
Sounds like it could be fun to play with...

Offline

 

#29 2008-10-18 00:16:58

fonz
Member / Developer
From: Berlin
Registered: 2007-02-06
Posts: 1716
Website

Re: chkbutton, buttons and leds

puterboy wrote:

Sounds like it could be fun to play with...

It's there and part of ffp (/ffp/bin/dns323-leds). For the lightshow, try

Code:

dns323-leds 0x16

For available documentation, see http://www.inreto.de/dns323/utils/

Offline

 

#30 2009-12-01 13:39:46

maurol
New member
Registered: 2009-12-01
Posts: 1

Re: chkbutton, buttons and leds

Hi,
I'm using your chkbutton2 code in a D-Link DNS323 with Hardware rev. C1 and Firmware version 1.07.
I have to change the following define

Code:

#define BTN_POWER 0x0200

to

Code:

#define BTN_POWER 0x0002

to make it work. Apparently the gpio registers and/or the driver implementation changed in this hardware and firmware revisions.

The leds seem to have changed also, as I'm unable to correctly crontrol them using the dns323-leds program. I'll look into that now.

Best regards, thanks.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2010 PunBB