From patchwork Tue Aug 8 12:52:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 18932 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 6970DC32B0 for ; Tue, 8 Aug 2023 12:52:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 958BB627F2; Tue, 8 Aug 2023 14:52:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1691499176; bh=36EIHG1L7eU/PrrksaiwvW43iuhdztySZBLII9guvtE=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=sBGXxXi556sPKUtNQ+/U0lKqXWAsrwwANT9SOp0tBlALLKxfQG87Fky0/TmMmemah 6Sxhya+FY9NiPDpNwxxRbZuFCOIY8iWWqqcBSCS6NNqNW+QJdT4VdvpBaLtd/ui+DH lJlo4NpwrZ9PoD8CvHe86fkJy7NEc+MmUvRElxmf2V9SXbsnqkY/lbIoVxyCrtBkF3 mCKNGbm14oQjJhovnYmn+hbpZDMCMr9ZDfSkCjXWggkVFQ4hbuh2IvGkxIX1BM2JOG ebQ6pminU7YTN7VScVAmH909KttRJ3gdCjty/FaxrBlbEuq6rN+JFi2+dCgfcaFmEv +gDZHl99ionbg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4563B627F2 for ; Tue, 8 Aug 2023 14:52:49 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Q+S9blwO"; dkim-atps=neutral Received: from uno.localdomain (mob-5-90-60-22.net.vodafone.it [5.90.60.22]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 657238611; Tue, 8 Aug 2023 14:51:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1691499101; bh=36EIHG1L7eU/PrrksaiwvW43iuhdztySZBLII9guvtE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q+S9blwOPvSwl/dbB9Dk/CmzvAbX403QGcF9xdzxGlMNWXxubN092ZiGwiHQPB0Zw bdOAiqfVDS+4AQPWhjSTXhLZYpGpMHJiYqqO+L4rm1RpPYjTCspmkEkigAlOkrFzff gMF1CoQGxD3XY2DrSO5LgYJn4OF9WLWatyeaF1Mk= To: libcamera-devel@lists.libcamera.org, Sophie Friedrich 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 Subject: [libcamera-devel] [RFC 5/9] libcamera: device_enumerator_udev: Enumerate USB devices X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jacopo Mondi via libcamera-devel From: Jacopo Mondi Reply-To: Jacopo Mondi Cc: Jacopo Mondi Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add experimental support for USB device enumeration in the udev-based device enumerator. Signed-off-by: Jacopo Mondi --- .../libcamera/internal/device_enumerator.h | 5 +++ .../internal/device_enumerator_udev.h | 1 + src/libcamera/device_enumerator.cpp | 21 ++++++++++++ src/libcamera/device_enumerator_udev.cpp | 32 +++++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h index 8df6a3e2ac92..1d6dbcb104b8 100644 --- a/include/libcamera/internal/device_enumerator.h +++ b/include/libcamera/internal/device_enumerator.h @@ -18,6 +18,7 @@ namespace libcamera { class MediaDevice; +class USBDevice; class DeviceEnumerator { @@ -38,8 +39,12 @@ protected: void addMediaDevice(std::unique_ptr media); void removeMediaDevice(const std::string &deviceNode); + void addUSBDevice(std::unique_ptr usb); + /* \todo implement remove() */ + private: std::vector> mediaDevices_; + std::vector> usbDevices_; }; } /* namespace libcamera */ diff --git a/include/libcamera/internal/device_enumerator_udev.h b/include/libcamera/internal/device_enumerator_udev.h index 1b3360df31ba..e67ee31e0376 100644 --- a/include/libcamera/internal/device_enumerator_udev.h +++ b/include/libcamera/internal/device_enumerator_udev.h @@ -59,6 +59,7 @@ private: std::string lookupDeviceNode(dev_t devnum); int addV4L2Device(dev_t devnum); + int createUSBDevice(struct udev_device *dev); void udevNotify(); struct udev *udev_; diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index 2ab731c68db9..1fde7367cfa8 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -7,6 +7,7 @@ #include "libcamera/internal/device_enumerator.h" +#include #include #include @@ -14,6 +15,7 @@ #include "libcamera/internal/device_enumerator_sysfs.h" #include "libcamera/internal/device_enumerator_udev.h" #include "libcamera/internal/media_device.h" +#include "libcamera/internal/usb_device.h" /** * \file device_enumerator.h @@ -189,6 +191,25 @@ void DeviceEnumerator::addMediaDevice(std::unique_ptr media) devicesAdded.emit(); } +void DeviceEnumerator::addUSBDevice(std::unique_ptr usb) +{ + /* + * This is a bit of an hack and could be improved! + * + * Can't use std::sort() + std::unique() because we're storing + * unique_ptr<> + */ + for (const auto &dev : usbDevices_) { + if (dev->pid() == usb->pid() && dev->vid() == usb->vid()) + return; + } + + LOG(DeviceEnumerator, Debug) + << "Added USB device " << usb->vid() << "-" << usb->pid(); + + usbDevices_.push_back(std::move(usb)); +} + /** * \brief Remove a media device from the enumerator * \param[in] deviceNode Path to the media device to remove diff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp index 3cb0044a9434..ce4543698ed4 100644 --- a/src/libcamera/device_enumerator_udev.cpp +++ b/src/libcamera/device_enumerator_udev.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,7 @@ #include #include "libcamera/internal/media_device.h" +#include "libcamera/internal/usb_device.h" namespace libcamera { @@ -115,6 +117,9 @@ int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev) return 0; } + if (!strcmp(subsystem, "input")) + return createUSBDevice(dev); + return -ENODEV; } @@ -136,6 +141,14 @@ int DeviceEnumeratorUdev::enumerate() if (ret < 0) goto done; + /* + * FIXME: this should use the appoprtiate subsystem for USB cameras. + * As a test, match on USB input devices. + */ + ret = udev_enumerate_add_match_subsystem(udev_enum, "input"); + if (ret < 0) + goto done; + ret = udev_enumerate_add_match_is_initialized(udev_enum); if (ret < 0) goto done; @@ -329,6 +342,25 @@ int DeviceEnumeratorUdev::addV4L2Device(dev_t devnum) return 0; } +int DeviceEnumeratorUdev::createUSBDevice(struct udev_device *dev) +{ + /* Get the USB parent device to get VID/PID information. */ + struct udev_device *usb_device = + udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device"); + if (!usb_device) + return -ENODEV; + + const char *vid = udev_device_get_sysattr_value(usb_device, "idVendor"); + const char *pid = udev_device_get_sysattr_value(usb_device, "idProduct"); + if (!vid || !pid) + return -ENODEV; + + std::unique_ptr usbDev = std::make_unique(vid, pid); + addUSBDevice(std::move(usbDev)); + + return 0; +} + void DeviceEnumeratorUdev::udevNotify() { struct udev_device *dev = udev_monitor_receive_device(monitor_);