LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2014, 07:51 AM   #1
answerme
Member
 
Registered: Apr 2008
Posts: 97

Rep: Reputation: 24
Detecting usb device type


Hi All

If I connect 1 USB PEN DRIVE in PC ,in /dev folder I recognize that particular device by /dev/sdb1. Then if I connect USB MOUSE ,it gets recognize by /dev/hidraw0.
How can I make one linux application which should be generic for all USB devices .I meant whichever USB device I connect I can fetch that device name(for example /dev/sdb1 In case of USB pendrive) on my applciation .Is there any API available
I have written one small application but it is hardcoded for USB ,It should be genric for all USB devices.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
main()
{
	int fd,fd1,led_no;
	fd = open ("/dev/hidraw0",O_RDWR); 
	if (fd <= 0) {
	 		  printf ("USB Port not opened %d\n",fd);
			//  exit(0);
	 }
	else
	 printf("USB_OPEN %d \n",fd);
	
	fd1 = open ("/dev/sdb1",O_RDWR);
	if (fd1 <= 0) {
	 		  printf ("USB port not opened %d\n",fd1);
			  //exit(0);
	 }
	else
	   printf("USB_OPEN %x\n ",fd1);
}

Last edited by answerme; 06-16-2014 at 08:08 AM.
 
Old 06-16-2014, 08:26 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
not sure but maybe /var/log/messages will give you a hint.
 
Old 06-16-2014, 11:30 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
All that info is under /sys/bus/usb/device
 
Old 06-16-2014, 03:38 PM   #4
briselec
Member
 
Registered: Jun 2013
Location: Ipswich, Australia
Distribution: Slackware
Posts: 74

Rep: Reputation: Disabled
You could use libusb_get_device_list() from libusb
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting a USB device which is write protected shedevil Linux - Newbie 2 09-25-2013 08:33 AM
usb device not detecting pieterparker17 Linux - Newbie 1 05-03-2011 06:05 AM
Best method for programmatically detecting a USB device? R00ts Linux - General 2 12-15-2010 09:36 AM
snd-usb-audio not detecting my device ekoome Linux - Kernel 1 07-15-2007 01:19 PM
Problem in Detecting USB Mass storage device LN85 Linux - Kernel 5 07-03-2007 11:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration