2009-05-07 03:30:00     Regarding USB detection from my application

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

2009-05-07 03:30:00     Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73791   

 

Hi,

 

I have to develop an application that is to work with USB contents. When a USB device is connected to my BF527 device, I have the following options through I can come to know the USB detection:

 

1. Keep polling in my application to check whether USB is connected

 

2. Kernel informs my application when a USB is connected

 

I request you to guide me to provide some link to docs which will help me to implement either of the two steps.

 

 

QuoteReplyEditDelete

 

 

2009-05-07 03:47:31     Regarding USB detection from my application

Michael Hennerich (GERMANY)

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

 

Some stupid hotplug script:

 

#!/bin/sh

 

/bin/mdev

 

# Define where to put the output message

 

output=/dev/tty0

 

# We only manage sda1 events

 

echo $DEVPATH | grep sda1

if [ $? -ne 0 ]

then

exit 0

fi

 

echo "Hotplug Script begin" > $output

 

# If key inserted, mount it and play one mp3 file

 

if [ $ACTION = "add" ]

then

mount -t vfat /dev/sda1 /mnt

echo "Key mounted" > $output

find /mnt/ -name "*.mp3" -print0 | xargs -0 mp3play -v -P -B 1024 > $output

fi

 

# If key removed, umount it

if [ $ACTION = "remove" ]

then

echo "Hotplug Remove" > $output

killall mp3play

umount /dev/sda1

echo "Key unmounted" > $output

fi

 

echo "Hotplug Script end" > $output

QuoteReplyEditDelete

 

 

2009-05-07 05:26:58     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73812   

 

Hi Michael,

 

Thanks for the inputs.

 

In the usb stack for uclinux, is there any callback feature available to which application specific function can be hooked to the usb stack so that when this callback is invoked from the kernel space, it gets executed in the user space?

 

Please clarify.

QuoteReplyEditDelete

 

 

2009-05-07 05:50:42     Re: Regarding USB detection from my application

Michael Hennerich (GERMANY)

Message: 73813    First of all there is nothing like an uclinux specific usb stack.

uClinux == Linux - and therefore uses the same usb stack.

 

Simple answer to your question - NO

QuoteReplyEditDelete

 

 

2009-05-07 06:27:50     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73815   

 

Hi Michael,

 

Could you please provide me some inputs as to how I can notify my application using the 'hotplug' mechanism. In the link "  docs.blackfin.uclinux.org/doku.php?id=linux-kernel:hotplug", its mentioned that kernel will execute a program mentioned in "/proc/sys/kernel/hotplug". My application is a C application in which I should take some action whenever a USB is connected to my device. However, the notification to the app must happen in a "non-polling" way. Could you guide me on how to bridge the kernel notification feature and my application using the hotplug mechanism.

QuoteReplyEditDelete

 

 

2009-05-07 13:57:29     Re: Regarding USB detection from my application

Mike Frysinger (UNITED STATES)

Message: 73830   

 

so have the hotplug script send a signal to your application.  something like `killall -USR2 your-app`.

QuoteReplyEditDelete

 

 

2009-05-08 00:04:05     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73846   

 

Hi,

 

Its mentioned in the hotplug link   docs.blackfin.uclinux.org/doku.php?id=linux-kernel:hotplug about some hotplug environment variables like ACTION, DEVPATH etc using which the kernel passes info about the device. If that is the case, suppose I have booted the kernel and type $ACTION in my busybox prompt, is it expected to display "remove" and similarly display "add" when a USB is connected? Please clarify.

QuoteReplyEditDelete

 

 

2009-05-08 01:27:42     Re: Regarding USB detection from my application

Mike Frysinger (UNITED STATES)

Message: 73850   

 

i dont see how typing anything into your shell prompt has any relation to the environment used when running a hotplug script

QuoteReplyEditDelete

 

 

2009-05-08 03:42:40     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73860   

 

Hi Mike,

 

Could you please clarify the following:

 

1. What are the ways (other than hotplug) through which I can notify user space from kernel, the addition and removal of a USB device?

 

2. Is it possible to have a non-polling mechanism (like callbacks) to inform the user space, the addition and removal of a USB device in uclinux kernel?

QuoteReplyEditDelete

 

 

2009-05-08 03:51:20     Re: Regarding USB detection from my application

Mike Frysinger (UNITED STATES)

Message: 73861   

 

the hotplug mechanism is the only way.  you can either use the method you've already been pointed to, or you can use the netlink method just like udevd does it.

QuoteReplyEditDelete

 

 

2009-05-08 07:16:33     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73887   

 

Hi Mike,

 

When I connect my USB stick, I get the following message in UART:

 

 

root: /> usb 1-1: new high speed USB device using musb_hdrc and address 2

usb 1-1: configuration #1 chosen from 1 choice

scsi0 : SCSI emulation for USB Mass Storage devices

scsi 0:0:0:0: Direct-Access     SanDisk  Cruzer           7.01 PQ: 0 ANSI: 0 CCS

sd 0:0:0:0: [sda] 1957887 512-byte hardware sectors: (1.00 GB/955 MiB)

sd 0:0:0:0: [sda] Write Protect is off

sd 0:0:0:0: [sda] Assuming drive cache: write through

sd 0:0:0:0: [sda] 1957887 512-byte hardware sectors: (1.00 GB/955 MiB)

sd 0:0:0:0: [sda] Write Protect is off

sd 0:0:0:0: [sda] Assuming drive cache: write through

sda:<7>usb-storage: queuecommand called

sda1

sd 0:0:0:0: [sda] Attached SCSI removable disk

 

I would like to know from which part of the kernel source code (like filename), these info are getting generated. Please clarify.

QuoteReplyEditDelete

 

 

2009-05-08 12:10:37     Re: Regarding USB detection from my application

Robin Getz (UNITED STATES)

Message: 73891   

 

Hari:

 

Hacking the kernel source is not recommended.

 

Why don't you want to use the methods (hotplug) that Mike and Michael pointed you to?

 

-Robin

QuoteReplyEditDelete

 

 

2009-05-08 12:57:34     Re: Regarding USB detection from my application

Amol Sukerkar (UNITED STATES)

Message: 73895   

 

Hari:

 

The messages that you posted come from a lot of kernel files in SCSI, msdos if applicable , FAT or whatever filesystem the disk has on it and of course USB driver code. It is not easy to hack all these and not break anything. So, I agree with Robin here and would recommend using HOTPLUG. Although this does not help you in any way, it should stop you from looking in wrong places.

 

 

 

My $0.02,

 

Amol

QuoteReplyEditDelete

 

 

2009-05-11 01:28:57     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73929   

 

  Hi Amol and Robin:

 

Thanks for your guidance. I am still bit unclear as to how I can use a script to notify a C application. My application is a media player application which would be running in the foreground. When I connect USB to the device, how can I use the hotplug script to notify my app without killing my app? One idea which I can think of is to write a script that would add a new environment variable. My app would keep polling for this variable and would do the needful. Please clarify.

QuoteReplyEditDelete

 

 

2009-05-11 01:51:24     Re: Regarding USB detection from my application

Mike Frysinger (UNITED STATES)

Message: 73930   

 

i already told you exactly how: use killall to send a USR signal

 

applications cannot touch the environment of any other process.  that is simply how POSIX works.

QuoteReplyEditDelete

 

 

2009-05-11 05:16:39     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73939   

 

Hi Mike,

 

I now got your point of using killall. Thanks. However, am facing a problem wherein I had modified the file /proc/sys/kernel/hotplug to invoke my custom shell script my.sh which will issue the killall command to my app as follows:

 

/proc/sys/kernel/hotplug file contents: /bin/sh /my.sh

 

my.sh file contents: killall -USR2 myapp

 

When I plugged in my USB stick, I expected the script my.sh to be invoked via /proc/sys/kernel/hotplug but it didnt happen. Could you please let me know if am missing anything and how to ensure kernel calls /proc/sys/kernel/hotplug script when a USB device is plugged in or removed?

QuoteReplyEditDelete

 

 

2009-05-11 07:03:26     Re: Regarding USB detection from my application

Michael Hennerich (GERMANY)

Message: 73943    Modify the example I posted. You still need mdev.

-Michael

QuoteReplyEditDelete

 

 

2009-05-11 07:40:52     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73947   

 

Hi Michael,

 

To check whether hotplug script works fine for me or not, I have established the following setup:

 

/proc/sys/kernel/hotplug contents:

 

root:/> cat /proc/sys/kernel/hotplug

sh /hotplug.sh

 

hotplug.sh contents:

 

#!/bin/sh

 

/bin/mdev -s

 

# Define where to put the output message

 

output=/dev/ttyBF0

 

echo "Hotplug Script begin" > $output

 

 

echo "Hotplug Script end" > $output

 

When I run the script standalone from command line, it works fine for me and gives the following output as expected:

 

Hotplug Script begin

Hotplug Script end

 

 

However, when I plug in my USB stick, I get the following:

 

oot:/> usb 1-1: USB disconnect, address 4

usb 1-1: new high speed USB device using musb_hdrc and address 5

usb 1-1: configuration #1 chosen from 1 choice

scsi3 : SCSI emulation for USB Mass Storage devices

scsi 3:0:0:0: Direct-Access     Memorex  TD Classic 003B  PMAP PQ: 0 ANSI: 0 CCS

sd 3:0:0:0: [sda] 4016128 512-byte hardware sectors: (2.05 GB/1.91 GiB)

sd 3:0:0:0: [sda] Write Protect is off

sd 3:0:0:0: [sda] Assuming drive cache: write through

sd 3:0:0:0: [sda] 4016128 512-byte hardware sectors: (2.05 GB/1.91 GiB)

sd 3:0:0:0: [sda] Write Protect is off

sd 3:0:0:0: [sda] Assuming drive cache: write through

sda:<7>usb-storage: queuecommand called

sda1

sd 3:0:0:0: [sda] Attached SCSI removable disk

 

 

And when I remove, I get the following:

 

usb 1-1: USB disconnect, address 3

 

As you can see, hotplug.sh has not been executed. What is the reason for the script hotplug.sh not getting executed? Am I missing anything?

QuoteReplyEditDelete

 

 

2009-05-11 14:12:12     Re: Regarding USB detection from my application

Mike Frysinger (UNITED STATES)

Message: 73957   

 

you're using the tunable wrong.  it isnt a shell script line, it's the program to execute.  it is the full path to a program and nothing else.

QuoteReplyEditDelete

 

 

2009-05-12 04:31:42     Re: Regarding USB detection from my application

Hari Prasad (INDIA)

Message: 73986   

 

Hi,

 

Then how can I invoke my custom script on inserting or removing USB? Also, how do I enable the hotplug environment variables?

Attachments

    Outcomes