All of lore.kernel.org
 help / color / mirror / Atom feed
From: Felipe Balbi <me@felipebalbi.com>
To: linux-hotplug@vger.kernel.org
Subject: Figuring out devnodes from a usb device
Date: Sun, 01 Aug 2010 19:58:07 +0000	[thread overview]
Message-ID: <4C55D1CF.5090206@felipebalbi.com> (raw)

[-- 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;       
}


             reply	other threads:[~2010-08-01 19:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-01 19:58 Felipe Balbi [this message]
2010-08-02 22:29 ` Figuring out devnodes from a usb device 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C55D1CF.5090206@felipebalbi.com \
    --to=me@felipebalbi.com \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.