DM368 DM365 Ethernet cable plug unplug

From RidgeRun Developer Connection
Jump to: navigation, search


I noticed that when using a Leopardboard, Ethernet cable plug / unplug events were not getting detected. Stated another way,

cat /sys/class/net/eth0/carrier

was not correctly reporting if the Ethernet link beat was present.

The goal was to detect if a live cable is connected so that the DHCP client daemon could be used to get an IP address for the device. The following information should be helpful for anyone trying to handle Ethernet in a reasonable way in an embedded device.

DM36x MDIO configuration

The first problem encountered was the kernel board file wasn't properly telling Linux about the PHY being used.

The following was added to the arch/arm/mach-davinci/board-*.c

/* PHY_MASK indicates to the davinci_emac driver which MDIO addresses
 * to scan for PHY's.  Whatever bits are 1 in this mask are the addresses that
 * that davinci_emac driver will look for a PHY. */
#define DM36x_PHY_MASK    (0x1)
#define DM36x_MDIO_FREQUENCY  (2200000) /* PHY bus frequency */


static void dm368_emac_configure(void)
        struct davinci_soc_info *soc_info = &davinci_soc_info;

        /* Specify the set of possibly populated  phy addresses */
        soc_info->emac_pdata->phy_mask = DM36x_PHY_MASK;
        soc_info->emac_pdata->mdio_max_freq = DM36x__MDIO_FREQUENCY;


Once the davinci emac driver knew where to look for the phy, cable plug /unplug events were properly detected.

Testing cable plug /unplug events

I booted without the Ethernet cable being plugged in.

cat /sys/class/net/eth0/carrier    # in the text below, this file is referred to as the sysfs ETH0 carrier file

returned 0 indicating no carrier was detected.

When I plugged in the Ethernet cable, the console reported:

PHY: 1:00 - Link is Up - 100/Full

and when I checked the sysfs ETH0 carrier file, it properly reported a '1. I unplugged the cable, and saw:

PHY: 1:00 - Link is Down

and when I checked the sysfs ETH0 carrier file, it properly reported a '0.

Network interface up down

The /etc/network/interfaces file is used by ifup and ifdown. A simplified working example /etc/network/interfaces file contains:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
	script /etc/udhcp.script

We can use ifup and ifdown to control both the loopback interface and the ethernet interface.

ifplugd network plug unplug deamon

The user space ifplugd daemon is used to monitor Ethernet cable plug / unplug events. Once a plug or unplug event occurs, it calls a script to handle the event. A simplified working example /etc/ifplugd/ifplugd.action contains:

# parameters:
# $1: interface
# $2: state

echo ifplugd.action: $* > /dev/console

if test x"$2" = x"down"; then
	ifdown $1

if test x"$2" = x"up"; then
	ifup $1

The echo is just to show what is going on and can be removed in a real system.

Device startup

When the device turns on, the /etc/init.d/network file is used to bring up the loopback interface and start ifplugd to monitor Ethernet cable plug / unplug events. A simplified working example /etc/init.d/network contains:

#!/bin/sh -e

case "$1" in
		echo "Configuring network interfaces"
		ifdown lo
		ifup lo
		ifplugd -i eth0 -r /etc/ifplugd/ifplugd.action 

		echo "Deconfiguring network interfaces"
		killall ifplugd
		ifdown lo
		ifdown eth0


First test is to power on the device without an Ethernet cable attached. For each test the following commands are run:

cat /sys/class/net/eth0/carrier
ifconfig eth0

Notice that the carrier is zero, loopback interface is configured, ifplugd is running and dhcpcd is not running. I am not sure why eth0 is in the ifconfig output since no ifup on the Ethernet interface has yet been performed.

Second test is to power on the device with an Ethernet cable attached, and again run the commands. Notice that the carrier is one, loopback interface is configured and eth0 is configured and has an IP address, and ifplugd and dhcpcd are both running.

Third test is to unplug the cable and run the commands. You can guess the results.

Forth test is to plug the cable and run the commands. Again, you can guess the results.