From patchwork Fri Dec 2 17:21:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 29041 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp982315wrr; Fri, 2 Dec 2022 09:26:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf4FSu4ZQilah17LL8MCi/Fi3UM7O5ReRgharoqwZCPRXud8ds1VPl9ww5Z7gFofN9v43sKQ X-Received: by 2002:a17:902:b20f:b0:188:d4ea:251f with SMTP id t15-20020a170902b20f00b00188d4ea251fmr65741934plr.36.1670002012585; Fri, 02 Dec 2022 09:26:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670002012; cv=none; d=google.com; s=arc-20160816; b=mBNyAxPVc8qc0Eos5Or/k8tbVkBaF35WiBBUYXp7d+dEOpI23e7Cw1Lmzye/uekQux c7VERW8p9ZADewPQGR8x/V6aWTUGWgnl2RlJJqKtgPW1jKvmf01fl0yABiovRGU6dEsM P0JQCI/CVYtxDV5DtmXn6T8vVDonTBo9+5W8udYviHouTj8AWCXy7GMSFnzpbTzYDFTd kl6xNSoefjFNvg5LiwO138MBP7EJpzmfwVzNSQlyTH2/6WJlInEq1O9zDWnxQ5xYr4h2 TZoNRl9YcSgfP7g4XE3Yq8kXCHyHWGEsDc/Sha+ldNLgDu23xqgc2ZfD1XsIS+74T+c6 2/Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=EjhL47z9Nl+DaAg29fxcGxhmqEDsIaNpB1QWBqLjg58=; b=lzeG/F4qcWE3Eoj6Sjr4mIUdpH6Eq5QFoM9aCfWqK4Y7Wyfy/8LAL9fkJ2u258gR1S GxALHQoilVBhYC2u4DUTKnvWMJmiOXnYvPh4o9Az2RT+K08TrXzpCn4z9mJc5eW0EuF/ Y2aGkxSJFKqri87L+o1gptA3O1sc1jqYuKso6cUdS+6w+iY3bux7jbakAZ1LskYUsWNR MQX5szPmWZ4B1fK1KPReUj9AYkEpPSRvAtjrpegGPjVOtwcBZn1VfTKgBWDEsbjgmMjv TJLQCZcyyOwLNCLbPYKPV5x00/arEW2mAW4IcfMfaQyem7Vfxp1WRbHJ0NjU81lAGoY8 vusQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gUybeRSn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mw10-20020a17090b4d0a00b0020d5867aab6si8627642pjb.141.2022.12.02.09.26.38; Fri, 02 Dec 2022 09:26:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gUybeRSn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234380AbiLBRW1 (ORCPT + 99 others); Fri, 2 Dec 2022 12:22:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233837AbiLBRWC (ORCPT ); Fri, 2 Dec 2022 12:22:02 -0500 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 222699D2C3 for ; Fri, 2 Dec 2022 09:21:57 -0800 (PST) Received: by mail-ej1-x635.google.com with SMTP id fy37so13028865ejc.11 for ; Fri, 02 Dec 2022 09:21:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=EjhL47z9Nl+DaAg29fxcGxhmqEDsIaNpB1QWBqLjg58=; b=gUybeRSnsTC0aPYHfRkd5QF9Nu44JbnujAhiWe8zqjr0Na8yfDqSl4ugB+81lmz0qV EZHYcR0O8pfYxb5qaPjjJsyKJNE9VIWMvCr+dNxSTLpvwo73ZLiqppnhEl+gvTftUMUT gMLacG2c4ujg6pOGC7I1P9mR8FUnfz3GEtZLM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EjhL47z9Nl+DaAg29fxcGxhmqEDsIaNpB1QWBqLjg58=; b=8PNGiKB25gv+m9eQbaLTbN9/z4awQEZL2A8HL2I5kITfC4iX3aEuK5GDS2alIqo8nO RGSAq6w9Xy2I6jKSo1zyjitJo7mDFvlW6rD4s2VcF5z06d5r9sfhPa8/LZK+4+lU93Nt T1XlRHPFOyVHkvfgNHntBQo405G3Ul0lkMsjg1EgjKC2euLiKeDcYzD29d5y/tI0bDSd 3x67KA8Ei02nW7Y/zjiUc8XRsJdzv3bLVTNskrkFqWhjPTOgjlX3XUphzho4WBz/8+tp xG1HHeNq205iq2ML5Zw+WVKFjeVWrj9hzldeWEomjGr3L1e4W73KmbjeHhDk+UwlxwzQ RgVg== X-Gm-Message-State: ANoB5pkxKBOIGUcEINU3JlmLNMOW7/7sCgfO/7zAQqxbvD1GoixjiAUZ preMmg/2QE5l7rK2tRel0g/U9Q== X-Received: by 2002:a17:907:986b:b0:7ad:b7c2:a2f7 with SMTP id ko11-20020a170907986b00b007adb7c2a2f7mr45617347ejc.227.1670001715642; Fri, 02 Dec 2022 09:21:55 -0800 (PST) Received: from alco.roam.corp.google.com (80.71.134.83.ipv4.parknet.dk. [80.71.134.83]) by smtp.gmail.com with ESMTPSA id fe17-20020a1709072a5100b007be301a1d51sm3206685ejc.211.2022.12.02.09.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Dec 2022 09:21:55 -0800 (PST) From: Ricardo Ribalda Date: Fri, 02 Dec 2022 18:21:41 +0100 Subject: [PATCH RESEND v2 7/7] media: uvcvideo: Use standard names for menus MIME-Version: 1.0 Message-Id: <20220920-resend-v4l2-compliance-v2-7-b0ceb15353ac@chromium.org> References: <20220920-resend-v4l2-compliance-v2-0-b0ceb15353ac@chromium.org> In-Reply-To: <20220920-resend-v4l2-compliance-v2-0-b0ceb15353ac@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart Cc: Hans Verkuil , Hans Verkuil , linux-media@vger.kernel.org, Ricardo Ribalda , linux-kernel@vger.kernel.org, Hans Verkuil X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=12904; i=ribalda@chromium.org; h=from:subject:message-id; bh=CQ0Q3iEnjgF/RdatrIffgvHt8KgiNN9Adkhd3ymwRxg=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjijQsHCyx0OPBe0KIY2MAlct06a6Z8vNSFlgl59Ra dYmEr3CJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY4o0LAAKCRDRN9E+zzrEiLsDEA CbR5VB6GomNQ5k9uB94p/hQJV0SIQWBUa1L1ZtyRc0ovxw0Baysc6YVnr/xgxS7zexj467+n+mEHXB ppsw/DtQnuiJa/vv4/XcCYbwTG0kM/KiS5vTLznCfHnToC/DLQTQvytS+s70W8fv6cy8KwGOOtVUAg 4HwFFbT2DT1RKEs87bqcBOZtBPKHM+913oSyL8iPB3/663JNjJFhVtjTOD1EPuupo92WfcTUT5Jeno U66lZpxDDF1AElyCvOTeoBW3/FsE6i17nNLGGzuk96+0NhyKvvFmmBY2p8K8BhX5v/7lsISQze3kfR JdqNgcUcQ9a6ruuuUdVOPPEQPsnhcb04xB094umEdomMsa+FiD6ELNnnQ+CDhKcSGIH+Nkv2LIJGbo 60tCcPqk5K5LmUmqOAFov5EJUW6fO0DIHIjQ3xgccJhb57+c3yz1+rycQNueAGvQD32EdQqb0SVVFI ZsLOqP5i1ZBju9p79IeytwsmP+sBZBtp33Dr2vNz4v9Y3eDK9DyYiMWx8j8xmI6cHsvtIET54rzBYH Rxbx49rc74D9Mbro2Bakfb1sHri+/WveUWyZopShstgiwCTxS11WoKLBNKX9HgwdcumFu66bJMzPYU 4Y/EFhHF9Cx0Wcpvht5clDthsQuavOLQi9B4dmYM3rxkYzPRt5X44KNJJh5A== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751124030384547504?= X-GMAIL-MSGID: =?utf-8?q?1751124030384547504?= Instead of duplicating the menu info, use the one from the core. Also, do not use extra memory for 1:1 mappings. Signed-off-by: Ricardo Ribalda Suggested-by: Laurent Pinchart --- drivers/media/usb/uvc/uvc_ctrl.c | 116 +++++++++++++++++++++++++------------ drivers/media/usb/uvc/uvc_driver.c | 9 +-- drivers/media/usb/uvc/uvc_v4l2.c | 81 ++++++++++++++++++++------ drivers/media/usb/uvc/uvcvideo.h | 3 +- include/uapi/linux/uvcvideo.h | 3 +- 5 files changed, 146 insertions(+), 66 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index df273b829961..5c28d8aace37 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -363,19 +363,31 @@ static const u32 uvc_control_classes[] = { V4L2_CID_USER_CLASS, }; -static const struct uvc_menu_info power_line_frequency_controls[] = { - { 0, "Disabled" }, - { 1, "50 Hz" }, - { 2, "60 Hz" }, - { 3, "Auto" }, -}; +static const int exposure_auto_mapping[] = { 2, 1, 4, 8 }; -static const struct uvc_menu_info exposure_auto_controls[] = { - { 2, "Auto Mode" }, - { 1, "Manual Mode" }, - { 4, "Shutter Priority Mode" }, - { 8, "Aperture Priority Mode" }, -}; +static u32 uvc_mapping_get_menu_value(struct uvc_control_mapping *mapping, + u32 idx) +{ + if (!test_bit(idx, &mapping->menu_mask)) + return 0; + + if (mapping->menu_mapping) + return mapping->menu_mapping[idx]; + + return idx; +} + +static const char +*uvc_mapping_get_menu_name(struct uvc_control_mapping *mapping, u32 idx) +{ + if (!test_bit(idx, &mapping->menu_mask)) + return NULL; + + if (mapping->menu_names) + return mapping->menu_names[idx]; + + return v4l2_ctrl_get_menu(mapping->id)[idx]; +} static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, u8 query, const u8 *data) @@ -524,8 +536,8 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_BITMASK, - .menu_info = exposure_auto_controls, - .menu_mask = BIT_MASK(ARRAY_SIZE(exposure_auto_controls)), + .menu_mapping = exposure_auto_mapping, + .menu_mask = GENMASK(ARRAY_SIZE(exposure_auto_mapping) - 1, 0), .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, }, }, { @@ -730,8 +742,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings_uvc11[] = { .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_ENUM, - .menu_info = power_line_frequency_controls, - .menu_mask = BIT_MASK(ARRAY_SIZE(power_line_frequency_controls) - 1), + .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0), }, }; @@ -744,8 +755,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings_uvc15[] = { .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_ENUM, - .menu_info = power_line_frequency_controls, - .menu_mask = BIT_MASK(ARRAY_SIZE(power_line_frequency_controls)), + .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0), }, }; @@ -972,13 +982,17 @@ static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping, s32 value = mapping->get(mapping, UVC_GET_CUR, data); if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) { - const struct uvc_menu_info *menu = mapping->menu_info; unsigned int i; - for (i = 0; BIT(i) <= mapping->menu_mask; ++i, ++menu) { + for (i = 0; BIT(i) <= mapping->menu_mask; ++i) { + u32 menu_value; + if (!test_bit(i, &mapping->menu_mask)) continue; - if (menu->value == value) { + + menu_value = uvc_mapping_get_menu_value(mapping, i); + + if (menu_value == value) { value = i; break; } @@ -1174,7 +1188,6 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, { struct uvc_control_mapping *master_map = NULL; struct uvc_control *master_ctrl = NULL; - const struct uvc_menu_info *menu; unsigned int i; memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl)); @@ -1219,11 +1232,15 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1; v4l2_ctrl->step = 1; - menu = mapping->menu_info; - for (i = 0; BIT(i) <= mapping->menu_mask; ++i, ++menu) { + for (i = 0; BIT(i) <= mapping->menu_mask; ++i) { + u32 menu_value; + if (!test_bit(i, &mapping->menu_mask)) continue; - if (menu->value == v4l2_ctrl->default_value) { + + menu_value = uvc_mapping_get_menu_value(mapping, i); + + if (menu_value == v4l2_ctrl->default_value) { v4l2_ctrl->default_value = i; break; } @@ -1322,11 +1339,11 @@ int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, int uvc_query_v4l2_menu(struct uvc_video_chain *chain, struct v4l2_querymenu *query_menu) { - const struct uvc_menu_info *menu_info; struct uvc_control_mapping *mapping; struct uvc_control *ctrl; u32 index = query_menu->index; u32 id = query_menu->id; + const char *name; int ret; memset(query_menu, 0, sizeof(*query_menu)); @@ -1348,22 +1365,28 @@ int uvc_query_v4l2_menu(struct uvc_video_chain *chain, goto done; } - menu_info = &mapping->menu_info[query_menu->index]; - if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) { + u32 menu_value; + if (!ctrl->cached) { ret = uvc_ctrl_populate_cache(chain, ctrl); if (ret < 0) goto done; } - if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & menu_info->value)) { + menu_value = uvc_mapping_get_menu_value(mapping, + query_menu->index); + if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & menu_value)) { ret = -EINVAL; goto done; } } - strscpy(query_menu->name, menu_info->name, sizeof(query_menu->name)); + name = uvc_mapping_get_menu_name(mapping, query_menu->index); + if (name) + strscpy(query_menu->name, name, sizeof(query_menu->name)); + else + ret = -EINVAL; done: mutex_unlock(&chain->ctrl_mutex); @@ -1865,7 +1888,7 @@ int uvc_ctrl_set(struct uvc_fh *handle, if (!test_bit(xctrl->value, &mapping->menu_mask)) return -EINVAL; - value = mapping->menu_info[xctrl->value].value; + value = uvc_mapping_get_menu_value(mapping, xctrl->value); /* * Valid menu indices are reported by the GET_RES request for @@ -2311,12 +2334,28 @@ static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain, INIT_LIST_HEAD(&map->ev_subs); - size = sizeof(*mapping->menu_info) * fls(mapping->menu_mask); - map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL); - if (map->menu_info == NULL) { - kfree(map->name); - kfree(map); - return -ENOMEM; + if (mapping->menu_mapping && mapping->menu_mask) { + size = sizeof(mapping->menu_mapping[0]) * + fls(mapping->menu_mask); + map->menu_mapping = kmemdup(mapping->menu_mapping, size, + GFP_KERNEL); + if (!map->menu_mapping) { + kfree(map->name); + kfree(map); + return -ENOMEM; + } + } + if (mapping->menu_names && mapping->menu_mask) { + size = sizeof(mapping->menu_names[0]) * + fls(mapping->menu_mask); + map->menu_names = kmemdup(mapping->menu_names, size, + GFP_KERNEL); + if (!map->menu_names) { + kfree(map->menu_mapping); + kfree(map->name); + kfree(map); + return -ENOMEM; + } } if (map->get == NULL) @@ -2661,7 +2700,8 @@ static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev, list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) { list_del(&mapping->list); - kfree(mapping->menu_info); + kfree(mapping->menu_names); + kfree(mapping->menu_mapping); kfree(mapping->name); kfree(mapping); } diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index abdb9ca7eed6..97c267e75fa4 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2661,11 +2661,6 @@ MODULE_PARM_DESC(timeout, "Streaming control requests timeout"); * Driver initialization and cleanup */ -static const struct uvc_menu_info power_line_frequency_controls_limited[] = { - { 1, "50 Hz" }, - { 2, "60 Hz" }, -}; - static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { .id = V4L2_CID_POWER_LINE_FREQUENCY, .entity = UVC_GUID_UVC_PROCESSING, @@ -2674,8 +2669,8 @@ static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_ENUM, - .menu_info = power_line_frequency_controls_limited, - .menu_mask = BIT_MASK(ARRAY_SIZE(power_line_frequency_controls_limited)), + .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ, + V4L2_CID_POWER_LINE_FREQUENCY_50HZ), }; static const struct uvc_device_info uvc_ctrl_power_line_limited = { diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index e6792fd46bf5..89581c1559df 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -25,6 +25,64 @@ #include "uvcvideo.h" +static int uvc_control_xu_2_mapping(struct uvc_control_mapping *map, + struct uvc_xu_control_mapping *xmap) +{ + char (*names)[UVC_MENU_NAME_LEN]; + unsigned int i; + u32 *mapping; + size_t size; + + /* Prevent excessive memory consumption, as well as integer + * overflows. + */ + if (xmap->menu_count == 0 || + xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) + return -EINVAL; + + map->menu_mask = BIT_MASK(xmap->menu_count); + + size = xmap->menu_count * sizeof(*map->menu_mapping); + mapping = kzalloc(size, GFP_KERNEL); + if (!mapping) + return -ENOMEM; + + for (i = 0; i < xmap->menu_count ; i++) + if (copy_from_user(&mapping[i], &xmap->menu_info[i].value, + sizeof(mapping[i]))) { + kfree(mapping); + return -ENOMEM; + } + + map->menu_mapping = mapping; + + /* + * Always use the standard naming if available. + */ + if (v4l2_ctrl_get_menu(map->id)) + return 0; + + size = xmap->menu_count * sizeof(map->menu_names[0]); + names = kzalloc(size, GFP_KERNEL); + if (!names) { + kfree(mapping); + return -ENOMEM; + } + + for (i = 0; i < xmap->menu_count ; i++) { + /* sizeof(names[i]) - 1: to take care of \0 */ + if (copy_from_user(&names[i], &xmap->menu_info[i].name, + sizeof(names[i]) - 1)) { + kfree(names); + kfree(mapping); + return -ENOMEM; + } + } + map->menu_names = names; + + return 0; +} + /* ------------------------------------------------------------------------ * UVC ioctls */ @@ -32,7 +90,6 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, struct uvc_xu_control_mapping *xmap) { struct uvc_control_mapping *map; - unsigned int size; int ret; map = kzalloc(sizeof(*map), GFP_KERNEL); @@ -63,24 +120,9 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, break; case V4L2_CTRL_TYPE_MENU: - /* - * Prevent excessive memory consumption, as well as integer - * overflows. - */ - if (xmap->menu_count == 0 || - xmap->menu_count > UVC_MAX_CONTROL_MENU_ENTRIES) { - ret = -EINVAL; - goto free_map; - } - - size = xmap->menu_count * sizeof(*map->menu_info); - map->menu_info = memdup_user(xmap->menu_info, size); - if (IS_ERR(map->menu_info)) { - ret = PTR_ERR(map->menu_info); + ret = uvc_control_xu_2_mapping(map, xmap); + if (ret) goto free_map; - } - - map->menu_mask = BIT_MASK(xmap->menu_count); break; default: @@ -92,7 +134,8 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, ret = uvc_ctrl_add_mapping(chain, map); - kfree(map->menu_info); + kfree(map->menu_names); + kfree(map->menu_mapping); free_map: kfree(map); diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 7e2339fc256e..8ebd7ee2934d 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -254,7 +254,8 @@ struct uvc_control_mapping { enum v4l2_ctrl_type v4l2_type; u32 data_type; - const struct uvc_menu_info *menu_info; + const u32 *menu_mapping; + const char (*menu_names)[UVC_MENU_NAME_LEN]; unsigned long menu_mask; u32 master_id; diff --git a/include/uapi/linux/uvcvideo.h b/include/uapi/linux/uvcvideo.h index 8288137387c0..1b64b6aa40b5 100644 --- a/include/uapi/linux/uvcvideo.h +++ b/include/uapi/linux/uvcvideo.h @@ -36,9 +36,10 @@ UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES | \ UVC_CTRL_FLAG_GET_DEF) +#define UVC_MENU_NAME_LEN 32 struct uvc_menu_info { __u32 value; - __u8 name[32]; + __u8 name[UVC_MENU_NAME_LEN]; }; struct uvc_xu_control_mapping {