2009-06-02 06:09:19     To Know removal and insertion of USB

Document created by Aaronwu Employee on Aug 15, 2013
Version 1Show Document
  • View in full screen mode

2009-06-02 06:09:19     To Know removal and insertion of USB

Narendra Nath (INDIA)

Message: 75010   

 

Hi all

 

        I am working on USB based application.Is there any way to make application know if the USB is inserted or removed.

 

To be more specific my application looks like

 

 

 

main()

 

{

 

                  /******** Insert all USB Modules***************/

 

                          system("modprobe usbcore");

                         system("insmod usb-storage.ko");

                         system("insmod bfin-isp1761.ko");

                         system("insmod hal_pxa.ko");

                        system("insmod pehci.ko");

 

              while(1)

 

              {

 

                      ret= check for USB_INSERT()/******************* Some how i need to get this function pass info from kernel**/

 

                     if(ret==1)

 

                             DISPLAY(USB__SYMBOL);

 

                      else

 

                            CLEAR_DISPLAY(USB__SYMBOL);

 

           }

 

I am looking for check for USB_INSERT() kind of function/Variable to intimate application about USB insertion and removal.

 

Best Regards

 

Narendra

QuoteReplyEditDelete

 

 

2009-06-02 06:20:35     Re: To Know removal and insertion of USB

Mike Frysinger (UNITED STATES)

Message: 75013   

 

search the forums and you'll find th is answered already

 

you can use hotplug or netlink:

http://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:hotplug

QuoteReplyEditDelete

 

 

2009-06-02 06:27:43     Re: To Know removal and insertion of USB

ian davidson (UNITED KINGDOM)

Message: 75017   

 

Narendra,

 

You may find this snippet of shell script useful, it may need changing a bit to suit your needs.

 

    modprobe usb-storage

    modprobe pehci

    modprobe hal_pxa

    # Create a zero filled file in ram to be used as a file system

    dd if=/dev/zero of=/tmp/usb.mem bs=256k count=6 2>/dev/null

    modprobe g_file_storage file=/tmp/usb.mem stall=0

    fdisk -S 8 -H 2 -C 192 /tmp/usb.mem <$SCRIPTS/fdisk_input  >NULL

 

    # HOST

    echo == USB HOST PORT ==

    echo Insert USB memory device

    if [ ! -d /mnt/usb ] ; then mkdir /mnt/usb; fi

    # try and mount the device, exit when mounted OK

    USBFOUND="0"

    while [ $USBFOUND -ne "1" ];

    do

        mount -t vfat /dev/sda1 /mnt/usb 2>/dev/null

        if [ "$?" -ne 0 ] ;

        then

            echo please insert usb stick

            sleep 2

        else

            echo thanks!

            USBFOUND="1";

        fi

    done

  

    # now copy a file onto the usb stick and compare it, use this file

    SRC=$SCRIPTS/btest

    DST=/mnt/usb/btest

    cp $SRC $DST

    diff -q -s $SRC $DST

    if [ "$?" -ne 0 ];

    then

        echo USB HOST test fail

    else

        echo USB HOST test pass

    fi

QuoteReplyEditDelete

 

 

2009-06-02 07:07:30     Re: To Know removal and insertion of USB

ian davidson (UNITED KINGDOM)

Message: 75019   

 

Just realised that the script uses a file called fdisk_input

 

It contains answers to the prompts for fdisk.

 

Here it is:

 

n

p

1

 

 

t

b

w

QuoteReplyEditDelete

 

 

2009-06-03 03:36:56     Re: To Know removal and insertion of USB

Narendra Nath (INDIA)

Message: 75050   

 

Hi Mike and Ian

 

     Thank you for your replies, I have searched in the forum and found how to use hotplug..,

 

     I have enabled Hotplug option and used the following script  in hotplug .alo i added kill all  to signal to my application when ever atatch and detach happens

 

#!/bin/sh

 

output=/dev/ttyBF0

 

/bin/mdev > $output

killall -USR2 MYUSBApplication

echo " Hotplug Script"> $output

 

Now that when ever i am inserting/removing USB i am able to get

 

Hotplug Script.

 

To handle the signal i have added the following in my MYUSBApplication i have added the following

 

sighandler_t signal(int signum, sighandler_t handler)

  {

      static char USB_connection=0;

       if(USB_connection==0)

      {

            printf("\n USB is detached");

           fflush(stdout);

            USB_connection=1;

 

      }

     else

     {

           printf("\n USB is attached");

           fflush(stdout);

          USB_connection=0;

 

      }

}

 

I am not able to view the USB is attached ,please let me know if  the Signal sending in kernel and signal handling in application is OK

 

 

 

Best Regards

 

Narendra

Attachments

    Outcomes