From patchwork Tue Apr 16 14:07:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain Izard X-Patchwork-Id: 10903195 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 66AF814DB for ; Tue, 16 Apr 2019 14:08:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4C43D26242 for ; Tue, 16 Apr 2019 14:08:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 40749287D1; Tue, 16 Apr 2019 14:08:00 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,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 219A426242 for ; Tue, 16 Apr 2019 14:07:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729066AbfDPOHy (ORCPT ); Tue, 16 Apr 2019 10:07:54 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:34045 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726713AbfDPOHx (ORCPT ); Tue, 16 Apr 2019 10:07:53 -0400 Received: by mail-wr1-f68.google.com with SMTP id p10so27304156wrq.1; Tue, 16 Apr 2019 07:07:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+HY91w5f+9s/uMWknBV9UYHwFztj3VHolQha5X2UoMo=; b=Qio959ZiEBsMV5cgyIL2Qrz7WLWDgXeDQwsbiGUGy+2xJTIG3a78Ic+Czz10YgM57Y ZgXi9xHjuOtBmSWkoZl68+gxA2uZkFr23Hb93WwFWpih+K+eEoB9B3re0Mh96Jyax5tw 9uczqVpHHU5Ka4bd8MGXqCayDf8ZtIIrXh9FJvKnME+rGC37mKX1EZ/i4ttS7TGgXPx1 w8xQousk+zZPlqledwVBFB1y0Ttpum1E+LO+1A6wsZaB87PgktBlBbAONku5YNi8y7QA XdIlBqvUcdKKQKZSD1iT40EpEG8fUEHynFuYNinLTV9uN9eY23T7Nth8xvqOtKeBQov5 oaUw== 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=+HY91w5f+9s/uMWknBV9UYHwFztj3VHolQha5X2UoMo=; b=DF44tSfPtwnHAKsiczppUKQIiOb64l5Qw6zAM2rWjPzMVIMPDcYisYKDFrKWNFLhh7 y/4oCP7/1rTAa2dDEMC4iNJ0HC9BlxEObye8rAYq4Q9HnTssmH+v1VObFNIoakWLBF9Q B1p/t0pS3cgfE5orY8eiehRcC8jMkgbzN2NNBoiXkvaDftm5EEcsd1ZO3uXBFXCKED3i 1khEmuq2oAtD7Gab9ulSKnT+ozCQLf8JhbiZtOoHebp9/a6dyOVXFXUx81/1T0wa8+lM /qXi372GgzXD7EDpJGQpuPS4z9uRPQqyKt/VySjXVgx48beWL5/uMp6YpW7sXm13tX+B 6DXQ== X-Gm-Message-State: APjAAAVCN6n04m1nX883L8zC6j6+PP+19TtosuDfMyWF9Ft2Agf+b4mk thB7kjyNcUm/WqB3BHDh4nk= X-Google-Smtp-Source: APXvYqwQUpN2z/xMJdGlvthSFTSeOI6pMVEPRSldxsbz2UKDArc7VW0qrmFuFAu9dAkuqezWDHaGBw== X-Received: by 2002:adf:ed8f:: with SMTP id c15mr28853326wro.144.1555423670848; Tue, 16 Apr 2019 07:07:50 -0700 (PDT) Received: from 5WDYG62.mdi (static-css-cqn-143221.business.bouyguestelecom.com. [176.149.143.221]) by smtp.gmail.com with ESMTPSA id v192sm24008494wme.24.2019.04.16.07.07.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 07:07:49 -0700 (PDT) From: Romain Izard To: Felipe Balbi , Greg Kroah-Hartman , Andrzej Pietrasiewicz Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Romain Izard Subject: [PATCH 2/2] usb: gadget: f_ncm: Add OS descriptor support Date: Tue, 16 Apr 2019 16:07:32 +0200 Message-Id: <20190416140732.21941-2-romain.izard.pro@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190416140732.21941-1-romain.izard.pro@gmail.com> References: <20190416140732.21941-1-romain.izard.pro@gmail.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 To be able to use the default USB class drivers available in Microsoft Windows, we need to add OS descriptors to the exported USB gadget to tell the OS that we are compatible with the built-in drivers. Copy the OS descriptor support from f_rndis into f_ncm. As a result, using the WINNCM compatible ID, the UsbNcm driver is loaded on enumeration without the need for a custom driver or inf file. Signed-off-by: Romain Izard --- drivers/usb/gadget/function/f_ncm.c | 47 +++++++++++++++++++++++++++-- drivers/usb/gadget/function/u_ncm.h | 3 ++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c index d5c47e7a7f61..2d6e76e4cffa 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -23,6 +23,7 @@ #include "u_ether.h" #include "u_ether_configfs.h" #include "u_ncm.h" +#include "configfs.h" /* * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link. @@ -1391,6 +1392,16 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f) return -EINVAL; ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); + + if (cdev->use_os_string) { + f->os_desc_table = kzalloc(sizeof(*f->os_desc_table), + GFP_KERNEL); + if (!f->os_desc_table) + return -ENOMEM; + f->os_desc_n = 1; + f->os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc; + } + /* * in drivers/usb/gadget/configfs.c:configfs_composite_bind() * configurations are bound in sequence with list_for_each_entry, @@ -1404,13 +1415,15 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f) status = gether_register_netdev(ncm_opts->net); mutex_unlock(&ncm_opts->lock); if (status) - return status; + goto fail; ncm_opts->bound = true; } us = usb_gstrings_attach(cdev, ncm_strings, ARRAY_SIZE(ncm_string_defs)); - if (IS_ERR(us)) - return PTR_ERR(us); + if (IS_ERR(us)) { + status = PTR_ERR(us); + goto fail; + } ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id; ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id; ncm_data_intf.iInterface = us[STRING_DATA_IDX].id; @@ -1427,6 +1440,10 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f) ncm_control_intf.bInterfaceNumber = status; ncm_union_desc.bMasterInterface0 = status; + if (cdev->use_os_string) + f->os_desc_table[0].if_id = + ncm_iad_desc.bFirstInterface; + status = usb_interface_id(c, f); if (status < 0) goto fail; @@ -1506,6 +1523,9 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f) return 0; fail: + kfree(f->os_desc_table); + f->os_desc_n = 0; + if (ncm->notify_req) { kfree(ncm->notify_req->buf); usb_ep_free_request(ncm->notify, ncm->notify_req); @@ -1560,16 +1580,22 @@ static void ncm_free_inst(struct usb_function_instance *f) gether_cleanup(netdev_priv(opts->net)); else free_netdev(opts->net); + kfree(opts->ncm_interf_group); kfree(opts); } static struct usb_function_instance *ncm_alloc_inst(void) { struct f_ncm_opts *opts; + struct usb_os_desc *descs[1]; + char *names[1]; + struct config_group *ncm_interf_group; opts = kzalloc(sizeof(*opts), GFP_KERNEL); if (!opts) return ERR_PTR(-ENOMEM); + opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id; + mutex_init(&opts->lock); opts->func_inst.free_func_inst = ncm_free_inst; opts->net = gether_setup_default(); @@ -1578,8 +1604,20 @@ static struct usb_function_instance *ncm_alloc_inst(void) kfree(opts); return ERR_CAST(net); } + INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop); + + descs[0] = &opts->ncm_os_desc; + names[0] = "ncm"; config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type); + ncm_interf_group = + usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs, + names, THIS_MODULE); + if (IS_ERR(ncm_interf_group)) { + ncm_free_inst(&opts->func_inst); + return ERR_CAST(ncm_interf_group); + } + opts->ncm_interf_group = ncm_interf_group; return &opts->func_inst; } @@ -1605,6 +1643,9 @@ static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) hrtimer_cancel(&ncm->task_timer); + kfree(f->os_desc_table); + f->os_desc_n = 0; + ncm_string_defs[0].id = 0; usb_free_all_descriptors(f); diff --git a/drivers/usb/gadget/function/u_ncm.h b/drivers/usb/gadget/function/u_ncm.h index d483e45c0f77..70da3201a1d0 100644 --- a/drivers/usb/gadget/function/u_ncm.h +++ b/drivers/usb/gadget/function/u_ncm.h @@ -20,6 +20,9 @@ struct f_ncm_opts { struct net_device *net; bool bound; + struct config_group *ncm_interf_group; + struct usb_os_desc ncm_os_desc; + char ncm_ext_compat_id[16]; /* * Read/write access to configfs attributes is handled by configfs. *