Linux to implement hot plug event triggering __linux USB port

Source: Internet
Author: User

Now do a hot plug event on the USB port on the embedded platform.

After my present analysis summary currently has the following method: 1, the regular examination/proc/scsi/scsi file

This method can only be on the PC, but is not available on the embedded platform. 2,netlink Way

Use NetLink.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <sys/ un.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <linux/types.h> #include <linux/ netlink.h> #include <errno.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/
    In.h> #define UEVENT_BUFFER_SIZE 2048 static int Init_hotplug_sock () {const int i_buffersize = 1024;

    int i_ret = 0;
    struct SOCKADDR_NL saddr_nl;
    Bzero (&saddr_nl, sizeof (struct sockaddr_nl));
    saddr_nl.nl_family = Af_netlink;
    Saddr_nl.nl_pid = Getpid ();

    saddr_nl.nl_groups = 1;
    int i_sock = socket (Pf_netlink, Sock_raw, netlink_kobject_uevent);
        if (-1 = i_sock) {perror ("socket");
    return-1;

    SetSockOpt (I_sock, Sol_socket, So_rcvbuf, &i_buffersize, sizeof (i_buffersize)); I_ret = bind (I_sock, struct sockaddr *) &saddr_nl, sizeof (struct soCKADDR_NL));
        if (I_ret < 0) {perror ("bind");
        Close (I_sock);
    return-1;
return i_sock;
    int main (int argc, char* argv[]) {int i_rcvlen = 0;
    int i_hotplug_sock = Init_hotplug_sock ();

    if (I_hotplug_sock < 0) return-1;
        while (1) {/* netlink message buffer */char psz_buf[uevent_buffer_size * 2] = {0};
        I_rcvlen = recv (I_hotplug_sock, &psz_buf, sizeof (PSZ_BUF), 0);

            if (I_rcvlen > 0) {printf ("recv msg:%s, Length:%d\n", Psz_buf, strlen (PSZ_BUF));

/* USB device plug will appear character information, by comparing different information to determine the specific device plug, in this add comparison code */} return 0; }

the test, if is uses the datagram way SOCK_DGRAM creates the socket, will appear the packet loss phenomenon;

Sock_raw Way, while loop inside can not sleep,sleep will cause the kernel always to netlink socket message, sleep too long will cause the message to send too much, buffer overflow.

After testing found that only plug and pull, but not specific to what the specific device. 3, use Mdev.

This method is quite troublesome and is now in the study. 4, using the most original parsing file method

This method is rather cumbersome and produces many files.

is to use the Cat/proc/bus/usb/devices information generated by the file to parse.

I have now completed this function. Although it can be used, it is too inefficient.

Http://www.linuxidc.com/Linux/2011-12/49112.htm


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.