Author Topic: Using the USB Watchdog with Linux  (Read 6511 times)

Admin

  • Administrator
  • Jr. Member
  • *****
  • Posts: 49
    • View Profile
Using the USB Watchdog with Linux
« on: February 11, 2011, 12:55:36 »
This is a question we get frequently. Here is the answer for a customer using Debian v6:

To use watchdog devices in Linux you will need a watchdog deamon.
This daemon will communicate via the /dev/watchdog device to your watchdog-driver/device.
The watchdog daemon needs to tickle/ping the watchdog to keep the system running.
If the daemon doesn't tickle/ping the watchdog, then the system will now that something is wrong and reboot.

How is this done in debian:

First make sure that you have the USB watchdog module loaded.
With the following command you can see it the module exists:
debian-server:~# ls /lib/modules/`uname -r`/kernel/drivers/watchdog/pcwd_usb.ko

Check if the module is loaded with:
debian-server:~# lsmod | grep pcwd_usb
If it is not loaded the load the module with:
debian-server:~# /sbin/modprobe pcwd_usb
And you could add it to your /etc/modules files so that it get's automatically loaded on reboot:
debian-server:~# echo 'pcwd_usb' >> /etc/modules

The watchdog dameon is available in the watchdog package.
You can get/install it with:
debian-server:~# apt-get install watchdog

You then need to check it's configuration file (/etc/watchdog.conf) and set things right.
With Debian Lenny you could do this:
debian-server:~# echo "file = /var/log/messages" >> /etc/watchdog.conf
debian-server:~# echo "watchdog-device = /dev/watchdog" >> /etc/watchdog.conf

you can start the watchdog ow with:
debian-server:~# /etc/init.d/watchdog restart
« Last Edit: February 11, 2011, 14:13:58 by Admin »

david

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #1 on: March 22, 2011, 07:47:38 »
Hey... I have a follow up question:

If I don't want to invoke the built-in watch dog module in Linux, can I still use Berkshire PC watchdog?
PS: my OS is ubuntu 9.04 server without watch dog module enabled.

Wim Van Sebroeck

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #2 on: March 22, 2011, 20:37:57 »
Hi David,

I'll have a look at buntu 9.04 server to see what's in and what's not and what will be needed.
You will however need to load a module to get access to the watchdog. Similar as in windows you have to load the dll to be able to communicate with the card.

I'm wondering now how you want to use the Berkshire PC watchdog card under ubuntu 9.04 server. Would you be willing to explain your use case to me? Then I can see what will be needed.

Kind regards,
Wim.

David

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #3 on: March 23, 2011, 01:27:33 »
sure, Wim.

Here is a general description of my use case,

We have a program run on Ubuntu to test some hardware. Due to some testing may cause the system hung up, we want the program can automatically trigger a rebooting after hung. To do this, we need a hard watch dog. How it would work with the watch dog?
1. Upon the start the program, send a message (or call API) to watch dog to set its max wait time and start the timer
2. If the program executes well (means no hung), at the end of the program, send a message (or call API) to watch dog to stop its timer
3. If the program gets hung, the watch dog will trigger a reboot when timing exhausts


As you could see, the key points we wanna the watch dog and host PC to have are:
1. ability to set the max wait time of watch dog dynamically (from host PC)
2. ability to stop the timer of watch dog remotely (from host PC)


Let me know if this is not clear to you.

Your response would be much appreciated.

David

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #4 on: March 25, 2011, 07:35:48 »
Hi Wim, how's is your verification on the PC watchdog on Ubuntu?

Wim Van Sebroeck

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #5 on: March 29, 2011, 13:15:43 »
Hi David,

Sorry for the delay. Had some other issues to tackle...
The use case isn't an issue. I'll post 2 small program's that will do this for you. One that starts the watchdog with max timeout and one that stops the watchdog.

Didn't had time to do the verification of Ubuntu server yet (I did already download it). Will try to do it tonight.
I don't think it will be an issue. worst case is to compile the module myself.

Kind regards,
Wim.

David

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #6 on: March 30, 2011, 01:41:12 »
Thanks Wim.

Just for double confirmation, I need to let the max timeout to be a parameter in the function to start the timer.
Another word, during the execution of my program, I will call watchdog to start timer with specific max timeouts for several times. In the event of watchdog timeout, it will trigger an auto rebooting.

Your help would be much appreciated.

Wim Van Sebroeck

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #7 on: April 05, 2011, 19:23:03 »
Hi David,

attached the two small programs.
* watchdog-start (program that starts the watchdog and sets the timeout (the timeout is the parameter for the program, so start-watchdog 60 will set a timeout of 60 seconds).
* watchdog-stop (program that stops the watchdog).

You can compile them with:
gcc -o watchdog-start watchdog-start.c
gcc -o watchdog-stop watchdog-stop.c

I'll look at the ubuntu-server later this week.

Kind regards,
Wim.
---watchdog-start.c----------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "linux/ioctl.h"

#define WATCHDOG_IOCTL_BASE     'W'
#define WDIOC_SETTIMEOUT        _IOWR(WATCHDOG_IOCTL_BASE, 6, int)

int main(int argc, const char *argv[]) {
        int timeout = 65535;
        int ret;
        int fd;

        /* open /dev/watchdog */
        fd=open("/dev/watchdog",O_WRONLY);
        if (fd==-1) {
                perror("watchdog");
                return 1;
        }

        if (argc==2)
                timeout = atoi(argv[1]);

        /* write the new timeout value */
        ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
        fsync(fd);

        /* Close /dev/watchdog without the magic char
           so that the watchdog keeps running */
        close(fd);

        return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------

---watchdog-stop.c----------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "linux/ioctl.h"

int main(int argc, const char *argv[]) {
        /* Open /dev/watchdog */
        int fd=open("/dev/watchdog",O_WRONLY);
        if (fd==-1) {
                perror("watchdog");
                return 1;
        }

        /* Write the magic character so that the watchdog stops
           when we close /dev/watchdog */
        write(fd, "V", 1);
        fsync(fd);

        /* close /dev/watchdog */
        close(fd);

        return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------

Wim Van Sebroeck

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #8 on: April 05, 2011, 21:24:48 »
Hi David,

I installed a basic Ubuntu 9.04 server and looked in /lib/modules/2.6.28-11-server/kernel/drivers/watchdog/ and saw that pcwd_usb.ko was present. So the description at the top of this thread is valid. You can load the watchdog module with "sudo /sbin/modprobe pcwd_usb". You can also add this module to /etc/modules as described (either as root or with the sudo command) to make it permanent.
With the 2 programs I put in the previous post you can then control the watchdog as desired.

Kind regards,
Wim.

David

  • Guest
Re: Using the USB Watchdog with Linux
« Reply #9 on: April 06, 2011, 12:59:06 »
Thanks very much Wim!

I will verify the watchdog module on my customized Ubuntu tomorrow. Hopefully, it was there as well.

If not, I'm not sure if you have communication code using libusb.

Thanks again.