LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   How to detect insertion/removal of flash card in usb reader? (https://www.linuxquestions.org/questions/linux-hardware-18/how-to-detect-insertion-removal-of-flash-card-in-usb-reader-204852/)

logularjason 07-14-2004 06:43 AM

How to detect insertion/removal of flash card in usb reader?
 
I have a Belkin USB flash card reader that is working nicely under Redhat 9.

Is there a way for me to determine when a card has been inserted/removed from the reader? I'd rather not poll, but if I have to, I don't want the test to be: attempt a mount and see if it succeeds.

The drive is an F5U140 (catalog.belkin.com/IWCatProductPage.process?Merchant_Id=&Section_Id=200406&pcount=&Product_Id=122161)

Thanks,

Jason.

netmask 07-14-2004 01:45 PM

I *think* /var/log/message may output something when a card is inserted,

MS3FGX 07-14-2004 03:36 PM

If you do:

Code:

tail -f /var/log/messages
A message should go by when you plug in a card.

logularjason 07-14-2004 05:48 PM

Nothing appears in my log, which is set up to record *.info and above. Seems like an unfortunate way to determine whether a card is in the reader anyway. Is there something analogous to hotplug that will allow a script to be called on card insertion/removal, or perhaps a flag that shows up somewhere under /proc?

MS3FGX 07-14-2004 10:06 PM

Your flash disk would show up as a SCSI device under /proc.

logularjason 07-14-2004 10:57 PM

Sure, but the SCSI device represents the USB card *reader*. I want to know if a card is in the reader...

(and before someone suggests a hook in /etc/hotplug/usb.agent, it's the same deal there too - it notifies when a USB device is attached/removed, however that's not what I'm after).

logularjason 07-17-2004 06:18 AM

My initial query remains unresolved, so I'll try and rephrase it:

I have a USB flash card reader as described above. Is there a way for me to detect when a flash card has been inserted into it? (Note that I do *not* want to detect whether the reader itself is connected to the PC - I know how to do that).

Thanks, Jason.

Electro 07-17-2004 03:47 PM

Here is a few links you can get hints from to make a script to detect if there is a card in one of the reader slots
http://www.cs.sfu.ca/~ggbaker/personal/cf-linux
http://www.linux-sxs.org/hardware/flashreaders.html

logularjason 07-17-2004 09:43 PM

The links above mainly concentrate on how to identify the right SCSI device and mount it.

I worked it out for myself in the end. The SCSI kernel driver returns a "no medium" code when an open() call is made and removable media (e.g. flash card) is not present. So all I need to do is make an open() call, and see what the return code is. I want to work at the bash level, so rather than writing c code to do this, I just used sg_dd from the sg3_utils suite, which does calls open - I grep out the 'no media' error. Of course, this works whether the reader has been mounted or not.

kasimtasdemir 06-24-2006 03:11 AM

Hi,
I am a newbie in Linux. I want to detect Usb flash drive in C whether polling or not. I thing I found a solution in this message:
Quote:

Originally Posted by logularjason
...I worked it out for myself in the end. The SCSI kernel driver returns a "no medium" code when an open() call is made and removable media (e.g. flash card) is not present. So all I need to do is make an open() call, and see what the return code is.

However I did not understand that how can I call "open()" in C in order to see the existance of USB flash drive? Could you give an samle code block,please? Thanks.


All times are GMT -5. The time now is 11:05 PM.