stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support
@ 2023-05-31 17:33 Joakim Tjernlund
  2023-06-17 16:03 ` Joakim Tjernlund
  0 siblings, 1 reply; 9+ messages in thread
From: Joakim Tjernlund @ 2023-05-31 17:33 UTC (permalink / raw)
  To: stable; +Cc: Romain Izard, Felipe Balbi, Joakim Tjernlund

From: Romain Izard <romain.izard.pro@gmail.com>

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 <romain.izard.pro@gmail.com>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
Cc: stable@vger.kernel.org # v4.19
---

 Seems to have been forgotten when backporting NCM fixes.
 Needed to make Win10 accept Linux NCM gadget ethernet

 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 e4aa370e86a9..b4571633f7b5 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.
@@ -1432,6 +1433,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,
@@ -1445,13 +1456,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;
@@ -1468,6 +1481,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;
@@ -1547,6 +1564,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);
@@ -1601,16 +1621,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();
@@ -1619,8 +1645,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;
 }
@@ -1646,6 +1684,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 67324f983343..dfd75ad61b3f 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.
 	 *
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support
  2023-05-31 17:33 [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support Joakim Tjernlund
@ 2023-06-17 16:03 ` Joakim Tjernlund
  2023-06-18  7:36   ` Greg KH
  0 siblings, 1 reply; 9+ messages in thread
From: Joakim Tjernlund @ 2023-06-17 16:03 UTC (permalink / raw)
  To: stable@vger.kernel.org
  Cc: felipe.balbi@linux.intel.com, romain.izard.pro@gmail.com

Ping ?

Did I do something wrong with submission or is it queued for later ?
4.19 is missing these which make USB NCM unusable with Win >= 10. 

 Jocke

On Wed, 2023-05-31 at 19:33 +0200, Joakim Tjernlund wrote:
> From: Romain Izard <romain.izard.pro@gmail.com>
> 
> 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 <romain.izard.pro@gmail.com>
> Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
> Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
> Cc: stable@vger.kernel.org # v4.19
> ---
> 
>  Seems to have been forgotten when backporting NCM fixes.
>  Needed to make Win10 accept Linux NCM gadget ethernet
> 
>  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 e4aa370e86a9..b4571633f7b5 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.
> @@ -1432,6 +1433,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,
> @@ -1445,13 +1456,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;
> @@ -1468,6 +1481,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;
> @@ -1547,6 +1564,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);
> @@ -1601,16 +1621,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();
> @@ -1619,8 +1645,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;
>  }
> @@ -1646,6 +1684,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 67324f983343..dfd75ad61b3f 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.
>  	 *


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support
  2023-06-17 16:03 ` Joakim Tjernlund
@ 2023-06-18  7:36   ` Greg KH
  2023-06-18  9:58     ` Joakim Tjernlund
  0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2023-06-18  7:36 UTC (permalink / raw)
  To: Joakim Tjernlund
  Cc: stable@vger.kernel.org, felipe.balbi@linux.intel.com,
	romain.izard.pro@gmail.com

On Sat, Jun 17, 2023 at 04:03:06PM +0000, Joakim Tjernlund wrote:
> Ping ?
> 
> Did I do something wrong with submission or is it queued for later ?
> 4.19 is missing these which make USB NCM unusable with Win >= 10. 
> 
>  Jocke
> 
> On Wed, 2023-05-31 at 19:33 +0200, Joakim Tjernlund wrote:
> > From: Romain Izard <romain.izard.pro@gmail.com>
> > 
> > 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 <romain.izard.pro@gmail.com>
> > Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
> > Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
> > Cc: stable@vger.kernel.org # v4.19
> > ---
> > 
> >  Seems to have been forgotten when backporting NCM fixes.
> >  Needed to make Win10 accept Linux NCM gadget ethernet
> > 
> >  drivers/usb/gadget/function/f_ncm.c | 47 +++++++++++++++++++++++++++--
> >  drivers/usb/gadget/function/u_ncm.h |  3 ++
> >  2 files changed, 47 insertions(+), 3 deletions(-)

What is the git commit id of this change in Linus's tree?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support
  2023-06-18  7:36   ` Greg KH
@ 2023-06-18  9:58     ` Joakim Tjernlund
  2023-06-18 11:04       ` gregkh
  0 siblings, 1 reply; 9+ messages in thread
From: Joakim Tjernlund @ 2023-06-18  9:58 UTC (permalink / raw)
  To: gregkh@linuxfoundation.org
  Cc: felipe.balbi@linux.intel.com, stable@vger.kernel.org,
	romain.izard.pro@gmail.com

On Sun, 2023-06-18 at 09:36 +0200, Greg KH wrote:
> On Sat, Jun 17, 2023 at 04:03:06PM +0000, Joakim Tjernlund wrote:
> > Ping ?
> > 
> > Did I do something wrong with submission or is it queued for later ?
> > 4.19 is missing these which make USB NCM unusable with Win >= 10. 
> > 
> >  Jocke
> > 
> > On Wed, 2023-05-31 at 19:33 +0200, Joakim Tjernlund wrote:
> > > From: Romain Izard <romain.izard.pro@gmail.com>
> > > 
> > > 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 <romain.izard.pro@gmail.com>
> > > Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
> > > Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
> > > Cc: stable@vger.kernel.org # v4.19
> > > ---
> > > 
> > >  Seems to have been forgotten when backporting NCM fixes.
> > >  Needed to make Win10 accept Linux NCM gadget ethernet
> > > 
> > >  drivers/usb/gadget/function/f_ncm.c | 47 +++++++++++++++++++++++++++--
> > >  drivers/usb/gadget/function/u_ncm.h |  3 ++
> > >  2 files changed, 47 insertions(+), 3 deletions(-)
> 
> What is the git commit id of this change in Linus's tree?
> 
> thanks,
> 
> greg k-h
For this patch:
	793409292382027226769d0299987f06cbd97a6e

and for "usb: gadget: f_ncm: Fix NTP-32 support"
	550eef0c353030ac4223b9c9479bdf77a05445d6

Thanks
       Jocke

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support
  2023-06-18  9:58     ` Joakim Tjernlund
@ 2023-06-18 11:04       ` gregkh
  2023-06-18 11:40         ` [PATCHv2 " Joakim Tjernlund
  2023-06-18 11:42         ` [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support Joakim Tjernlund
  0 siblings, 2 replies; 9+ messages in thread
From: gregkh @ 2023-06-18 11:04 UTC (permalink / raw)
  To: Joakim Tjernlund
  Cc: felipe.balbi@linux.intel.com, stable@vger.kernel.org,
	romain.izard.pro@gmail.com

On Sun, Jun 18, 2023 at 09:58:14AM +0000, Joakim Tjernlund wrote:
> On Sun, 2023-06-18 at 09:36 +0200, Greg KH wrote:
> > On Sat, Jun 17, 2023 at 04:03:06PM +0000, Joakim Tjernlund wrote:
> > > Ping ?
> > > 
> > > Did I do something wrong with submission or is it queued for later ?
> > > 4.19 is missing these which make USB NCM unusable with Win >= 10. 
> > > 
> > >  Jocke
> > > 
> > > On Wed, 2023-05-31 at 19:33 +0200, Joakim Tjernlund wrote:
> > > > From: Romain Izard <romain.izard.pro@gmail.com>
> > > > 
> > > > 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 <romain.izard.pro@gmail.com>
> > > > Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
> > > > Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
> > > > Cc: stable@vger.kernel.org # v4.19
> > > > ---
> > > > 
> > > >  Seems to have been forgotten when backporting NCM fixes.
> > > >  Needed to make Win10 accept Linux NCM gadget ethernet
> > > > 
> > > >  drivers/usb/gadget/function/f_ncm.c | 47 +++++++++++++++++++++++++++--
> > > >  drivers/usb/gadget/function/u_ncm.h |  3 ++
> > > >  2 files changed, 47 insertions(+), 3 deletions(-)
> > 
> > What is the git commit id of this change in Linus's tree?
> > 
> > thanks,
> > 
> > greg k-h
> For this patch:
> 	793409292382027226769d0299987f06cbd97a6e
> 
> and for "usb: gadget: f_ncm: Fix NTP-32 support"
> 	550eef0c353030ac4223b9c9479bdf77a05445d6

Ah, yeah, they did get lost in the deluge, sorry.

Can you please resend these _with_ the git commit id in the message so
that we know what is going on?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCHv2 1/2] usb: gadget: f_ncm: Add OS descriptor support
  2023-06-18 11:04       ` gregkh
@ 2023-06-18 11:40         ` Joakim Tjernlund
  2023-06-18 11:40           ` [PATCHv2 2/2] usb: gadget: f_ncm: Fix NTP-32 support Joakim Tjernlund
  2023-06-18 11:42         ` [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support Joakim Tjernlund
  1 sibling, 1 reply; 9+ messages in thread
From: Joakim Tjernlund @ 2023-06-18 11:40 UTC (permalink / raw)
  To: stable @ vger . kernel . org, gregkh
  Cc: Romain Izard, Felipe Balbi, Joakim Tjernlund

From: Romain Izard <romain.izard.pro@gmail.com>

upstream commit: 793409292382027226769d0299987f06cbd97a6e

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 <romain.izard.pro@gmail.com>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
Cc: stable@vger.kernel.org # v4.19
---

 Seems to have been forgotten when backporting NCM fixes.
 Needed to make Win10 accept Linux NCM gadget ethernet

 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 e4aa370e86a9..b4571633f7b5 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.
@@ -1432,6 +1433,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,
@@ -1445,13 +1456,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;
@@ -1468,6 +1481,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;
@@ -1547,6 +1564,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);
@@ -1601,16 +1621,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();
@@ -1619,8 +1645,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;
 }
@@ -1646,6 +1684,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 67324f983343..dfd75ad61b3f 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.
 	 *
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCHv2 2/2] usb: gadget: f_ncm: Fix NTP-32 support
  2023-06-18 11:40         ` [PATCHv2 " Joakim Tjernlund
@ 2023-06-18 11:40           ` Joakim Tjernlund
  0 siblings, 0 replies; 9+ messages in thread
From: Joakim Tjernlund @ 2023-06-18 11:40 UTC (permalink / raw)
  To: stable @ vger . kernel . org, gregkh
  Cc: Romain Izard, Felipe Balbi, Joakim Tjernlund

From: Romain Izard <romain.izard.pro@gmail.com>

upstream commit: 550eef0c353030ac4223b9c9479bdf77a05445d6

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 <romain.izard.pro@gmail.com>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
Cc: stable@vger.kernel.org # v4.19
---

 Seems to have been forgotten when backporting NCM fixes.
 Needed to make Win10 accept Linux NCM gadget ethernet

 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 b4571633f7b5..92a7c3a83945 100644
--- a/drivers/usb/gadget/function/f_ncm.c
+++ b/drivers/usb/gadget/function/f_ncm.c
@@ -36,9 +36,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 */
@@ -530,6 +528,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 */
@@ -812,25 +811,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;
 	}
@@ -847,6 +841,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) {
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support
  2023-06-18 11:04       ` gregkh
  2023-06-18 11:40         ` [PATCHv2 " Joakim Tjernlund
@ 2023-06-18 11:42         ` Joakim Tjernlund
  2023-06-18 12:55           ` gregkh
  1 sibling, 1 reply; 9+ messages in thread
From: Joakim Tjernlund @ 2023-06-18 11:42 UTC (permalink / raw)
  To: gregkh@linuxfoundation.org
  Cc: felipe.balbi@linux.intel.com, stable@vger.kernel.org,
	romain.izard.pro@gmail.com

On Sun, 2023-06-18 at 13:04 +0200, gregkh@linuxfoundation.org wrote:
> On Sun, Jun 18, 2023 at 09:58:14AM +0000, Joakim Tjernlund wrote:
> > On Sun, 2023-06-18 at 09:36 +0200, Greg KH wrote:
> > > On Sat, Jun 17, 2023 at 04:03:06PM +0000, Joakim Tjernlund wrote:
> > > > Ping ?
> > > > 
> > > > Did I do something wrong with submission or is it queued for later ?
> > > > 4.19 is missing these which make USB NCM unusable with Win >= 10. 
> > > > 
> > > >  Jocke
> > > > 
> > > > On Wed, 2023-05-31 at 19:33 +0200, Joakim Tjernlund wrote:
> > > > > From: Romain Izard <romain.izard.pro@gmail.com>
> > > > > 
> > > > > 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 <romain.izard.pro@gmail.com>
> > > > > Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
> > > > > Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
> > > > > Cc: stable@vger.kernel.org # v4.19
> > > > > ---
> > > > > 
> > > > >  Seems to have been forgotten when backporting NCM fixes.
> > > > >  Needed to make Win10 accept Linux NCM gadget ethernet
> > > > > 
> > > > >  drivers/usb/gadget/function/f_ncm.c | 47 +++++++++++++++++++++++++++--
> > > > >  drivers/usb/gadget/function/u_ncm.h |  3 ++
> > > > >  2 files changed, 47 insertions(+), 3 deletions(-)
> > > 
> > > What is the git commit id of this change in Linus's tree?
> > > 
> > > thanks,
> > > 
> > > greg k-h
> > For this patch:
> > 	793409292382027226769d0299987f06cbd97a6e
> > 
> > and for "usb: gadget: f_ncm: Fix NTP-32 support"
> > 	550eef0c353030ac4223b9c9479bdf77a05445d6
> 
> Ah, yeah, they did get lost in the deluge, sorry.
> 
> Can you please resend these _with_ the git commit id in the message so
> that we know what is going on?
> 
> thanks,
> 
> greg k-h

Resent as PATCHv2 with commit id's

  Jocke

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support
  2023-06-18 11:42         ` [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support Joakim Tjernlund
@ 2023-06-18 12:55           ` gregkh
  0 siblings, 0 replies; 9+ messages in thread
From: gregkh @ 2023-06-18 12:55 UTC (permalink / raw)
  To: Joakim Tjernlund
  Cc: felipe.balbi@linux.intel.com, stable@vger.kernel.org,
	romain.izard.pro@gmail.com

On Sun, Jun 18, 2023 at 11:42:59AM +0000, Joakim Tjernlund wrote:
> On Sun, 2023-06-18 at 13:04 +0200, gregkh@linuxfoundation.org wrote:
> > On Sun, Jun 18, 2023 at 09:58:14AM +0000, Joakim Tjernlund wrote:
> > > On Sun, 2023-06-18 at 09:36 +0200, Greg KH wrote:
> > > > On Sat, Jun 17, 2023 at 04:03:06PM +0000, Joakim Tjernlund wrote:
> > > > > Ping ?
> > > > > 
> > > > > Did I do something wrong with submission or is it queued for later ?
> > > > > 4.19 is missing these which make USB NCM unusable with Win >= 10. 
> > > > > 
> > > > >  Jocke
> > > > > 
> > > > > On Wed, 2023-05-31 at 19:33 +0200, Joakim Tjernlund wrote:
> > > > > > From: Romain Izard <romain.izard.pro@gmail.com>
> > > > > > 
> > > > > > 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 <romain.izard.pro@gmail.com>
> > > > > > Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
> > > > > > Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
> > > > > > Cc: stable@vger.kernel.org # v4.19
> > > > > > ---
> > > > > > 
> > > > > >  Seems to have been forgotten when backporting NCM fixes.
> > > > > >  Needed to make Win10 accept Linux NCM gadget ethernet
> > > > > > 
> > > > > >  drivers/usb/gadget/function/f_ncm.c | 47 +++++++++++++++++++++++++++--
> > > > > >  drivers/usb/gadget/function/u_ncm.h |  3 ++
> > > > > >  2 files changed, 47 insertions(+), 3 deletions(-)
> > > > 
> > > > What is the git commit id of this change in Linus's tree?
> > > > 
> > > > thanks,
> > > > 
> > > > greg k-h
> > > For this patch:
> > > 	793409292382027226769d0299987f06cbd97a6e
> > > 
> > > and for "usb: gadget: f_ncm: Fix NTP-32 support"
> > > 	550eef0c353030ac4223b9c9479bdf77a05445d6
> > 
> > Ah, yeah, they did get lost in the deluge, sorry.
> > 
> > Can you please resend these _with_ the git commit id in the message so
> > that we know what is going on?
> > 
> > thanks,
> > 
> > greg k-h
> 
> Resent as PATCHv2 with commit id's

Both now queued up, thanks.

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-06-18 12:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-31 17:33 [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support Joakim Tjernlund
2023-06-17 16:03 ` Joakim Tjernlund
2023-06-18  7:36   ` Greg KH
2023-06-18  9:58     ` Joakim Tjernlund
2023-06-18 11:04       ` gregkh
2023-06-18 11:40         ` [PATCHv2 " Joakim Tjernlund
2023-06-18 11:40           ` [PATCHv2 2/2] usb: gadget: f_ncm: Fix NTP-32 support Joakim Tjernlund
2023-06-18 11:42         ` [PATCH 1/2] usb: gadget: f_ncm: Add OS descriptor support Joakim Tjernlund
2023-06-18 12:55           ` gregkh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).