Gianfranco
Gianfranco

Reputation: 165

How to detect "Over Current" event of an USB device?

I have to detect the event "over current" of an USB device.

I'm developing in a Linux system and C/C++ language.

How do I do that?

Upvotes: 0

Views: 3577

Answers (2)

Rajish
Rajish

Reputation: 6805

You can use uevents. Here is some tutorial. For watching uevents you have to bind specific NETLINK_KOBJECT_UEVENT:

int create_socket()
{
    int                sock   = -1;
    int                result = 0;
    struct sockaddr_nl snl;

    memset(&snl, 0x00, sizeof(struct sockaddr_nl));
    snl.nl_family = AF_NETLINK;
    snl.nl_pid    = getpid();
    snl.nl_groups = -1;

    sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if (sock != -1)
    {
        result = bind(sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
        if (result < 0)
        {
            dbg("bind failed, exit\n");
            close(sock);
            sock = -1;
        }
    }
    else
        dbg("error getting socket, exit\n");
    return sock;
}

int main() {
...    
    sock = create_socket();
    while(sock != -1)
    {
        buflen = recv(sock, &buffer, sizeof(buffer), 0);
        // parse buffer for event description
        ...
    }
}

One of the events for sure will indicate the over-current state.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283773

It might help you to look at the source code for your particular root port / host controller.

For example: http://www.spinics.net/lists/linux-usb/msg49451.html explains the overcurrent detection using a particular host controller driver.

Upvotes: 0

Related Questions