How to detect if /dev/* is a USB device?

How to detect if /dev/* is a USB device?, 9.5 out of 10 based on 2 ratings
VN:F [1.9.22_1171]
Rating: 9.5/10 (2 votes cast)

I spend a lot of time in finding out how to detect if a device is a USB device. The reason behind is that not all USB sticks tell that they are USB sticks. By using the debugger I discovered that most of the new USB sticks identify themselves as SCSI device.

So this leads us to the problem.

How to find out if /dev/* is a USB device or internal hardware?

What I first tried was:
SG_EMULATED_HOST
“SG_EMULATED_HOST 0×2203. Assumes 3rd argument points to an int and outputs a flag indicating whether the host (adapter) is connected to a “real” SCSI bus or is an emulated one (e.g. ide-scsi or usb storage device driver). A value of 1 means emulated while 0 is not. [To check: is IEEE1394 a "real" SCSI serial bus?]”

But it didn’t sending the ioctl returned always ZERO, even if the device was connected via USB.

The only reliable way seems to be using HAL (Hardware Abstraction Layer).
In my case my snippet is in C++ / C by using QT IDE. But should be easy to get it working.

If you need help may write a comment.

bool DevDetect::IsUsb(){
// is called to often! Limit it into SCSI DEVICES!
//qDebug() << “IS USB entered with “<<this->myDevPathIN;

// hal check:
HalManager *hal = new HalManager(“org.freedesktop.Hal”, “/org/freedesktop/Hal/Manager”, QDBusConnection::systemBus());
RaiiDelete<HalManager *> raiiDeleteHal(&hal);
if (hal->isValid() == false)
return false;

QDBusPendingReply<QStringList> device2check = hal->FindDeviceStringMatch(“block.device”,this->myDevPathIN); //nDevName
device2check.waitForFinished();
if (device2check.isError() == false)
{
// it returns only one result so for could be removed
for (int iUdi = 0; iUdi < device2check.value().size(); iUdi++) // not needed will be only one return!
{
HalDevice *dev = new HalDevice(“org.freedesktop.Hal”, device2check.value().at(iUdi), QDBusConnection::systemBus());
RaiiDelete<HalDevice *> raiiDeleteDev(&dev);
if (dev->isValid() == false)
continue;
QDBusPendingReply<QVariantMap> devProps = dev->GetAllProperties();
devProps.waitForFinished();
if (devProps.isError() == false)
{
if(devProps.value().value(“storage.bus”).toString().toUpper() == “USB”){

return true;
}
}
}
}

return false;
}

bool DevDetect::IsUsb(){
// is called to often! Limit it into SCSI DEVICES!
//qDebug() << “IS USB entered with “<<this->mDevPath;

// hal check:
HalManager *hal = new HalManager(“org.freedesktop.Hal”, “/org/freedesktop/Hal/Manager”, QDBusConnection::systemBus());
RaiiDelete<HalManager *> raiiDeleteHal(&hal);
if (hal->isValid() == false)
return false;

QDBusPendingReply<QStringList> device2check = hal->FindDeviceStringMatch(“block.device”,this->mDevPath); //nDevName
device2check.waitForFinished();
if (device2check.isError() == false)
{
// it returns only one result so for could be removed
for (int iUdi = 0; iUdi < device2check.value().size(); iUdi++) // not needed will be only one return!
{
HalDevice *dev = new HalDevice(“org.freedesktop.Hal”, device2check.value().at(iUdi), QDBusConnection::systemBus());
RaiiDelete<HalDevice *> raiiDeleteDev(&dev);
if (dev->isValid() == false)
continue;
QDBusPendingReply<QVariantMap> devProps = dev->GetAllProperties();
devProps.waitForFinished();
if (devProps.isError() == false)
{
if(devProps.value().value(“storage.bus”).toString().toUpper() == “USB”){

return true;
}
}
}
}

return false;
}

Big thanks to:
SolProg
Martin Vidovic
http://www.solprog.com/

He provided the most important parts of HAL integration into QT.
Thanks

This entry was posted in C++ General. Bookmark the permalink.

One Response to How to detect if /dev/* is a USB device?

  1. Pharme465 says:

    Hello! edgcccc interesting edgcccc site!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>