From patchwork Tue Apr 24 21:29:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerry Zhang X-Patchwork-Id: 10361103 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 40E92602D6 for ; Tue, 24 Apr 2018 21:29:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E855289BD for ; Tue, 24 Apr 2018 21:29:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2244528D77; Tue, 24 Apr 2018 21:29:27 +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=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 6D67F289BD for ; Tue, 24 Apr 2018 21:29:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751242AbeDXV3Y (ORCPT ); Tue, 24 Apr 2018 17:29:24 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:36555 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750835AbeDXV3X (ORCPT ); Tue, 24 Apr 2018 17:29:23 -0400 Received: by mail-pg0-f66.google.com with SMTP id i6so11791692pgv.3 for ; Tue, 24 Apr 2018 14:29:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=pEldiCY+H+ZEwBAZQ4K0HCxH0qev5ttEvD9PJq9dp5Y=; b=NQhxEbvhMn/7HFCbTiKUWgUGvpPzXD3JeA3N/dhHDbvRI8ISjIR6uifJhAMJlrpbye /DkHRMKNpoUUsBfETqll/tJ6BvNOzUfph8wcDgjQuDmuhrFLXloUk2WqqJ4Jz0/D4D0H KJPIPpV+fV6wsjYGD7tTQ2lcDZ3kgDF925xqPlBfVfamX6QGs1j/1jRP7og4Pjax3sd5 XpcbkngzS9wT7woC30hKARag8Sg6Dcqv1V9xVT4N/jslyIvzFRxAcVu17h7ZBb5pFd0P ul4czeYOZEtu4YP114E0WjWzaRAKooHrKS7zSU7N3O/7ZWR1M1+wJZMDdT2ojISkXVHb W2FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=pEldiCY+H+ZEwBAZQ4K0HCxH0qev5ttEvD9PJq9dp5Y=; b=nmM2mF+xIgYmo3RtYZDoTXFtdjv/AKqJG0yYo6769lNJ8PeI3MLctbib0oomXrMT1t +XhzpJoFcMeuoL74hoaryC8aEO8jxYJvi3JMtOTW3Rc2gW0FYGqml4p6obu28eDLCXqJ 3eNRU5MLdFzNyaPAos6rr+WllHYi+qICAoAjTXEtHvfTUIcWkNBJJrJvFkmv7JSpyxsR 5AWBdQpUdCtuNg51Vv5xPMjxNK1qL1TkrwyctNOqarJtp40wGsp5JvEhZho4UTFS4LH5 9Vgw4yFIoAO6+cR1A4kaSFS8V4pRuQuWTDhB/sBZXlP8iVYMsxqc9ukgz1bbCBQN0qxz 1qjw== X-Gm-Message-State: ALQs6tDrK1/5dG+eXPcdOs7eQRaERfca4Lby9yEVrR8z+4gZb1ERRdAB zgcCuI2+LVpApAdPp+Qy+BHMnQ== X-Google-Smtp-Source: AIpwx49jo6o7cBruRNvOLFwmVk1Qu1MI0XhCgHMfP26IVEVUEQDFA7xmYgRxZATNARwOl+JSYrSnZw== X-Received: by 2002:a17:902:c24:: with SMTP id 33-v6mr25905941pls.88.1524605362596; Tue, 24 Apr 2018 14:29:22 -0700 (PDT) Received: from zhangjerry.mtv.corp.google.com ([2620:0:1000:1611:ee20:2649:babf:1d14]) by smtp.gmail.com with ESMTPSA id y15sm28637797pfb.37.2018.04.24.14.29.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 14:29:21 -0700 (PDT) From: Jerry Zhang To: Felipe Balbi , Greg Kroah-Hartman , linux-usb@vger.kernel.org Cc: Michal Nazarewicz , Krzysztof Opasiak , Badhri Jagan Sridharan , Andrzej Pietrasiewicz , felixhaedicke@web.de, Jerry Zhang Subject: [PATCH V2 2/4] usb: gadget: configfs: Create control_config group Date: Tue, 24 Apr 2018 14:29:09 -0700 Message-Id: <20180424212909.169874-1-zhangjerry@google.com> X-Mailer: git-send-email 2.17.0.484.g0c8726318c-goog In-Reply-To: <20180417011045.183035-3-zhangjerry@google.com> References: <20180417011045.183035-3-zhangjerry@google.com> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Control_config is a group under gadget that acts as a normal config group, except it does not appear in cdev->configs. Functions that have exactly zero descriptors can be linked into control_config. These functions are bound and unbound with the rest of the gadget, but are never enabled. Also, functions with zero descriptors cannot be used in real configs. Create configfs_setup(), which will first attempt composite setup. If that fails, it will go through functions in control_config and use req_match to find and deliver the request to a function that can handle it. This allows the user to create a functionfs instance dedicated to handling non-standard control requests no matter what functions or configurations are currently active. Signed-off-by: Jerry Zhang --- Changes in V2: - Added config_item_type for control_config without default attrs - Changed bind to fail if the wrong kind of function is linked to a normal config or control_config - Changed bind to fail if a control function has no req_match, instead of skipping it over drivers/usb/gadget/configfs.c | 114 ++++++++++++++++++++++++++++------ 1 file changed, 96 insertions(+), 18 deletions(-) diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c index efba66ca0719..ed3d675ee7bb 100644 --- a/drivers/usb/gadget/configfs.c +++ b/drivers/usb/gadget/configfs.c @@ -44,12 +44,22 @@ int check_user_usb_string(const char *name, static const struct usb_descriptor_header *otg_desc[2]; +struct config_usb_cfg { + struct config_group group; + struct config_group strings_group; + struct list_head string_list; + struct usb_configuration c; + struct list_head func_list; + struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; +}; + struct gadget_info { struct config_group group; struct config_group functions_group; struct config_group configs_group; struct config_group strings_group; struct config_group os_desc_group; + struct config_usb_cfg control_config; struct mutex lock; struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; @@ -68,15 +78,6 @@ static inline struct gadget_info *to_gadget_info(struct config_item *item) return container_of(to_config_group(item), struct gadget_info, group); } -struct config_usb_cfg { - struct config_group group; - struct config_group strings_group; - struct list_head string_list; - struct usb_configuration c; - struct list_head func_list; - struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; -}; - static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) { return container_of(to_config_group(item), struct config_usb_cfg, @@ -512,6 +513,16 @@ static const struct config_item_type gadget_config_type = { .ct_owner = THIS_MODULE, }; +static struct configfs_item_operations control_config_item_ops = { + .allow_link = config_usb_cfg_link, + .drop_link = config_usb_cfg_unlink, +}; + +static const struct config_item_type control_config_type = { + .ct_item_ops = &control_config_item_ops, + .ct_owner = THIS_MODULE, +}; + static const struct config_item_type gadget_root_type = { .ct_item_ops = &gadget_root_item_ops, .ct_attrs = gadget_root_attrs, @@ -1205,11 +1216,10 @@ int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, static void purge_configs_funcs(struct gadget_info *gi) { struct usb_configuration *c; + struct usb_function *f, *tmp; + struct config_usb_cfg *cfg; list_for_each_entry(c, &gi->cdev.configs, list) { - struct usb_function *f, *tmp; - struct config_usb_cfg *cfg; - cfg = container_of(c, struct config_usb_cfg, c); list_for_each_entry_safe(f, tmp, &c->functions, list) { @@ -1229,6 +1239,14 @@ static void purge_configs_funcs(struct gadget_info *gi) c->highspeed = 0; c->fullspeed = 0; } + + cfg = &gi->control_config; + c = &cfg->c; + list_for_each_entry_safe(f, tmp, &c->functions, list) { + list_move_tail(&f->list, &cfg->func_list); + if (f->unbind) + f->unbind(c, f); + } } static int configfs_composite_bind(struct usb_gadget *gadget, @@ -1242,6 +1260,9 @@ static int configfs_composite_bind(struct usb_gadget *gadget, struct usb_string *s; unsigned i; int ret; + struct config_usb_cfg *cfg; + struct usb_function *f; + struct usb_function *tmp; /* the gi->lock is hold by the caller */ cdev->gadget = gadget; @@ -1260,8 +1281,6 @@ static int configfs_composite_bind(struct usb_gadget *gadget, list_for_each_entry(c, &gi->cdev.configs, list) { - struct config_usb_cfg *cfg; - cfg = container_of(c, struct config_usb_cfg, c); if (list_empty(&cfg->func_list)) { pr_err("Config %s/%d of %s needs at least one function.\n", @@ -1320,9 +1339,6 @@ static int configfs_composite_bind(struct usb_gadget *gadget, /* Go through all configs, attach all functions */ list_for_each_entry(c, &gi->cdev.configs, list) { - struct config_usb_cfg *cfg; - struct usb_function *f; - struct usb_function *tmp; struct gadget_config_name *cn; if (gadget_is_otg(gadget)) @@ -1349,6 +1365,12 @@ static int configfs_composite_bind(struct usb_gadget *gadget, list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { list_del(&f->list); ret = usb_add_function(c, f); + if (!(f->fs_descriptors || f->hs_descriptors + || f->ss_descriptors || f->ssp_descriptors)) { + pr_err("%s can't be used in a config since it " + "has no descriptors\n", f->name); + ret = ret < 0 ? ret : -EINVAL; + } if (ret) { list_add(&f->list, &cfg->func_list); goto err_purge_funcs; @@ -1362,6 +1384,27 @@ static int configfs_composite_bind(struct usb_gadget *gadget, goto err_purge_funcs; } + cfg = &gi->control_config; + c = &cfg->c; + list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { + if (!f->req_match || !f->setup) { + ret = -EINVAL; + goto err_purge_funcs; + } + list_del(&f->list); + ret = usb_add_function(&cfg->c, f); + if (f->fs_descriptors || f->hs_descriptors + || f->ss_descriptors || f->ssp_descriptors) { + pr_err("Only functions with no descriptors " + "can be used in control_config\n"); + ret = ret < 0 ? ret : -EINVAL; + } + if (ret) { + list_add(&f->list, &cfg->func_list); + goto err_purge_funcs; + } + } + usb_ep_autoconfig_reset(cdev->gadget); return 0; @@ -1391,11 +1434,34 @@ static void configfs_composite_unbind(struct usb_gadget *gadget) set_gadget_data(gadget, NULL); } +static int configfs_composite_setup(struct usb_gadget *gadget, + const struct usb_ctrlrequest *c) +{ + struct usb_composite_dev *cdev = get_gadget_data(gadget); + struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); + + struct config_usb_cfg *cfg = &gi->control_config; + struct usb_function *f; + int value; + + value = composite_setup(gadget, c); + if (value >= 0) + return value; + + list_for_each_entry(f, &cfg->c.functions, list) { + if (f->req_match(f, c, false)) { + value = f->setup(f, c); + break; + } + } + return value; +} + static const struct usb_gadget_driver configfs_driver_template = { .bind = configfs_composite_bind, .unbind = configfs_composite_unbind, - .setup = composite_setup, + .setup = configfs_composite_setup, .reset = composite_disconnect, .disconnect = composite_disconnect, @@ -1461,6 +1527,18 @@ static struct config_group *gadgets_make( if (!gi->composite.gadget_driver.function) goto err; + gi->control_config.c.label = "control_config"; + /* composite requires some value, but it doesn't matter */ + gi->control_config.c.bConfigurationValue = 42; + INIT_LIST_HEAD(&gi->control_config.func_list); + config_group_init_type_name(&gi->control_config.group, + "control_config", &control_config_type); + configfs_add_default_group(&gi->control_config.group, &gi->group); + + if (usb_add_config_only(&gi->cdev, &gi->control_config.c)) + goto err; + list_del(&gi->control_config.c.list); + return &gi->group; err: kfree(gi);