{"id":18932,"url":"https://patchwork.libcamera.org/api/1.1/patches/18932/?format=json","web_url":"https://patchwork.libcamera.org/patch/18932/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20230808125228.29043-6-jacopo.mondi@ideasonboard.com>","date":"2023-08-08T12:52:24","name":"[libcamera-devel,RFC,5/9] libcamera: device_enumerator_udev: Enumerate USB devices","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"0ca18ec2b3aec7468a7b6fed059d9e1dd9237028","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/1.1/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18932/mbox/","series":[{"id":4002,"url":"https://patchwork.libcamera.org/api/1.1/series/4002/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4002","date":"2023-08-08T12:52:19","name":"libcamera: Generalize device match and support USB devices","version":1,"mbox":"https://patchwork.libcamera.org/series/4002/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18932/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18932/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 6970DC32B0\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  8 Aug 2023 12:52:57 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 958BB627F2;\n\tTue,  8 Aug 2023 14:52:56 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4563B627F2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Aug 2023 14:52:49 +0200 (CEST)","from uno.localdomain (mob-5-90-60-22.net.vodafone.it [5.90.60.22])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 657238611; \n\tTue,  8 Aug 2023 14:51:41 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1691499176;\n\tbh=36EIHG1L7eU/PrrksaiwvW43iuhdztySZBLII9guvtE=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=sBGXxXi556sPKUtNQ+/U0lKqXWAsrwwANT9SOp0tBlALLKxfQG87Fky0/TmMmemah\n\t6Sxhya+FY9NiPDpNwxxRbZuFCOIY8iWWqqcBSCS6NNqNW+QJdT4VdvpBaLtd/ui+DH\n\tlJlo4NpwrZ9PoD8CvHe86fkJy7NEc+MmUvRElxmf2V9SXbsnqkY/lbIoVxyCrtBkF3\n\tmCKNGbm14oQjJhovnYmn+hbpZDMCMr9ZDfSkCjXWggkVFQ4hbuh2IvGkxIX1BM2JOG\n\tebQ6pminU7YTN7VScVAmH909KttRJ3gdCjty/FaxrBlbEuq6rN+JFi2+dCgfcaFmEv\n\t+gDZHl99ionbg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1691499101;\n\tbh=36EIHG1L7eU/PrrksaiwvW43iuhdztySZBLII9guvtE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Q+S9blwOPvSwl/dbB9Dk/CmzvAbX403QGcF9xdzxGlMNWXxubN092ZiGwiHQPB0Zw\n\tbdOAiqfVDS+4AQPWhjSTXhLZYpGpMHJiYqqO+L4rm1RpPYjTCspmkEkigAlOkrFzff\n\tgMF1CoQGxD3XY2DrSO5LgYJn4OF9WLWatyeaF1Mk="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Q+S9blwO\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org, Sophie Friedrich <dev@flowerpot.me>","Date":"Tue,  8 Aug 2023 14:52:24 +0200","Message-Id":"<20230808125228.29043-6-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.40.1","In-Reply-To":"<20230808125228.29043-1-jacopo.mondi@ideasonboard.com>","References":"<20230808125228.29043-1-jacopo.mondi@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC 5/9] libcamera: device_enumerator_udev:\n\tEnumerate USB devices","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add experimental support for USB device enumeration in the udev-based\ndevice enumerator.\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n .../libcamera/internal/device_enumerator.h    |  5 +++\n .../internal/device_enumerator_udev.h         |  1 +\n src/libcamera/device_enumerator.cpp           | 21 ++++++++++++\n src/libcamera/device_enumerator_udev.cpp      | 32 +++++++++++++++++++\n 4 files changed, 59 insertions(+)","diff":"diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h\nindex 8df6a3e2ac92..1d6dbcb104b8 100644\n--- a/include/libcamera/internal/device_enumerator.h\n+++ b/include/libcamera/internal/device_enumerator.h\n@@ -18,6 +18,7 @@\n namespace libcamera {\n \n class MediaDevice;\n+class USBDevice;\n \n class DeviceEnumerator\n {\n@@ -38,8 +39,12 @@ protected:\n \tvoid addMediaDevice(std::unique_ptr<MediaDevice> media);\n \tvoid removeMediaDevice(const std::string &deviceNode);\n \n+\tvoid addUSBDevice(std::unique_ptr<USBDevice> usb);\n+\t/* \\todo implement remove() */\n+\n private:\n \tstd::vector<std::shared_ptr<MediaDevice>> mediaDevices_;\n+\tstd::vector<std::shared_ptr<USBDevice>> usbDevices_;\n };\n \n } /* namespace libcamera */\ndiff --git a/include/libcamera/internal/device_enumerator_udev.h b/include/libcamera/internal/device_enumerator_udev.h\nindex 1b3360df31ba..e67ee31e0376 100644\n--- a/include/libcamera/internal/device_enumerator_udev.h\n+++ b/include/libcamera/internal/device_enumerator_udev.h\n@@ -59,6 +59,7 @@ private:\n \tstd::string lookupDeviceNode(dev_t devnum);\n \n \tint addV4L2Device(dev_t devnum);\n+\tint createUSBDevice(struct udev_device *dev);\n \tvoid udevNotify();\n \n \tstruct udev *udev_;\ndiff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\nindex 2ab731c68db9..1fde7367cfa8 100644\n--- a/src/libcamera/device_enumerator.cpp\n+++ b/src/libcamera/device_enumerator.cpp\n@@ -7,6 +7,7 @@\n \n #include \"libcamera/internal/device_enumerator.h\"\n \n+#include <algorithm>\n #include <string.h>\n \n #include <libcamera/base/log.h>\n@@ -14,6 +15,7 @@\n #include \"libcamera/internal/device_enumerator_sysfs.h\"\n #include \"libcamera/internal/device_enumerator_udev.h\"\n #include \"libcamera/internal/media_device.h\"\n+#include \"libcamera/internal/usb_device.h\"\n \n /**\n  * \\file device_enumerator.h\n@@ -189,6 +191,25 @@ void DeviceEnumerator::addMediaDevice(std::unique_ptr<MediaDevice> media)\n \tdevicesAdded.emit();\n }\n \n+void DeviceEnumerator::addUSBDevice(std::unique_ptr<USBDevice> usb)\n+{\n+\t/*\n+\t * This is a bit of an hack and could be improved!\n+\t *\n+\t * Can't use std::sort() + std::unique() because we're storing\n+\t * unique_ptr<>\n+\t */\n+\tfor (const auto &dev : usbDevices_) {\n+\t\tif (dev->pid() == usb->pid() && dev->vid() == usb->vid())\n+\t\t\treturn;\n+\t}\n+\n+\tLOG(DeviceEnumerator, Debug)\n+\t\t<< \"Added USB device \" << usb->vid() << \"-\" << usb->pid();\n+\n+\tusbDevices_.push_back(std::move(usb));\n+}\n+\n /**\n  * \\brief Remove a media device from the enumerator\n  * \\param[in] deviceNode Path to the media device to remove\ndiff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp\nindex 3cb0044a9434..ce4543698ed4 100644\n--- a/src/libcamera/device_enumerator_udev.cpp\n+++ b/src/libcamera/device_enumerator_udev.cpp\n@@ -12,6 +12,7 @@\n #include <libudev.h>\n #include <list>\n #include <map>\n+#include <memory>\n #include <string.h>\n #include <string_view>\n #include <sys/ioctl.h>\n@@ -22,6 +23,7 @@\n #include <libcamera/base/log.h>\n \n #include \"libcamera/internal/media_device.h\"\n+#include \"libcamera/internal/usb_device.h\"\n \n namespace libcamera {\n \n@@ -115,6 +117,9 @@ int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev)\n \t\treturn 0;\n \t}\n \n+\tif (!strcmp(subsystem, \"input\"))\n+\t\treturn createUSBDevice(dev);\n+\n \treturn -ENODEV;\n }\n \n@@ -136,6 +141,14 @@ int DeviceEnumeratorUdev::enumerate()\n \tif (ret < 0)\n \t\tgoto done;\n \n+\t/*\n+\t * FIXME: this should use the appoprtiate subsystem for USB cameras.\n+\t * As a test, match on USB input devices.\n+\t */\n+\tret = udev_enumerate_add_match_subsystem(udev_enum, \"input\");\n+\tif (ret < 0)\n+\t\tgoto done;\n+\n \tret = udev_enumerate_add_match_is_initialized(udev_enum);\n \tif (ret < 0)\n \t\tgoto done;\n@@ -329,6 +342,25 @@ int DeviceEnumeratorUdev::addV4L2Device(dev_t devnum)\n \treturn 0;\n }\n \n+int DeviceEnumeratorUdev::createUSBDevice(struct udev_device *dev)\n+{\n+\t/* Get the USB parent device to get VID/PID information. */\n+\tstruct udev_device *usb_device =\n+\t\tudev_device_get_parent_with_subsystem_devtype(dev, \"usb\", \"usb_device\");\n+\tif (!usb_device)\n+\t\treturn -ENODEV;\n+\n+\tconst char *vid = udev_device_get_sysattr_value(usb_device, \"idVendor\");\n+\tconst char *pid = udev_device_get_sysattr_value(usb_device, \"idProduct\");\n+\tif (!vid || !pid)\n+\t\treturn -ENODEV;\n+\n+\tstd::unique_ptr<USBDevice> usbDev = std::make_unique<USBDevice>(vid, pid);\n+\taddUSBDevice(std::move(usbDev));\n+\n+\treturn 0;\n+}\n+\n void DeviceEnumeratorUdev::udevNotify()\n {\n \tstruct udev_device *dev = udev_monitor_receive_device(monitor_);\n","prefixes":["libcamera-devel","RFC","5/9"]}