From patchwork Tue Apr 16 14:07:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain Izard X-Patchwork-Id: 10903193 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 4C30C14DB for ; Tue, 16 Apr 2019 14:07:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 03845287CC for ; Tue, 16 Apr 2019 14:07:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E8EBA287D3; Tue, 16 Apr 2019 14:07:53 +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 892CE287CC for ; Tue, 16 Apr 2019 14:07:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727326AbfDPOHr (ORCPT ); Tue, 16 Apr 2019 10:07:47 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:44402 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726713AbfDPOHr (ORCPT ); Tue, 16 Apr 2019 10:07:47 -0400 Received: by mail-wr1-f65.google.com with SMTP id y7so27232809wrn.11; Tue, 16 Apr 2019 07:07:46 -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; bh=2+D4bewqFy4nGLicEOZn/Id12fvAtNwEFAcyOEIgm4I=; b=Ikd0o67ZrKxOaHpUssQg/OnYxGqylkllsNPGtrxJcnLkit4UW56jW+4/EbOSUKwKQH dS8NsY3nLTC+oAModX+id5nPXiuxtVNWmbXFA4o4LhP1xsTND+/EdAoYuRReX8DsKQ+7 HJGChT42xfQwZFBgdkUHYD3zgcKVylkTp0ubKpp8BHl6ZJs9SRe0oN1/05XmgDOarRnF def1zr018F8pYwKaEdlUJR7V403ZLpsDclfIjpv0141MLBdOlJxo/rUp+xehxozjm3Pq XQU84t3teNr/8xeu3GRtJ74afsG4VNDqSsmD2BD2Cppa2kWI5r++czp1eO2l90yT6v/x PiZg== 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; bh=2+D4bewqFy4nGLicEOZn/Id12fvAtNwEFAcyOEIgm4I=; b=VPNVvMKaeSE2kXHUxlGxamAch5DJWwey91eVzoHku88QWovXPt6HfHxlRkyytZWZ3Q Og0th2aUYDh5Tv8GLGIQnJ4dIwrpU/TSVk31KaO8PAZZSqLlOmOByuFys5G7+fMqDQug cYZ1O26rNY5i6DZKr98mG49KsdXm0hrC/UH+93m0ZPlXsUcOj00Ha8RQ8y8Vyr2skGal K0quxLaO2pFgMVH3j91p/1L4Z0u9ttCvWpHXDOZAwMFH5hX+4qsbhYLex+D5Z14tuUEi ZSXMpYTCsdHZefLaPmIthPkJAZL6jkQOitW01ZcOP7Wdl/wxpQ9igWiR7OeIqYnJza0V 9fMA== X-Gm-Message-State: APjAAAXm0a/76DPjgnEi94Di3lwl1n7Qh3UmNwZ0xc0al4dCnkxjENTW Q8vACSJzRNYcE/QfRNd3OcQ= X-Google-Smtp-Source: APXvYqwu2QX5QEz/JV3kPKI8dNHgOVqeHbvceTAs9Qx1OK5IqI+3fzyTpfshEBPokoK0R7pQAJHS1Q== X-Received: by 2002:a5d:424b:: with SMTP id s11mr43565750wrr.158.1555423666052; Tue, 16 Apr 2019 07:07:46 -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.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 07:07:45 -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 1/2] usb: gadget: f_ncm: Fix NTP-32 support Date: Tue, 16 Apr 2019 16:07:31 +0200 Message-Id: <20190416140732.21941-1-romain.izard.pro@gmail.com> X-Mailer: git-send-email 2.17.1 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 When connecting a CDC-NCM gadget to an host that uses the NTP-32 mode, or that relies on the default CRC setting, the current implementation gets confused, and does not expect the correct signature for its packets. Fix this, by ensuring that the ndp_sign member in the f_ncm structure always contain a valid value. Signed-off-by: Romain Izard --- drivers/usb/gadget/function/f_ncm.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c index 5780fba620ab..d5c47e7a7f61 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -35,9 +35,7 @@ /* to trigger crc/non-crc ndp signature */ -#define NCM_NDP_HDR_CRC_MASK 0x01000000 #define NCM_NDP_HDR_CRC 0x01000000 -#define NCM_NDP_HDR_NOCRC 0x00000000 enum ncm_notify_state { NCM_NOTIFY_NONE, /* don't notify */ @@ -526,6 +524,7 @@ static inline void ncm_reset_values(struct f_ncm *ncm) { ncm->parser_opts = &ndp16_opts; ncm->is_crc = false; + ncm->ndp_sign = ncm->parser_opts->ndp_sign; ncm->port.cdc_filter = DEFAULT_FILTER; /* doesn't make sense for ncm, fixed size used */ @@ -805,25 +804,20 @@ static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | USB_CDC_SET_CRC_MODE: { - int ndp_hdr_crc = 0; - if (w_length != 0 || w_index != ncm->ctrl_id) goto invalid; switch (w_value) { case 0x0000: ncm->is_crc = false; - ndp_hdr_crc = NCM_NDP_HDR_NOCRC; DBG(cdev, "non-CRC mode selected\n"); break; case 0x0001: ncm->is_crc = true; - ndp_hdr_crc = NCM_NDP_HDR_CRC; DBG(cdev, "CRC mode selected\n"); break; default: goto invalid; } - ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc; value = 0; break; } @@ -840,6 +834,8 @@ static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) ctrl->bRequestType, ctrl->bRequest, w_value, w_index, w_length); } + ncm->ndp_sign = ncm->parser_opts->ndp_sign | + (ncm->is_crc ? NCM_NDP_HDR_CRC : 0); /* respond with data transfer or status phase? */ if (value >= 0) { 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. *