From patchwork Thu Aug 11 20:29:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 9275927 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F220360780 for ; Thu, 11 Aug 2016 20:30:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E0BF92874E for ; Thu, 11 Aug 2016 20:30:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D597728789; Thu, 11 Aug 2016 20:30:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 79E5A2874E for ; Thu, 11 Aug 2016 20:30:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752184AbcHKUaK (ORCPT ); Thu, 11 Aug 2016 16:30:10 -0400 Received: from mga02.intel.com ([134.134.136.20]:36703 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752100AbcHKUaK (ORCPT ); Thu, 11 Aug 2016 16:30:10 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP; 11 Aug 2016 13:30:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.28,507,1464678000"; d="scan'208";a="747510391" Received: from paasikivi.fi.intel.com ([10.237.72.42]) by FMSMGA003.fm.intel.com with ESMTP; 11 Aug 2016 13:30:05 -0700 Received: from nauris.fi.intel.com (nauris.localdomain [192.168.240.2]) by paasikivi.fi.intel.com (Postfix) with ESMTP id 3FDAC20ADD; Thu, 11 Aug 2016 23:30:04 +0300 (EEST) Received: by nauris.fi.intel.com (Postfix, from userid 1000) id B9043200E0; Thu, 11 Aug 2016 23:29:28 +0300 (EEST) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: hverkuil@xs4all.nl, laurent.pinchart@ideasonboard.com, mchehab@osg.samsung.com Subject: [PATCH v4 1/5] media: Determine early whether an IOCTL is supported Date: Thu, 11 Aug 2016 23:29:14 +0300 Message-Id: <1470947358-31168-2-git-send-email-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1470947358-31168-1-git-send-email-sakari.ailus@linux.intel.com> References: <1470947358-31168-1-git-send-email-sakari.ailus@linux.intel.com> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Preparation for refactoring media IOCTL handling to unify common parts. Reviewed-by: Laurent Pinchart Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/media-device.c | 54 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/drivers/media/media-device.c b/drivers/media/media-device.c index 1795abe..aedd64e 100644 --- a/drivers/media/media-device.c +++ b/drivers/media/media-device.c @@ -419,13 +419,41 @@ static long media_device_get_topology(struct media_device *mdev, return 0; } -static long media_device_ioctl(struct file *filp, unsigned int cmd, - unsigned long arg) +#define MEDIA_IOC(__cmd) \ + [_IOC_NR(MEDIA_IOC_##__cmd)] = { .cmd = MEDIA_IOC_##__cmd } + +/* the table is indexed by _IOC_NR(cmd) */ +struct media_ioctl_info { + unsigned int cmd; +}; + +static const struct media_ioctl_info ioctl_info[] = { + MEDIA_IOC(DEVICE_INFO), + MEDIA_IOC(ENUM_ENTITIES), + MEDIA_IOC(ENUM_LINKS), + MEDIA_IOC(SETUP_LINK), + MEDIA_IOC(G_TOPOLOGY), +}; + +static inline long is_valid_ioctl(const struct media_ioctl_info *info, + unsigned int cmd) +{ + return (_IOC_NR(cmd) >= ARRAY_SIZE(ioctl_info) + || info[_IOC_NR(cmd)].cmd != cmd) ? -ENOIOCTLCMD : 0; +} + +static long __media_device_ioctl( + struct file *filp, unsigned int cmd, void __user *arg, + const struct media_ioctl_info *info_array) { struct media_devnode *devnode = media_devnode_data(filp); struct media_device *dev = devnode->media_dev; long ret; + ret = is_valid_ioctl(info_array, cmd); + if (ret) + return ret; + mutex_lock(&dev->graph_mutex); switch (cmd) { case MEDIA_IOC_DEVICE_INFO: @@ -461,6 +489,13 @@ static long media_device_ioctl(struct file *filp, unsigned int cmd, return ret; } +static long media_device_ioctl(struct file *filp, unsigned int cmd, + unsigned long arg) +{ + return __media_device_ioctl( + filp, cmd, (void __user *)arg, ioctl_info); +} + #ifdef CONFIG_COMPAT struct media_links_enum32 { @@ -491,6 +526,14 @@ static long media_device_enum_links32(struct media_device *mdev, #define MEDIA_IOC_ENUM_LINKS32 _IOWR('|', 0x02, struct media_links_enum32) +static const struct media_ioctl_info compat_ioctl_info[] = { + MEDIA_IOC(DEVICE_INFO), + MEDIA_IOC(ENUM_ENTITIES), + MEDIA_IOC(ENUM_LINKS32), + MEDIA_IOC(SETUP_LINK), + MEDIA_IOC(G_TOPOLOGY), +}; + static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { @@ -498,6 +541,13 @@ static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, struct media_device *dev = devnode->media_dev; long ret; + /* + * The number of supported IOCTLs is the same for both regular and + * compat cases. Instead of passing the sizes around, ensure that + * they match. + */ + BUILD_BUG_ON(ARRAY_SIZE(ioctl_info) != ARRAY_SIZE(compat_ioctl_info)); + switch (cmd) { case MEDIA_IOC_ENUM_LINKS32: mutex_lock(&dev->graph_mutex);