All of lore.kernel.org
 help / color / mirror / Atom feed
From: Enrico Mioso <mrkiko.rs@gmail.com>
To: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>
Cc: netdev@vger.kernel.org
Subject: [PATCH V3 2/2] Introduce huawei_cdc_ncm driver
Date: Wed, 3 Jul 2013 15:38:43 +0200 (CEST)	[thread overview]
Message-ID: <alpine.LNX.2.02.1307031534180.25440@eeeadesso> (raw)
In-Reply-To: <51D424D1.2010507@cogentembedded.com>

This new driver, handles devices that mimic the NCM standard, but using NCM as
a transport layer to encapsulate other protocols (i.e. AT protocol).

Signed-off-by: Enrico Mioso <mrkiko.rs@gmail.com>
V3:
  - fixed typo in Kconfig (help text)

diff --git a/drivers/net/usb/Kconfig b/drivers/net/usb/Kconfig
index 287cc62..33cb175 100644
--- a/drivers/net/usb/Kconfig
+++ b/drivers/net/usb/Kconfig
@@ -246,6 +246,18 @@ config USB_NET_CDC_NCM
 	    * ST-Ericsson M343 HSPA Mobile Broadband Modem (reference design)
 	    * Ericsson F5521gw Mobile Broadband Module
 
+config USB_NET_HUAWEI_CDC_NCM
+	tristate "Huawei-style CDC NCM support"
+	depends on USB_USBNET
+	default y
+	select USB_WDM
+	select USB_NET_CDC_NCM
+	help
+		This driver aims to support huawei-style NCM devices, that use NCM as a 
+		transport for other protocols.
+		To compile this driver as a module, choose M here: the module will be 
+		called huawei_cdc_ncm.
+
 config USB_NET_CDC_MBIM
 	tristate "CDC MBIM support"
 	depends on USB_USBNET
diff --git a/drivers/net/usb/Makefile b/drivers/net/usb/Makefile
index 9ab5c9d..12a0279 100644
--- a/drivers/net/usb/Makefile
+++ b/drivers/net/usb/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_USB_IPHETH)	+= ipheth.o
 obj-$(CONFIG_USB_SIERRA_NET)	+= sierra_net.o
 obj-$(CONFIG_USB_NET_CX82310_ETH)	+= cx82310_eth.o
 obj-$(CONFIG_USB_NET_CDC_NCM)	+= cdc_ncm.o
+obj-$(CONFIG_USB_NET_HUAWEI_CDC_NCM)	+= huawei_cdc_ncm.o
 obj-$(CONFIG_USB_VL600)		+= lg-vl600.o
 obj-$(CONFIG_USB_NET_QMI_WWAN)	+= qmi_wwan.o
 obj-$(CONFIG_USB_NET_CDC_MBIM)	+= cdc_mbim.o
diff --git a/drivers/net/usb/huawei_cdc_ncm.c b/drivers/net/usb/huawei_cdc_ncm.c
index e69de29..46591e7 100644
--- a/drivers/net/usb/huawei_cdc_ncm.c
+++ b/drivers/net/usb/huawei_cdc_ncm.c
@@ -0,0 +1,206 @@
+/*
+ * huawei_cdc_ncm.c - handles hawei-style NCM devices.
+ * Copyright (C) 1012	 Enrico Mioso <mrkiko.rs@gmail.com>
+ * This driver handles devices resembling the CDC NCM standard, but 
+ * encapsulating another protocol inside it. An example are some Huawei 3G devices.
+ * This code has been heavily inspired by the cdc_mbim.c driver, which is
+ * Copyright (c) 2012  Smith Micro Software, Inc. 
+ * Copyright (c) 2012  Bj??rn Mork <bjorn@mork.no>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/ethtool.h>
+#include <linux/if_vlan.h>
+#include <linux/ip.h>
+#include <linux/mii.h>
+#include <linux/usb.h>
+#include <linux/usb/cdc.h>
+#include <linux/usb/usbnet.h>
+#include <linux/usb/cdc-wdm.h>
+#include <linux/usb/cdc_ncm.h>
+
+/* Specific driver data */
+struct huawei_cdc_ncm_state {
+	struct cdc_ncm_ctx *ctx;
+	atomic_t pmcount;
+	struct usb_driver *subdriver;
+	struct usb_interface *control;
+	struct usb_interface *data;
+};
+
+static int huawei_cdc_ncm_manage_power(struct usbnet *usbnet_dev, int on)
+{
+	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	int rv = 0;
+
+	if ((on && atomic_add_return(1, &drvstate->pmcount) == 1) || (!on && atomic_dec_and_test(&drvstate->pmcount))) {
+		rv = usb_autopm_get_interface(usbnet_dev->intf);
+		if (rv < 0)
+			goto err;
+		usbnet_dev->intf->needs_remote_wakeup = on;
+		usb_autopm_put_interface(usbnet_dev->intf);
+	}
+err:
+	return rv;
+}
+
+static int huawei_cdc_ncm_wdm_manage_power(struct usb_interface *intf, int status)
+{
+	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
+
+	/* can be called while disconnecting */
+	if (!usbnet_dev)
+		return 0;
+
+	return huawei_cdc_ncm_manage_power(usbnet_dev, status);
+}
+
+
+static int huawei_cdc_ncm_bind(struct usbnet *usbnet_dev, struct usb_interface *intf) {
+	struct cdc_ncm_ctx *ctx;
+	struct usb_driver *subdriver = ERR_PTR(-ENODEV);
+	int ret = -ENODEV;
+	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	
+	/* Actually binds us to the device: use standard ncm function */
+	ret = cdc_ncm_bind_common(usbnet_dev, intf, 1); /* altsetting should be 1 */
+	if (ret)
+		goto err;
+	
+	ctx = drvstate->ctx;
+	
+	if (usbnet_dev->status) 
+	/* The wdm buffer size is choosen to match the one specified in AT R1.1 Standard. */	
+	subdriver = usb_cdc_wdm_register(ctx->control, &usbnet_dev->status->desc, 256, huawei_cdc_ncm_wdm_manage_power); /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256" */
+	if (IS_ERR(subdriver)) {
+	 ret = PTR_ERR(subdriver);
+	 cdc_ncm_unbind(usbnet_dev, intf);
+   goto err;
+	}
+	
+	/* Prevent usbnet from using the status descriptor */
+	usbnet_dev->status = NULL;
+	
+	drvstate->subdriver = subdriver;
+	
+err:
+	return ret;
+}
+
+static void huawei_cdc_ncm_unbind(struct usbnet *usbnet_dev, struct usb_interface *intf) {
+	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct cdc_ncm_ctx *ctx = drvstate->ctx;
+	
+	if (drvstate->subdriver && drvstate->subdriver->disconnect)
+		drvstate->subdriver->disconnect(ctx->control);
+	drvstate->subdriver = NULL;
+
+	cdc_ncm_unbind(usbnet_dev, intf);
+}
+
+static int huawei_cdc_ncm_suspend(struct usb_interface *intf, pm_message_t message)
+{
+	int ret = 0;
+	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
+	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct cdc_ncm_ctx *ctx = drvstate->ctx;
+
+	if (ctx == NULL) {
+		ret = -1;
+		goto error;
+	}
+
+	ret = usbnet_suspend(intf, message);
+	if (ret < 0)
+		goto error;
+
+	if (intf == ctx->control && drvstate->subdriver && drvstate->subdriver->suspend)
+		ret = drvstate->subdriver->suspend(intf, message);
+	if (ret < 0)
+		usbnet_resume(intf);
+
+error:
+	return ret;
+}
+
+static int huawei_cdc_ncm_resume(struct usb_interface *intf)
+{
+	int ret = 0;
+	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
+	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct cdc_ncm_ctx *ctx = drvstate->ctx;
+	bool callsub = (intf == ctx->control && drvstate->subdriver && drvstate->subdriver->resume); /* should we call subdriver's resume? ? */
+
+	if (callsub)
+		ret = drvstate->subdriver->resume(intf);
+	if (ret < 0)
+		goto err;
+	ret = usbnet_resume(intf);
+	if (ret < 0 && callsub && drvstate->subdriver->suspend)
+		drvstate->subdriver->suspend(intf, PMSG_SUSPEND);
+err:
+	return ret;
+}
+
+static int huawei_cdc_ncm_check_connect(struct usbnet *usbnet_dev) {
+	struct cdc_ncm_ctx *ctx;
+	
+	ctx = (struct cdc_ncm_ctx *)usbnet_dev->data[0];
+	
+	if (ctx == NULL)
+		return 1; /* disconnected */
+	
+	return !ctx->connected;
+}
+
+static const struct driver_info huawei_cdc_ncm_info = {
+	.description = "Huawei CDC NCM",
+	.flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
+	.bind = huawei_cdc_ncm_bind,
+	.unbind = huawei_cdc_ncm_unbind,
+	.check_connect = huawei_cdc_ncm_check_connect,
+	.manage_power = huawei_cdc_ncm_manage_power,
+	.rx_fixup = cdc_ncm_rx_fixup,
+	.tx_fixup = cdc_ncm_tx_fixup,
+};
+
+static const struct usb_device_id huawei_cdc_ncm_devs[] = {
+	/* Huawei NCM devices disguised as vendor specific */
+	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
+	  .driver_info = (unsigned long)&huawei_cdc_ncm_info,
+	},
+	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
+	  .driver_info = (unsigned long)&huawei_cdc_ncm_info,
+	},
+	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
+	  .driver_info = (unsigned long)&huawei_cdc_ncm_info,
+	},
+
+	/* Terminating entry */
+	{
+	},
+};
+MODULE_DEVICE_TABLE(usb, huawei_cdc_ncm_devs);
+
+
+/* USB driver struct - used by USB stack */
+static struct usb_driver huawei_cdc_ncm_driver = {
+	.name = "huawei_cdc_ncm",
+	.id_table = huawei_cdc_ncm_devs,
+	.probe = usbnet_probe,
+	.disconnect = usbnet_disconnect,
+	.suspend = huawei_cdc_ncm_suspend,
+	.resume = huawei_cdc_ncm_resume,
+	.reset_resume = huawei_cdc_ncm_resume,
+	.supports_autosuspend = 1,
+	.disable_hub_initiated_lpm = 1,
+};
+module_usb_driver(huawei_cdc_ncm_driver);
+MODULE_AUTHOR("Enrico Mioso <mrkiko.rs@gmail.com>");
+MODULE_DESCRIPTION("USB CDC NCM host driver with encapsulated protocol support");
+MODULE_LICENSE("GPL");

  parent reply	other threads:[~2013-07-03 13:38 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-02 10:43 [PATCH RFC] cdc_ncm.c: make rx and tx functions exportable Enrico Mioso
2013-07-02 12:01 ` Bjørn Mork
2013-07-02 12:05   ` Enrico Mioso
2013-07-02 17:04   ` [RFC] huawei_cdc_ncm driver - status Enrico Mioso
2013-07-02 19:06     ` Enrico Mioso
2013-07-02 20:06     ` [PATCH 0/3] make cdc_ncm_tx_fixup and cdc_ncm_rx_fixup non-static functions Enrico Mioso
2013-07-02 20:10       ` [PATCH 1/3] Export cdc_ncm_{tx,rx}_fixup functions Enrico Mioso
2013-07-02 20:13         ` [PATCH RFC 2/3] Introduce huawei_cdc_ncm driver Enrico Mioso
2013-07-02 20:15           ` [PATCH RFC 3/3] huawei_cdc_ncm base skeleton Enrico Mioso
2013-07-03  6:40             ` Huaei E3131 - connected! Enrico Mioso
2013-07-03  7:11               ` Huaei E3131 - status Enrico Mioso
2013-07-03  8:15                 ` Bjørn Mork
2013-07-03 12:55                   ` [PATCH V2 1/3] usbnet: cdc_ncm: let cdc_ncm_tx_fixup and cdc_ncm_rx_fixup be non-static Enrico Mioso
2013-07-03 13:00                   ` [PATCH V2 2/3] usbnet: cdc_ncm: export cdc_ncm_tx_fixup and cdc_ncm_rx_fixup Enrico Mioso
2013-07-03 13:19                     ` Sergei Shtylyov
2013-07-03 13:22                       ` Enrico Mioso
2013-07-03 13:33                       ` [PATCH V3 1/2] Export cdc_ncm_{tx,rx]_fixup functions Enrico Mioso
2013-07-03 13:38                       ` Enrico Mioso [this message]
2013-07-03 16:18                         ` [PATCH V3 2/2] Introduce huawei_cdc_ncm driver Ben Hutchings
2013-07-03 18:11                           ` [PATCH V3 2/2 RESEND] " Enrico Mioso
2013-07-04 13:19                             ` Bjørn Mork
2013-07-04 17:03                               ` Enrico Mioso
2013-07-03 18:11                           ` [PATCH V3 2/2] " Enrico Mioso
2013-07-03 13:05                   ` [PATCH V2 3/3] usbnet: introduce " Enrico Mioso
2013-07-03 13:13                   ` [PATCH] usbnet: cdc_ncm: remove huawei devices handled by cdc_ncm_huawei.c Enrico Mioso
2013-07-03  7:13               ` Huaei E3131 - connected! Bjørn Mork
2013-07-03  7:38             ` [PATCH RFC 3/3] huawei_cdc_ncm base skeleton Bjørn Mork
2013-07-03 10:16               ` Enrico Mioso
2013-07-03  7:40           ` [PATCH RFC 2/3] Introduce huawei_cdc_ncm driver Bjørn Mork
2013-07-03  7:43         ` [PATCH 1/3] Export cdc_ncm_{tx,rx}_fixup functions Bjørn Mork
2013-07-03 10:19           ` Enrico Mioso
2013-07-03  7:45       ` [PATCH 0/3] make cdc_ncm_tx_fixup and cdc_ncm_rx_fixup non-static functions Bjørn Mork
2013-07-10 12:27   ` [PATCH 1/3] net: cdc_ncm: Export cdc_ncm_{tx,rx}_fixup functions for re-use Enrico Mioso
     [not found]     ` <0cbf7c81-182e-493c-9c28-5b78160a08f1@email.android.com>
     [not found]       ` <alpine.LNX.2.02.1307170457450.1161@eeeadesso>
     [not found]         ` <87bo4xx3ef.fsf@nemi.mork.no>
2013-08-16 13:39           ` [PATCH] " Enrico Mioso
2013-08-16 13:39             ` Enrico Mioso
2013-08-16 13:49             ` Greg Kroah-Hartman
2013-08-16 13:51               ` Enrico Mioso
2013-08-16 13:52               ` Greg Kroah-Hartman
2013-08-16 13:44           ` [PATCH] net: huawei_cdc_ncm: Introduce new huawei_cdc_ncm driver Enrico Mioso
2013-07-10 12:28   ` [PATCH 2/3] net: huawei_cdc_ncm: Introduce the " Enrico Mioso
2013-07-10 12:30   ` [PATCH 3/3] net: cdc_ncm: remove Huawei non-standard NCM device IDs Enrico Mioso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.02.1307031534180.25440@eeeadesso \
    --to=mrkiko.rs@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=sergei.shtylyov@cogentembedded.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.