1/* huawei_cdc_ncm.c - handles Huawei devices using the CDC NCM protocol as
2 * transport layer.
3 * Copyright (C) 2013	 Enrico Mioso <mrkiko.rs@gmail.com>
4 *
5 *
6 * ABSTRACT:
7 * This driver handles devices resembling the CDC NCM standard, but
8 * encapsulating another protocol inside it. An example are some Huawei 3G
9 * devices, exposing an embedded AT channel where you can set up the NCM
10 * connection.
11 * This code has been heavily inspired by the cdc_mbim.c driver, which is
12 * Copyright (c) 2012  Smith Micro Software, Inc.
13 * Copyright (c) 2012  Bjørn Mork <bjorn@mork.no>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 */
19
20#include <linux/module.h>
21#include <linux/netdevice.h>
22#include <linux/ethtool.h>
23#include <linux/if_vlan.h>
24#include <linux/ip.h>
25#include <linux/mii.h>
26#include <linux/usb.h>
27#include <linux/usb/cdc.h>
28#include <linux/usb/usbnet.h>
29#include <linux/usb/cdc-wdm.h>
30#include <linux/usb/cdc_ncm.h>
31
32/* Driver data */
33struct huawei_cdc_ncm_state {
34	struct cdc_ncm_ctx *ctx;
35	atomic_t pmcount;
36	struct usb_driver *subdriver;
37	struct usb_interface *control;
38	struct usb_interface *data;
39};
40
41static int huawei_cdc_ncm_manage_power(struct usbnet *usbnet_dev, int on)
42{
43	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
44	int rv;
45
46	if ((on && atomic_add_return(1, &drvstate->pmcount) == 1) ||
47			(!on && atomic_dec_and_test(&drvstate->pmcount))) {
48		rv = usb_autopm_get_interface(usbnet_dev->intf);
49		usbnet_dev->intf->needs_remote_wakeup = on;
50		if (!rv)
51			usb_autopm_put_interface(usbnet_dev->intf);
52	}
53	return 0;
54}
55
56static int huawei_cdc_ncm_wdm_manage_power(struct usb_interface *intf,
57					   int status)
58{
59	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
60
61	/* can be called while disconnecting */
62	if (!usbnet_dev)
63		return 0;
64
65	return huawei_cdc_ncm_manage_power(usbnet_dev, status);
66}
67
68
69static int huawei_cdc_ncm_bind(struct usbnet *usbnet_dev,
70			       struct usb_interface *intf)
71{
72	struct cdc_ncm_ctx *ctx;
73	struct usb_driver *subdriver = ERR_PTR(-ENODEV);
74	int ret = -ENODEV;
75	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
76
77	/* altsetting should always be 1 for NCM devices - so we hard-coded
78	 * it here
79	 */
80	ret = cdc_ncm_bind_common(usbnet_dev, intf, 1);
81	if (ret)
82		goto err;
83
84	ctx = drvstate->ctx;
85
86	if (usbnet_dev->status)
87		/* The wMaxCommand buffer must be big enough to hold
88		 * any message from the modem. Experience has shown
89		 * that some replies are more than 256 bytes long
90		 */
91		subdriver = usb_cdc_wdm_register(ctx->control,
92						 &usbnet_dev->status->desc,
93						 1024, /* wMaxCommand */
94						 huawei_cdc_ncm_wdm_manage_power);
95	if (IS_ERR(subdriver)) {
96		ret = PTR_ERR(subdriver);
97		cdc_ncm_unbind(usbnet_dev, intf);
98		goto err;
99	}
100
101	/* Prevent usbnet from using the status descriptor */
102	usbnet_dev->status = NULL;
103
104	drvstate->subdriver = subdriver;
105
106err:
107	return ret;
108}
109
110static void huawei_cdc_ncm_unbind(struct usbnet *usbnet_dev,
111				  struct usb_interface *intf)
112{
113	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
114	struct cdc_ncm_ctx *ctx = drvstate->ctx;
115
116	if (drvstate->subdriver && drvstate->subdriver->disconnect)
117		drvstate->subdriver->disconnect(ctx->control);
118	drvstate->subdriver = NULL;
119
120	cdc_ncm_unbind(usbnet_dev, intf);
121}
122
123static int huawei_cdc_ncm_suspend(struct usb_interface *intf,
124				  pm_message_t message)
125{
126	int ret = 0;
127	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
128	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
129	struct cdc_ncm_ctx *ctx = drvstate->ctx;
130
131	if (ctx == NULL) {
132		ret = -ENODEV;
133		goto error;
134	}
135
136	ret = usbnet_suspend(intf, message);
137	if (ret < 0)
138		goto error;
139
140	if (intf == ctx->control &&
141		drvstate->subdriver &&
142		drvstate->subdriver->suspend)
143		ret = drvstate->subdriver->suspend(intf, message);
144	if (ret < 0)
145		usbnet_resume(intf);
146
147error:
148	return ret;
149}
150
151static int huawei_cdc_ncm_resume(struct usb_interface *intf)
152{
153	int ret = 0;
154	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
155	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
156	bool callsub;
157	struct cdc_ncm_ctx *ctx = drvstate->ctx;
158
159	/* should we call subdriver's resume function? */
160	callsub =
161		(intf == ctx->control &&
162		drvstate->subdriver &&
163		drvstate->subdriver->resume);
164
165	if (callsub)
166		ret = drvstate->subdriver->resume(intf);
167	if (ret < 0)
168		goto err;
169	ret = usbnet_resume(intf);
170	if (ret < 0 && callsub)
171		drvstate->subdriver->suspend(intf, PMSG_SUSPEND);
172err:
173	return ret;
174}
175
176static const struct driver_info huawei_cdc_ncm_info = {
177	.description = "Huawei CDC NCM device",
178	.flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_WWAN,
179	.bind = huawei_cdc_ncm_bind,
180	.unbind = huawei_cdc_ncm_unbind,
181	.manage_power = huawei_cdc_ncm_manage_power,
182	.rx_fixup = cdc_ncm_rx_fixup,
183	.tx_fixup = cdc_ncm_tx_fixup,
184};
185
186static const struct usb_device_id huawei_cdc_ncm_devs[] = {
187	/* Huawei NCM devices disguised as vendor specific */
188	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
189	  .driver_info = (unsigned long)&huawei_cdc_ncm_info,
190	},
191	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
192	  .driver_info = (unsigned long)&huawei_cdc_ncm_info,
193	},
194	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
195	  .driver_info = (unsigned long)&huawei_cdc_ncm_info,
196	},
197	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x03, 0x16),
198	  .driver_info = (unsigned long)&huawei_cdc_ncm_info,
199	},
200
201	/* Terminating entry */
202	{
203	},
204};
205MODULE_DEVICE_TABLE(usb, huawei_cdc_ncm_devs);
206
207static struct usb_driver huawei_cdc_ncm_driver = {
208	.name = "huawei_cdc_ncm",
209	.id_table = huawei_cdc_ncm_devs,
210	.probe = usbnet_probe,
211	.disconnect = usbnet_disconnect,
212	.suspend = huawei_cdc_ncm_suspend,
213	.resume = huawei_cdc_ncm_resume,
214	.reset_resume = huawei_cdc_ncm_resume,
215	.supports_autosuspend = 1,
216	.disable_hub_initiated_lpm = 1,
217};
218module_usb_driver(huawei_cdc_ncm_driver);
219MODULE_AUTHOR("Enrico Mioso <mrkiko.rs@gmail.com>");
220MODULE_DESCRIPTION("USB CDC NCM host driver with encapsulated protocol support");
221MODULE_LICENSE("GPL");
222