From patchwork Fri Nov 21 16:14:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacek Anaszewski X-Patchwork-Id: 5356091 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 25FCAC11AC for ; Fri, 21 Nov 2014 16:15:55 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3604F201B4 for ; Fri, 21 Nov 2014 16:15:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E77F120176 for ; Fri, 21 Nov 2014 16:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932590AbaKUQPQ (ORCPT ); Fri, 21 Nov 2014 11:15:16 -0500 Received: from mailout1.samsung.com ([203.254.224.24]:31429 "EHLO mailout1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932288AbaKUQPN (ORCPT ); Fri, 21 Nov 2014 11:15:13 -0500 Received: from epcpsbgm2.samsung.com (epcpsbgm2 [203.254.230.27]) by mailout1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NFE00GWCD5CL030@mailout1.samsung.com> for linux-media@vger.kernel.org; Sat, 22 Nov 2014 01:15:12 +0900 (KST) X-AuditID: cbfee61b-f79d76d0000024d6-00-546f65108aed Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id BC.EE.09430.0156F645; Sat, 22 Nov 2014 01:15:12 +0900 (KST) Received: from AMDC2362.DIGITAL.local ([106.120.53.23]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0NFE00IXRD4IRL40@mmp1.samsung.com>; Sat, 22 Nov 2014 01:15:12 +0900 (KST) From: Jacek Anaszewski To: linux-media@vger.kernel.org Cc: m.chehab@samsung.com, gjasny@googlemail.com, hdegoede@redhat.com, hans.verkuil@cisco.com, b.zolnierkie@samsung.com, kyungmin.park@samsung.com, sakari.ailus@linux.intel.com, laurent.pinchart@ideasonboard.com, Jacek Anaszewski Subject: [PATCH/RFC v4 06/11] mediactl: Add media_device creation helpers Date: Fri, 21 Nov 2014 17:14:35 +0100 Message-id: <1416586480-19982-7-git-send-email-j.anaszewski@samsung.com> X-Mailer: git-send-email 1.7.9.5 In-reply-to: <1416586480-19982-1-git-send-email-j.anaszewski@samsung.com> References: <1416586480-19982-1-git-send-email-j.anaszewski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrFLMWRmVeSWpSXmKPExsVy+t9jAV2B1PwQg41nVSw2zljPajF9yixG iyU/dzFZvDk+ncmi9+pzRouzTW/YLTonLmG36NmwldXi4jp5i09bvjE5cHlM+b2R1ePphMns HrM7ZrJ6zDsZ6PF+31U2j74tqxg9Pm+SC2CP4rJJSc3JLEst0rdL4Mo4tNy54KVKRVv/dPYG xv+yXYycHBICJhLv15xlgbDFJC7cW8/WxcjFISSwiFGiu7WVGcJpZ5K4824RI0gVm4ChxM8X r5lAbBEBeYknvTfAOpgFWpkkpjU9YAdJCAt4SkxY8wtsLIuAqsSswwfB4rwCHhLXzzUCDeIA WqcgMWeSDUiYE6h887V7YCVCQCX35mxnm8DIu4CRYRWjaGpBckFxUnqukV5xYm5xaV66XnJ+ 7iZGcAg+k97BuKrB4hCjAAejEg/vh+l5IUKsiWXFlbmHGCU4mJVEeMuF80OEeFMSK6tSi/Lj i0pzUosPMUpzsCiJ8964mRsiJJCeWJKanZpakFoEk2Xi4JRqYBS62PCn+W3yDE4zhg8ulR1n WB3+PjkiOu9ReIBiSndA5ayFWz7NSmeLrplklJB4frWdV86fbc5+Fhfzrv9btXfrxsZvlVPF rENO5R5ee83dlaVN6kKUSqWk1ZtolqyG7Y7GHvNjVs9sPaR1YPvK8uyJ2kVnj/It0nfit7dn YjQvTMh2i3twVomlOCPRUIu5qDgRAFiW2sI9AgAA Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add helper functions that allow for easy instantiation of media_device object basing on whether the media device contains video device with given node name. Signed-off-by: Jacek Anaszewski Acked-by: Kyungmin Park --- utils/media-ctl/libmediactl.c | 75 +++++++++++++++++++++++++++++++++++++++++ utils/media-ctl/mediactl.h | 29 ++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/utils/media-ctl/libmediactl.c b/utils/media-ctl/libmediactl.c index a476601..9c81711 100644 --- a/utils/media-ctl/libmediactl.c +++ b/utils/media-ctl/libmediactl.c @@ -853,6 +853,43 @@ struct media_device *media_device_new(const char *devnode) return media; } +struct media_device *media_device_new_by_entity_devname(char *entity_devname) +{ + struct media_device *media; + char media_devname[32]; + int i, ret; + + if (entity_devname == NULL) + return NULL; + + /* query all available media devices */ + for (i = 0;; ++i) { + sprintf(media_devname, "/dev/media%d", i); + + media = media_device_new(media_devname); + if (media == NULL) + return NULL; + + ret = media_device_enumerate(media); + if (ret < 0) { + media_dbg(media, "Failed to enumerate %s (%d)\n", media_devname, ret); + goto err_dev_enum; + } + + /* Check if the media device contains entity with entity_devname */ + if (media_get_entity_by_devname(media, entity_devname, strlen(entity_devname))) + return media; + + if (media) + media_device_unref(media); + } + +err_dev_enum: + if (media) + media_device_unref(media); + return NULL; +} + struct media_device *media_device_new_emulated(struct media_device_info *info) { struct media_device *media; @@ -896,6 +933,44 @@ void media_device_unref(struct media_device *media) free(media); } +int media_get_devname_by_fd(int fd, char *node_name) +{ + struct udev *udev; + struct media_entity tmp_entity; + struct stat stat; + int ret; + + if (node_name == NULL) + return -EINVAL; + + ret = fstat(fd, &stat); + if (ret < 0) + return -EINVAL; + + tmp_entity.info.v4l.major = MAJOR(stat.st_rdev); + tmp_entity.info.v4l.minor = MINOR(stat.st_rdev); + + ret = media_udev_open(&udev); + if (ret < 0) + printf("Can't get udev context\n"); + + /* Try to get the device name via udev */ + ret = media_get_devname_udev(udev, &tmp_entity); + if (!ret) + goto out; + + ret = media_get_devname_sysfs(&tmp_entity); + if (ret < 0) + goto err_get_devname; + +out: + strcpy(node_name, tmp_entity.devname); +err_get_devname: + media_udev_close(udev); + return ret; +} + + int media_device_add_entity(struct media_device *media, const struct media_entity_desc *desc, const char *devnode) diff --git a/utils/media-ctl/mediactl.h b/utils/media-ctl/mediactl.h index 18a1e0e..7f16097 100644 --- a/utils/media-ctl/mediactl.h +++ b/utils/media-ctl/mediactl.h @@ -77,6 +77,23 @@ struct media_device *media_device_new(const char *devnode); struct media_device *media_device_new_emulated(struct media_device_info *info); /** + * @brief Create a new media device if it comprises entity with entity_devname + * @param entity_devname - device node name of the entity to be matched + * + * Query all media devices available in the system to find the one comprising + * the entity with device node name equal to entity_devname. If the media + * device is matched then its instance is created and initialized with + * enumerated entities and links. The returned device can be accessed. + * + * Media devices are reference-counted, see media_device_ref() and + * media_device_unref() for more information. + * + * @return A pointer to the new media device or NULL if video_devname cannot + * be matched or memory cannot be allocated. + */ +struct media_device *media_device_new_by_entity_devname(char *video_devname); + +/** * @brief Take a reference to the device. * @param media - device instance. * @@ -242,6 +259,18 @@ const char *media_entity_get_devname(struct media_entity *entity); const char *media_entity_get_name(struct media_entity *entity); /** + * @brief Get the device node name by its file descriptor + * @param fd - file descriptor of a device + * @param node_name - output device node name string + * + * This function returns the full path and name to the device node corresponding + * to the given file descriptor. + * + * @return 0 on success, or a negative error code on failure. + */ +int media_get_devname_by_fd(int fd, char *node_name); + +/** * @brief Get the type of an entity. * @param entity - the entity. *