This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Callback on Interface arrival/departure

Hi Experts,

Any mechanism or sample program where the registered callback will be invoked when the USB interface is  connected/disconnected based on the VID/PID ?

It is like how the udev invokes the specific actions in the rules file.

Regards,

Lyf

  • Hi lyf,

    Yes, when you query the USB device, you will get the "Device descriptor which encloses VID and PID.

    Usually, querying and getting the descriptor details is the first thing that the USB host will do when the USB device is connected. It is the part of the communication protocol. So, almost in all the USB drivers, you will find the sample code for getting the details of "device descriptor".

    For ease of use, I will refer you to the USB starterware program ( USB host MSC) has the code to query all the descriptors of USB device. You can take the code from there. Starterware download link:

    Regards,

    Shankari

    -------------------------------------------------------------------------------------------------------

    Please click the Verify Answer button on this post if it answers your question.

    --------------------------------------------------------------------------------------------------------

  • Thank u for the reply TItus.
  • Hi Lyf,


    Any mechanism or sample program where the registered callback will be invoked when the USB interface is connected/disconnected based on the VID/PID ?


    You want this requirement from driver level of user space level.

    What you want to do if USB get connected/disconnected ?

    You want like below, need to call scripts when you connect/disconnect the particular USB device.

    I think, thats what Titus has suggested.

    You can put this entry in "etc/udev/scripts/"

    ACTION=="add", ENV{DEVTYPE}=="usb_device", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/usb_add.sh"

    ACTION=="remove", ENV{DEVTYPE}=="usb_device", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c51a", RUN+="/home/user/.scripts/usb_remove.sh"


    Regards,
    Shankari.
  • Hi Shankari,

    Glad to see ur reply.

    I could understand the working of udev and descriptor based polling of devices.

    I think my requirement is bit different here. It was like how I register for a callback for signal hander and each time a user space program receives the signal the call back will be invoked. Likewise instead of invoking a program/script from the udev rules file or polling it from the list of devices attached is it possible as pseudo code as shown below.

    int main()
    {
    register_udev_event(device_arrival_routine);
    while(1);
    deregister_udev_event(device_arrival_routine);
    }


    int device_arrival_routine()
    {
    printf("Device arrived");
    }

    If you are denoting the same sort of provision could I get bit detail on it.
  • Any ideas on the same !!