linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Figuring out devnodes from a usb device
@ 2010-08-01 19:58 Felipe Balbi
  2010-08-02 22:29 ` Greg KH
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Felipe Balbi @ 2010-08-01 19:58 UTC (permalink / raw)
  To: linux-hotplug

[-- Attachment #1: Type: text/plain, Size: 576 bytes --]

Hi all,

I'm trying to figure out how I could find all devnodes related to a USB
device. For example, if I attach a usb mass storage device I want to
figure out which /dev/sdXX I'm supposed to use when trying to read/write
to that particular device. Similarly for ACM, Network and all other devices.

Is there any way to achieve that with libudev ? I tried using
udev_device_get_devlinks_list_entry() but that didn't help.

I'm attaching my current code which is a modifed version of a tutorial
from Alan Ott available at [1].

[1] http://www.signal11.us/oss/udev/

-- 
balbi

[-- Attachment #2: udev.c --]
[-- Type: text/x-c, Size: 1765 bytes --]

#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>

int main (void)
{
	struct udev *udev;
	struct udev_enumerate *enumerate;
	struct udev_list_entry *devices, *dev_list_entry;
	struct udev_device *dev;

	/* Create the udev object */
	udev = udev_new();
	if (!udev) {
		printf("Can't create udev\n");
		exit(1);
	}

	/* Create a list of the devices in the 'hidraw' subsystem. */
	enumerate = udev_enumerate_new(udev);
	udev_enumerate_add_match_subsystem(enumerate, "usb");
	udev_enumerate_scan_devices(enumerate);
	devices = udev_enumerate_get_list_entry(enumerate);

	/* For each item enumerated, print out its information.
	 * udev_list_entry_foreach is a macro which expands to
	 * a loop. The loop will be executed for each member in
	 * devices, setting dev_list_entry to a list entry
	 * which contains the device's path in /sys. */
	udev_list_entry_foreach(dev_list_entry, devices) {
		struct udev_list_entry		*links, *more_devs;
		const char *path;
		unsigned count = 0;

		/* Get the filename of the /sys entry for the device
		 * and create a udev_device object (dev) representing it */
		path = udev_list_entry_get_name(dev_list_entry);
		dev = udev_device_new_from_syspath(udev, path);
		links = udev_device_get_devlinks_list_entry(dev);

		udev_list_entry_foreach(more_devs, links) {
			struct udev_device	*other_dev;
			const char		*path;

			printf("dev #%d\n", count);
			count++;

			path = udev_list_entry_get_name(links);

			other_dev = udev_device_new_from_syspath(udev, path);

			printf("Device node path: %s\n", udev_device_get_devnode(other_dev));
		}

		udev_device_unref(dev);
	}

	/* Free the enumerator object */
	udev_enumerate_unref(enumerate);
	udev_unref(udev);

	return 0;       
}


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-08-04  5:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-01 19:58 Figuring out devnodes from a usb device Felipe Balbi
2010-08-02 22:29 ` Greg KH
2010-08-03  7:28 ` Felipe Balbi
2010-08-03  9:13 ` Kay Sievers
2010-08-03 19:53 ` Greg KH
2010-08-03 20:20 ` Felipe Balbi
2010-08-03 20:32 ` Greg KH
2010-08-04  5:09 ` Felipe Balbi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).