linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Antonio Ospite <ao2@ao2.it>
To: linux-bluetooth@vger.kernel.org
Cc: Antonio Ospite <ao2@ao2.it>, Bastien Nocera <hadess@hadess.net>,
	Szymon Janc <szymon.janc@gmail.com>,
	Frank Praznik <frank.praznik@oh.rr.com>
Subject: [PATCH BlueZ 1/5] plugins/sixaxis: simplify get_js_numbers()
Date: Tue,  6 May 2014 12:06:12 +0200	[thread overview]
Message-ID: <1399370776-5027-2-git-send-email-ao2@ao2.it> (raw)
In-Reply-To: <1399370776-5027-1-git-send-email-ao2@ao2.it>

Use udev_enumerate_add_match_parent() to simplify get_js_number() a lot,
with this mechanism the old JOIN-like operation to find joystick nodes
is no longer necessary.

Also require libudev >= 172, this is where
udev_enumerate_add_match_parent() has been first introduced.

NOTE: using udev_enumerate_add_match_parent() results in a memory leak
when enumerating child devices, this has been fixed in udev 207; the
commit which fixes the issue is this one:
http://cgit.freedesktop.org/systemd/systemd/commit/?id=51cc07576e119dea6e65478eeba9472979fd0936
---
 configure.ac      |  4 +--
 plugins/sixaxis.c | 92 +++++++++++++++++++++++++++----------------------------
 2 files changed, 47 insertions(+), 49 deletions(-)

diff --git a/configure.ac b/configure.ac
index 54a387f..4208ad8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -125,8 +125,8 @@ AM_CONDITIONAL(MONITOR, test "${enable_monitor}" != "no")
 AC_ARG_ENABLE(udev, AC_HELP_STRING([--disable-udev],
 		[disable udev device support]), [enable_udev=${enableval}])
 if (test "${enable_tools}" != "no" && test "${enable_udev}" != "no"); then
-	PKG_CHECK_MODULES(UDEV, libudev >= 143, dummy=yes,
-				AC_MSG_ERROR(libudev >= 143 is required))
+	PKG_CHECK_MODULES(UDEV, libudev >= 172, dummy=yes,
+				AC_MSG_ERROR(libudev >= 172 is required))
 	AC_SUBST(UDEV_CFLAGS)
 	AC_SUBST(UDEV_LIBS)
 	AC_CHECK_LIB(udev, udev_hwdb_new,
diff --git a/plugins/sixaxis.c b/plugins/sixaxis.c
index 8045448..9db14ec 100644
--- a/plugins/sixaxis.c
+++ b/plugins/sixaxis.c
@@ -235,62 +235,60 @@ static bool setup_device(int fd, int index, struct btd_adapter *adapter)
 
 static int get_js_number(struct udev_device *udevice)
 {
-	struct udev_list_entry *devices, *dev_list_entry;
+	struct udev_list_entry *dev_list_entry;
 	struct udev_enumerate *enumerate;
 	struct udev_device *hid_parent;
-	const char *hidraw_node;
-	const char *hid_phys;
+	struct udev_device *transport_parent;
+	struct udev_device *js_dev;
+	const char *js_devname;
 	int number = 0;
 
-	hid_parent = udev_device_get_parent_with_subsystem_devtype(udevice,
-								"hid", NULL);
-
-	hid_phys = udev_device_get_property_value(hid_parent, "HID_PHYS");
-	hidraw_node = udev_device_get_devnode(udevice);
-	if (!hid_phys || !hidraw_node)
-		return 0;
+	/*
+	 * Go up by two levels from the hidraw device in order to support
+	 * older kernels, where the hierachy of HID devices is different than
+	 * newer kernels:
+	 *
+	 *   - on kernels < 3.14 the input device is child of the transport
+	 *     device (usbhid, hidp):
+	 *
+	 *       USB/BT
+	 *       |- hid
+	 *       |  `- hidraw *
+	 *       `- input
+	 *          `- js
+	 *
+	 *   - on kernels >= 3.14 the input device is child of the hid device
+	 *     itself, as it should be:
+	 *
+	 *       USB/BT
+	 *       `- hid
+	 *          |- hidraw *
+	 *          `- input
+	 *             `- js
+	 */
+	hid_parent = udev_device_get_parent(udevice);
+	transport_parent = udev_device_get_parent(hid_parent);
 
 	enumerate = udev_enumerate_new(udev_device_get_udev(udevice));
+	udev_enumerate_add_match_parent(enumerate, transport_parent);
 	udev_enumerate_add_match_sysname(enumerate, "js*");
 	udev_enumerate_scan_devices(enumerate);
-	devices = udev_enumerate_get_list_entry(enumerate);
-
-	udev_list_entry_foreach(dev_list_entry, devices) {
-		struct udev_device *input_parent;
-		struct udev_device *js_dev;
-		const char *input_phys;
-		const char *devname;
-
-		devname = udev_list_entry_get_name(dev_list_entry);
-		js_dev = udev_device_new_from_syspath(
-						udev_device_get_udev(udevice),
-						devname);
-
-		input_parent = udev_device_get_parent_with_subsystem_devtype(
-							js_dev, "input", NULL);
-		if (!input_parent)
-			goto next;
-
-		/* check if this is the joystick relative to the hidraw device
-		 * above */
-		input_phys = udev_device_get_sysattr_value(input_parent,
-									"phys");
-		if (!input_phys)
-			goto next;
-
-		if (!strcmp(input_phys, hid_phys)) {
-			number = atoi(udev_device_get_sysnum(js_dev));
-
-			/* joystick numbers start from 0, leds from 1 */
-			number++;
-
-			udev_device_unref(js_dev);
-			break;
-		}
-next:
-		udev_device_unref(js_dev);
-	}
 
+	/* get the first js* device, there should be only one anyway */
+	dev_list_entry = udev_enumerate_get_list_entry(enumerate);
+
+	if (dev_list_entry == NULL)
+		return number;
+
+	js_devname = udev_list_entry_get_name(dev_list_entry);
+	js_dev = udev_device_new_from_syspath(
+					udev_device_get_udev(udevice),
+					js_devname);
+
+	/* joystick numbers start from 0, leds from 1 */
+	number = atoi(udev_device_get_sysnum(js_dev)) + 1;
+
+	udev_device_unref(js_dev);
 	udev_enumerate_unref(enumerate);
 
 	return number;
-- 
2.0.0.rc0


  reply	other threads:[~2014-05-06 10:06 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-06 10:06 [PATCH BlueZ 0/5] plugin/sixaxis: use the sysfs leds class Antonio Ospite
2014-05-06 10:06 ` Antonio Ospite [this message]
2014-05-07 19:57   ` [PATCH BlueZ 1/5] plugins/sixaxis: simplify get_js_numbers() Szymon Janc
2014-05-06 10:06 ` [PATCH BlueZ 2/5] plugins/sixaxis: factor out a set_leds_hidraw() function Antonio Ospite
2014-05-07 19:58   ` Szymon Janc
2014-05-06 10:06 ` [PATCH BlueZ 3/5] plugins/sixaxis: factor out a calc_leds_bitmap() function Antonio Ospite
2014-05-07 19:59   ` Szymon Janc
2014-05-06 10:06 ` [PATCH BlueZ 4/5] plugins/sixaxis: add a get_leds_data() function Antonio Ospite
2014-05-07 20:04   ` Szymon Janc
2014-05-09 12:22   ` Johan Hedberg
2014-05-09 13:31     ` Antonio Ospite
2014-05-06 10:06 ` [PATCH BlueZ 5/5] plugins/sixaxis: add a set_leds_sysfs() function Antonio Ospite
2014-05-07 20:24   ` Szymon Janc
2014-05-14 21:40 ` [PATCHv2 BlueZ 0/5] plugin/sixaxis: Set leds using the sysfs leds class Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 2/5] plugins/sixaxis: Factor out a set_leds_hidraw() function Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 3/5] plugins/sixaxis: Factor out a calc_leds_bitmap() function Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 4/5] plugins/sixaxis: Add a get_leds_data() function Antonio Ospite
2014-05-15 20:25     ` Szymon Janc
2014-05-16  8:13       ` Antonio Ospite
2014-05-14 21:40   ` [PATCHv2 BlueZ 5/5] plugins/sixaxis: Add a set_leds_sysfs() function Antonio Ospite
2014-05-15 20:25     ` Szymon Janc
2014-05-15 15:28   ` [PATCHv2 BlueZ 0/5] plugin/sixaxis: Set leds using the sysfs leds class Frank Praznik
2014-05-15 16:33     ` Antonio Ospite
2014-05-16 12:37   ` Szymon Janc
2014-05-27 11:25   ` [PATCHv3 " Antonio Ospite
2014-05-27 11:25     ` [PATCHv3 BlueZ 4/5] plugins/sixaxis: Add a get_leds_data() function Antonio Ospite
2014-05-27 11:25     ` [PATCHv3 BlueZ 5/5] plugins/sixaxis: Add a set_leds_sysfs() function Antonio Ospite
2014-06-08 12:58     ` [PATCHv3 BlueZ 0/5] plugin/sixaxis: Set leds using the sysfs leds class Szymon Janc

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=1399370776-5027-2-git-send-email-ao2@ao2.it \
    --to=ao2@ao2.it \
    --cc=frank.praznik@oh.rr.com \
    --cc=hadess@hadess.net \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=szymon.janc@gmail.com \
    /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 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).