blob: bfab8796730f36af3ae1c9c4ed12b81fec192592 [file] [log] [blame]
Alexey Orishko900d4952010-11-29 23:23:28 +00001/*
2 * cdc_ncm.c
3 *
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00004 * Copyright (C) ST-Ericsson 2010-2012
Alexey Orishko900d4952010-11-29 23:23:28 +00005 * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
6 * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
7 *
8 * USB Host Driver for Network Control Model (NCM)
9 * http://www.usb.org/developers/devclass_docs/NCM10.zip
10 *
11 * The NCM encoding, decoding and initialization logic
12 * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
13 *
14 * This software is available to you under a choice of one of two
15 * licenses. You may choose this file to be licensed under the terms
16 * of the GNU General Public License (GPL) Version 2 or the 2-clause
17 * BSD license listed below:
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#include <linux/module.h>
42#include <linux/init.h>
43#include <linux/netdevice.h>
44#include <linux/ctype.h>
45#include <linux/ethtool.h>
46#include <linux/workqueue.h>
47#include <linux/mii.h>
48#include <linux/crc32.h>
49#include <linux/usb.h>
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000050#include <linux/hrtimer.h>
Alexey Orishko900d4952010-11-29 23:23:28 +000051#include <linux/atomic.h>
52#include <linux/usb/usbnet.h>
53#include <linux/usb/cdc.h>
Bjørn Morkc91ce3b2012-10-22 10:56:35 +000054#include <linux/usb/cdc_ncm.h>
Alexey Orishko900d4952010-11-29 23:23:28 +000055
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000056#define DRIVER_VERSION "14-Mar-2012"
Alexey Orishko900d4952010-11-29 23:23:28 +000057
Bjørn Mork1e8bbe62013-03-14 01:05:13 +000058#if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
59static bool prefer_mbim = true;
60#else
61static bool prefer_mbim;
62#endif
63module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
64MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
65
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000066static void cdc_ncm_txpath_bh(unsigned long param);
67static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
68static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +000069static struct usb_driver cdc_ncm_driver;
Alexey Orishko900d4952010-11-29 23:23:28 +000070
71static void
72cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
73{
74 struct usbnet *dev = netdev_priv(net);
75
Jiri Pirko7826d432013-01-06 00:44:26 +000076 strlcpy(info->driver, dev->driver_name, sizeof(info->driver));
77 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
78 strlcpy(info->fw_version, dev->driver_info->description,
Alexey Orishko900d4952010-11-29 23:23:28 +000079 sizeof(info->fw_version));
80 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
81}
82
Alexey Orishko900d4952010-11-29 23:23:28 +000083static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
84{
Alexey Orishko900d4952010-11-29 23:23:28 +000085 u32 val;
Alexey Orishko900d4952010-11-29 23:23:28 +000086 u8 flags;
87 u8 iface_no;
88 int err;
Greg Suarez2d1c4312012-10-22 10:56:30 +000089 int eth_hlen;
Alexey Orishko84e77a82011-02-07 09:45:10 +000090 u16 ntb_fmt_supported;
Greg Suarez2d1c4312012-10-22 10:56:30 +000091 u32 min_dgram_size;
92 u32 min_hdr_size;
Ming Lei90b8b032012-10-24 19:46:56 +000093 struct usbnet *dev = netdev_priv(ctx->netdev);
Alexey Orishko900d4952010-11-29 23:23:28 +000094
95 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
96
Ming Lei90b8b032012-10-24 19:46:56 +000097 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
98 USB_TYPE_CLASS | USB_DIR_IN
99 |USB_RECIP_INTERFACE,
100 0, iface_no, &ctx->ncm_parm,
101 sizeof(ctx->ncm_parm));
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000102 if (err < 0) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000103 pr_debug("failed GET_NTB_PARAMETERS\n");
104 return 1;
105 }
106
107 /* read correct set of parameters according to device mode */
108 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
109 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
110 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
111 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
112 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000113 /* devices prior to NCM Errata shall set this field to zero */
114 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
115 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
Alexey Orishko900d4952010-11-29 23:23:28 +0000116
Greg Suarez2d1c4312012-10-22 10:56:30 +0000117 eth_hlen = ETH_HLEN;
118 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
119 min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
120 if (ctx->mbim_desc != NULL) {
121 flags = ctx->mbim_desc->bmNetworkCapabilities;
122 eth_hlen = 0;
123 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
124 min_hdr_size = 0;
125 } else if (ctx->func_desc != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000126 flags = ctx->func_desc->bmNetworkCapabilities;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000127 } else {
Alexey Orishko900d4952010-11-29 23:23:28 +0000128 flags = 0;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000129 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000130
131 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
132 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
Alexey Orishko84e77a82011-02-07 09:45:10 +0000133 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
Alexey Orishko900d4952010-11-29 23:23:28 +0000134 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
Alexey Orishko84e77a82011-02-07 09:45:10 +0000135 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
Alexey Orishko900d4952010-11-29 23:23:28 +0000136
Alexey Orishko84e77a82011-02-07 09:45:10 +0000137 /* max count of tx datagrams */
138 if ((ctx->tx_max_datagrams == 0) ||
139 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
140 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
Alexey Orishko900d4952010-11-29 23:23:28 +0000141
142 /* verify maximum size of received NTB in bytes */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000143 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
144 pr_debug("Using min receive length=%d\n",
145 USB_CDC_NCM_NTB_MIN_IN_SIZE);
146 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
147 }
148
149 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000150 pr_debug("Using default maximum receive length=%d\n",
151 CDC_NCM_NTB_MAX_SIZE_RX);
152 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
153 }
154
Alexey Orishko84e77a82011-02-07 09:45:10 +0000155 /* inform device about NTB input size changes */
156 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
Ming Lei90b8b032012-10-24 19:46:56 +0000157 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000158
Ming Lei90b8b032012-10-24 19:46:56 +0000159 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
160 USB_TYPE_CLASS | USB_DIR_OUT
161 | USB_RECIP_INTERFACE,
162 0, iface_no, &dwNtbInMaxSize, 4);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000163 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000164 pr_debug("Setting NTB Input Size failed\n");
165 }
166
Alexey Orishko900d4952010-11-29 23:23:28 +0000167 /* verify maximum size of transmitted NTB in bytes */
168 if ((ctx->tx_max <
Greg Suarez2d1c4312012-10-22 10:56:30 +0000169 (min_hdr_size + min_dgram_size)) ||
Alexey Orishko900d4952010-11-29 23:23:28 +0000170 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
171 pr_debug("Using default maximum transmit length=%d\n",
172 CDC_NCM_NTB_MAX_SIZE_TX);
173 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
174 }
175
176 /*
177 * verify that the structure alignment is:
178 * - power of two
179 * - not greater than the maximum transmit length
180 * - not less than four bytes
181 */
182 val = ctx->tx_ndp_modulus;
183
184 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
185 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
186 pr_debug("Using default alignment: 4 bytes\n");
187 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
188 }
189
190 /*
191 * verify that the payload alignment is:
192 * - power of two
193 * - not greater than the maximum transmit length
194 * - not less than four bytes
195 */
196 val = ctx->tx_modulus;
197
198 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
199 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
200 pr_debug("Using default transmit modulus: 4 bytes\n");
201 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
202 }
203
204 /* verify the payload remainder */
205 if (ctx->tx_remainder >= ctx->tx_modulus) {
206 pr_debug("Using default transmit remainder: 0 bytes\n");
207 ctx->tx_remainder = 0;
208 }
209
210 /* adjust TX-remainder according to NCM specification. */
Greg Suarez2d1c4312012-10-22 10:56:30 +0000211 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
212 (ctx->tx_modulus - 1));
Alexey Orishko900d4952010-11-29 23:23:28 +0000213
214 /* additional configuration */
215
216 /* set CRC Mode */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000217 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
Ming Lei90b8b032012-10-24 19:46:56 +0000218 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
219 USB_TYPE_CLASS | USB_DIR_OUT
220 | USB_RECIP_INTERFACE,
221 USB_CDC_NCM_CRC_NOT_APPENDED,
222 iface_no, NULL, 0);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000223 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000224 pr_debug("Setting CRC mode off failed\n");
225 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000226
Alexey Orishko84e77a82011-02-07 09:45:10 +0000227 /* set NTB format, if both formats are supported */
228 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
Ming Lei90b8b032012-10-24 19:46:56 +0000229 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
230 USB_TYPE_CLASS | USB_DIR_OUT
231 | USB_RECIP_INTERFACE,
232 USB_CDC_NCM_NTB16_FORMAT,
233 iface_no, NULL, 0);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000234 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000235 pr_debug("Setting NTB format to 16-bit failed\n");
236 }
237
Greg Suarez2d1c4312012-10-22 10:56:30 +0000238 ctx->max_datagram_size = min_dgram_size;
Alexey Orishko900d4952010-11-29 23:23:28 +0000239
240 /* set Max Datagram Size (MTU) */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000241 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
Ming Lei90b8b032012-10-24 19:46:56 +0000242 __le16 max_datagram_size;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000243 u16 eth_max_sz;
244 if (ctx->ether_desc != NULL)
245 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
246 else if (ctx->mbim_desc != NULL)
247 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
248 else
249 goto max_dgram_err;
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000250
Ming Lei90b8b032012-10-24 19:46:56 +0000251 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
252 USB_TYPE_CLASS | USB_DIR_IN
253 | USB_RECIP_INTERFACE,
254 0, iface_no, &max_datagram_size, 2);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000255 if (err < 0) {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000256 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
Greg Suarez2d1c4312012-10-22 10:56:30 +0000257 min_dgram_size);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000258 } else {
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000259 ctx->max_datagram_size =
Ming Lei90b8b032012-10-24 19:46:56 +0000260 le16_to_cpu(max_datagram_size);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000261 /* Check Eth descriptor value */
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000262 if (ctx->max_datagram_size > eth_max_sz)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000263 ctx->max_datagram_size = eth_max_sz;
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000264
265 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
Greg Suarez2d1c4312012-10-22 10:56:30 +0000266 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000267
Greg Suarez2d1c4312012-10-22 10:56:30 +0000268 if (ctx->max_datagram_size < min_dgram_size)
269 ctx->max_datagram_size = min_dgram_size;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000270
271 /* if value changed, update device */
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000272 if (ctx->max_datagram_size !=
Ming Lei90b8b032012-10-24 19:46:56 +0000273 le16_to_cpu(max_datagram_size)) {
274 err = usbnet_write_cmd(dev,
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000275 USB_CDC_SET_MAX_DATAGRAM_SIZE,
276 USB_TYPE_CLASS | USB_DIR_OUT
277 | USB_RECIP_INTERFACE,
278 0,
Ming Lei90b8b032012-10-24 19:46:56 +0000279 iface_no, &max_datagram_size,
280 2);
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000281 if (err < 0)
282 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
283 }
Alexey Orishko84e77a82011-02-07 09:45:10 +0000284 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000285 }
286
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000287max_dgram_err:
Greg Suarez2d1c4312012-10-22 10:56:30 +0000288 if (ctx->netdev->mtu != (ctx->max_datagram_size - eth_hlen))
289 ctx->netdev->mtu = ctx->max_datagram_size - eth_hlen;
Alexey Orishko900d4952010-11-29 23:23:28 +0000290
291 return 0;
292}
293
294static void
295cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
296{
297 struct usb_host_endpoint *e;
298 u8 ep;
299
300 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
301
302 e = intf->cur_altsetting->endpoint + ep;
303 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
304 case USB_ENDPOINT_XFER_INT:
305 if (usb_endpoint_dir_in(&e->desc)) {
306 if (ctx->status_ep == NULL)
307 ctx->status_ep = e;
308 }
309 break;
310
311 case USB_ENDPOINT_XFER_BULK:
312 if (usb_endpoint_dir_in(&e->desc)) {
313 if (ctx->in_ep == NULL)
314 ctx->in_ep = e;
315 } else {
316 if (ctx->out_ep == NULL)
317 ctx->out_ep = e;
318 }
319 break;
320
321 default:
322 break;
323 }
324 }
325}
326
327static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
328{
329 if (ctx == NULL)
330 return;
331
Alexey Orishko900d4952010-11-29 23:23:28 +0000332 if (ctx->tx_rem_skb != NULL) {
333 dev_kfree_skb_any(ctx->tx_rem_skb);
334 ctx->tx_rem_skb = NULL;
335 }
336
337 if (ctx->tx_curr_skb != NULL) {
338 dev_kfree_skb_any(ctx->tx_curr_skb);
339 ctx->tx_curr_skb = NULL;
340 }
341
342 kfree(ctx);
343}
344
Dan Williamsf94898e2012-07-24 08:43:22 +0000345static const struct ethtool_ops cdc_ncm_ethtool_ops = {
346 .get_drvinfo = cdc_ncm_get_drvinfo,
347 .get_link = usbnet_get_link,
348 .get_msglevel = usbnet_get_msglevel,
349 .set_msglevel = usbnet_set_msglevel,
350 .get_settings = usbnet_get_settings,
351 .set_settings = usbnet_set_settings,
352 .nway_reset = usbnet_nway_reset,
353};
354
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000355int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
Alexey Orishko900d4952010-11-29 23:23:28 +0000356{
357 struct cdc_ncm_ctx *ctx;
358 struct usb_driver *driver;
359 u8 *buf;
360 int len;
361 int temp;
362 u8 iface_no;
363
Thomas Meyerc7969842011-11-17 12:43:40 +0000364 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Devendra Naga8f0923c2013-03-29 23:03:22 +0000365 if (!ctx)
366 return -ENOMEM;
Alexey Orishko900d4952010-11-29 23:23:28 +0000367
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000368 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
369 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
370 ctx->bh.data = (unsigned long)ctx;
371 ctx->bh.func = cdc_ncm_txpath_bh;
372 atomic_set(&ctx->stop, 0);
Alexey Orishko900d4952010-11-29 23:23:28 +0000373 spin_lock_init(&ctx->mtx);
374 ctx->netdev = dev->net;
375
376 /* store ctx pointer in device data field */
377 dev->data[0] = (unsigned long)ctx;
378
379 /* get some pointers */
380 driver = driver_of(intf);
381 buf = intf->cur_altsetting->extra;
382 len = intf->cur_altsetting->extralen;
383
384 ctx->udev = dev->udev;
Alexey Orishko900d4952010-11-29 23:23:28 +0000385
386 /* parse through descriptors associated with control interface */
387 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
388
389 if (buf[1] != USB_DT_CS_INTERFACE)
390 goto advance;
391
392 switch (buf[2]) {
393 case USB_CDC_UNION_TYPE:
394 if (buf[0] < sizeof(*(ctx->union_desc)))
395 break;
396
397 ctx->union_desc =
398 (const struct usb_cdc_union_desc *)buf;
399
400 ctx->control = usb_ifnum_to_if(dev->udev,
401 ctx->union_desc->bMasterInterface0);
402 ctx->data = usb_ifnum_to_if(dev->udev,
403 ctx->union_desc->bSlaveInterface0);
404 break;
405
406 case USB_CDC_ETHERNET_TYPE:
407 if (buf[0] < sizeof(*(ctx->ether_desc)))
408 break;
409
410 ctx->ether_desc =
411 (const struct usb_cdc_ether_desc *)buf;
Alexey Orishko900d4952010-11-29 23:23:28 +0000412 dev->hard_mtu =
413 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
414
Alexey Orishko84e77a82011-02-07 09:45:10 +0000415 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
416 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
417 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
418 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
Alexey Orishko900d4952010-11-29 23:23:28 +0000419 break;
420
421 case USB_CDC_NCM_TYPE:
422 if (buf[0] < sizeof(*(ctx->func_desc)))
423 break;
424
425 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
426 break;
427
Greg Suarez38396e42012-10-22 10:56:31 +0000428 case USB_CDC_MBIM_TYPE:
429 if (buf[0] < sizeof(*(ctx->mbim_desc)))
430 break;
431
432 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
433 break;
434
Alexey Orishko900d4952010-11-29 23:23:28 +0000435 default:
436 break;
437 }
438advance:
439 /* advance to next descriptor */
440 temp = buf[0];
441 buf += temp;
442 len -= temp;
443 }
444
Bjørn Mork9992c2e2013-01-21 05:50:38 +0000445 /* some buggy devices have an IAD but no CDC Union */
Bjørn Mork56a666d2013-01-25 23:36:59 +0000446 if (!ctx->union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
447 ctx->control = intf;
448 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
449 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
Bjørn Mork9992c2e2013-01-21 05:50:38 +0000450 }
451
Alexey Orishko900d4952010-11-29 23:23:28 +0000452 /* check if we got everything */
453 if ((ctx->control == NULL) || (ctx->data == NULL) ||
Greg Suarez38396e42012-10-22 10:56:31 +0000454 ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
Alexey Orishko900d4952010-11-29 23:23:28 +0000455 goto error;
456
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000457 /* claim data interface, if different from control */
458 if (ctx->data != ctx->control) {
459 temp = usb_driver_claim_interface(driver, ctx->data, dev);
460 if (temp)
461 goto error;
462 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000463
464 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
465
466 /* reset data interface */
467 temp = usb_set_interface(dev->udev, iface_no, 0);
468 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000469 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000470
471 /* initialize data interface */
472 if (cdc_ncm_setup(ctx))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000473 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000474
475 /* configure data interface */
Greg Suarez38396e42012-10-22 10:56:31 +0000476 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
Alexey Orishko900d4952010-11-29 23:23:28 +0000477 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000478 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000479
480 cdc_ncm_find_endpoints(ctx, ctx->data);
481 cdc_ncm_find_endpoints(ctx, ctx->control);
482
483 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
484 (ctx->status_ep == NULL))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000485 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000486
487 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
488
489 usb_set_intfdata(ctx->data, dev);
490 usb_set_intfdata(ctx->control, dev);
Alexey Orishko900d4952010-11-29 23:23:28 +0000491
Greg Suarez38396e42012-10-22 10:56:31 +0000492 if (ctx->ether_desc) {
493 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
494 if (temp)
495 goto error2;
496 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
497 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000498
Alexey Orishko900d4952010-11-29 23:23:28 +0000499
500 dev->in = usb_rcvbulkpipe(dev->udev,
501 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
502 dev->out = usb_sndbulkpipe(dev->udev,
503 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
504 dev->status = ctx->status_ep;
505 dev->rx_urb_size = ctx->rx_max;
506
Bjørn Mork20572222013-11-01 11:16:38 +0100507 /* cdc_ncm_setup will override dwNtbOutMaxSize if it is
508 * outside the sane range. Adding a pad byte here if necessary
509 * simplifies the handling in cdc_ncm_fill_tx_frame, making
510 * tx_max always represent the real skb max size.
511 */
512 if (ctx->tx_max != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
513 ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
514 ctx->tx_max++;
515
Alexey Orishko900d4952010-11-29 23:23:28 +0000516 ctx->tx_speed = ctx->rx_speed = 0;
517 return 0;
518
Alexey Orishko19694ac2011-05-24 05:26:13 +0000519error2:
520 usb_set_intfdata(ctx->control, NULL);
521 usb_set_intfdata(ctx->data, NULL);
Bjørn Mork6b4ef602013-01-21 05:50:40 +0000522 if (ctx->data != ctx->control)
523 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko900d4952010-11-29 23:23:28 +0000524error:
525 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
526 dev->data[0] = 0;
Alexey Orishko19694ac2011-05-24 05:26:13 +0000527 dev_info(&dev->udev->dev, "bind() failure\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000528 return -ENODEV;
529}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000530EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
Alexey Orishko900d4952010-11-29 23:23:28 +0000531
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000532void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
Alexey Orishko900d4952010-11-29 23:23:28 +0000533{
534 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko19694ac2011-05-24 05:26:13 +0000535 struct usb_driver *driver = driver_of(intf);
Alexey Orishko900d4952010-11-29 23:23:28 +0000536
537 if (ctx == NULL)
538 return; /* no setup */
539
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000540 atomic_set(&ctx->stop, 1);
541
542 if (hrtimer_active(&ctx->tx_timer))
543 hrtimer_cancel(&ctx->tx_timer);
544
545 tasklet_kill(&ctx->bh);
546
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000547 /* handle devices with combined control and data interface */
548 if (ctx->control == ctx->data)
549 ctx->data = NULL;
550
Alexey Orishko19694ac2011-05-24 05:26:13 +0000551 /* disconnect master --> disconnect slave */
552 if (intf == ctx->control && ctx->data) {
553 usb_set_intfdata(ctx->data, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000554 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000555 ctx->data = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000556
Alexey Orishko19694ac2011-05-24 05:26:13 +0000557 } else if (intf == ctx->data && ctx->control) {
558 usb_set_intfdata(ctx->control, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000559 usb_driver_release_interface(driver, ctx->control);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000560 ctx->control = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000561 }
562
Bjørn Mork3e515662013-11-01 11:16:40 +0100563 usb_set_intfdata(intf, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000564 cdc_ncm_free(ctx);
565}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000566EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
Alexey Orishko900d4952010-11-29 23:23:28 +0000567
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000568/* Select the MBIM altsetting iff it is preferred and available,
569 * returning the number of the corresponding data interface altsetting
570 */
571u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf)
Greg Suarez38396e42012-10-22 10:56:31 +0000572{
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000573 struct usb_host_interface *alt;
Greg Suarez38396e42012-10-22 10:56:31 +0000574
Bjørn Morkbd329e12012-10-22 10:56:38 +0000575 /* The MBIM spec defines a NCM compatible default altsetting,
576 * which we may have matched:
577 *
578 * "Functions that implement both NCM 1.0 and MBIM (an
579 * “NCM/MBIM function”) according to this recommendation
580 * shall provide two alternate settings for the
581 * Communication Interface. Alternate setting 0, and the
582 * associated class and endpoint descriptors, shall be
583 * constructed according to the rules given for the
584 * Communication Interface in section 5 of [USBNCM10].
585 * Alternate setting 1, and the associated class and
586 * endpoint descriptors, shall be constructed according to
587 * the rules given in section 6 (USB Device Model) of this
588 * specification."
Bjørn Morkbd329e12012-10-22 10:56:38 +0000589 */
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000590 if (prefer_mbim && intf->num_altsetting == 2) {
591 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
592 if (alt && cdc_ncm_comm_intf_is_mbim(alt) &&
593 !usb_set_interface(dev->udev,
594 intf->cur_altsetting->desc.bInterfaceNumber,
595 CDC_NCM_COMM_ALTSETTING_MBIM))
596 return CDC_NCM_DATA_ALTSETTING_MBIM;
Bjørn Morkf350ca02013-02-13 12:09:52 +0000597 }
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000598 return CDC_NCM_DATA_ALTSETTING_NCM;
599}
600EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
601
602static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
603{
604 int ret;
605
606 /* MBIM backwards compatible function? */
607 cdc_ncm_select_altsetting(dev, intf);
608 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
609 return -ENODEV;
Bjørn Morkbd329e12012-10-22 10:56:38 +0000610
Greg Suarez38396e42012-10-22 10:56:31 +0000611 /* NCM data altsetting is always 1 */
612 ret = cdc_ncm_bind_common(dev, intf, 1);
613
614 /*
615 * We should get an event when network connection is "connected" or
616 * "disconnected". Set network connection in "disconnected" state
617 * (carrier is OFF) during attach, so the IP network stack does not
618 * start IPv6 negotiation and more.
619 */
Ming Lei8a34b0a2013-04-11 04:40:33 +0000620 usbnet_link_change(dev, 0, 0);
Greg Suarez38396e42012-10-22 10:56:31 +0000621 return ret;
622}
623
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000624static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
Alexey Orishko900d4952010-11-29 23:23:28 +0000625{
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000626 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
627
628 if (skb->len + align > max)
629 align = max - skb->len;
630 if (align && skb_tailroom(skb) >= align)
631 memset(skb_put(skb, align), 0, align);
632}
633
634/* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
635 * allocating a new one within skb
636 */
637static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
638{
639 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
640 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
641 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
642
643 /* follow the chain of NDPs, looking for a match */
644 while (ndpoffset) {
645 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
646 if (ndp16->dwSignature == sign)
647 return ndp16;
648 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
649 }
650
651 /* align new NDP */
652 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
653
654 /* verify that there is room for the NDP and the datagram (reserve) */
655 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
656 return NULL;
657
658 /* link to it */
659 if (ndp16)
660 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
661 else
662 nth16->wNdpIndex = cpu_to_le16(skb->len);
663
664 /* push a new empty NDP */
665 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
666 ndp16->dwSignature = sign;
667 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
668 return ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000669}
670
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000671struct sk_buff *
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000672cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
Alexey Orishko900d4952010-11-29 23:23:28 +0000673{
Bjørn Mork20572222013-11-01 11:16:38 +0100674 struct usbnet *dev = netdev_priv(ctx->netdev);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000675 struct usb_cdc_ncm_nth16 *nth16;
676 struct usb_cdc_ncm_ndp16 *ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000677 struct sk_buff *skb_out;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000678 u16 n = 0, index, ndplen;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000679 u8 ready2send = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000680
681 /* if there is a remaining skb, it gets priority */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000682 if (skb != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000683 swap(skb, ctx->tx_rem_skb);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000684 swap(sign, ctx->tx_rem_sign);
685 } else {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000686 ready2send = 1;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000687 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000688
689 /* check if we are resuming an OUT skb */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000690 skb_out = ctx->tx_curr_skb;
Alexey Orishko900d4952010-11-29 23:23:28 +0000691
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000692 /* allocate a new OUT skb */
693 if (!skb_out) {
Bjørn Mork20572222013-11-01 11:16:38 +0100694 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
Alexey Orishko900d4952010-11-29 23:23:28 +0000695 if (skb_out == NULL) {
696 if (skb != NULL) {
697 dev_kfree_skb_any(skb);
698 ctx->netdev->stats.tx_dropped++;
699 }
700 goto exit_no_skb;
701 }
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000702 /* fill out the initial 16-bit NTB header */
703 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
704 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
705 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
706 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
Alexey Orishko900d4952010-11-29 23:23:28 +0000707
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000708 /* count total number of frames in this NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +0000709 ctx->tx_curr_frame_num = 0;
710 }
711
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000712 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
713 /* send any remaining skb first */
Alexey Orishko900d4952010-11-29 23:23:28 +0000714 if (skb == NULL) {
715 skb = ctx->tx_rem_skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000716 sign = ctx->tx_rem_sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000717 ctx->tx_rem_skb = NULL;
718
719 /* check for end of skb */
720 if (skb == NULL)
721 break;
722 }
723
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000724 /* get the appropriate NDP for this skb */
725 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
726
727 /* align beginning of next frame */
728 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
729
730 /* check if we had enough room left for both NDP and frame */
731 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000732 if (n == 0) {
733 /* won't fit, MTU problem? */
734 dev_kfree_skb_any(skb);
735 skb = NULL;
736 ctx->netdev->stats.tx_dropped++;
737 } else {
738 /* no room for skb - store for later */
739 if (ctx->tx_rem_skb != NULL) {
740 dev_kfree_skb_any(ctx->tx_rem_skb);
741 ctx->netdev->stats.tx_dropped++;
742 }
743 ctx->tx_rem_skb = skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000744 ctx->tx_rem_sign = sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000745 skb = NULL;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000746 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000747 }
748 break;
749 }
750
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000751 /* calculate frame number withing this NDP */
752 ndplen = le16_to_cpu(ndp16->wLength);
753 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000754
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000755 /* OK, add this skb */
756 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
757 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
758 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
759 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000760 dev_kfree_skb_any(skb);
761 skb = NULL;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000762
763 /* send now if this NDP is full */
764 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
765 ready2send = 1;
766 break;
767 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000768 }
769
770 /* free up any dangling skb */
771 if (skb != NULL) {
772 dev_kfree_skb_any(skb);
773 skb = NULL;
774 ctx->netdev->stats.tx_dropped++;
775 }
776
777 ctx->tx_curr_frame_num = n;
778
779 if (n == 0) {
780 /* wait for more frames */
781 /* push variables */
782 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +0000783 goto exit_no_skb;
784
Alexey Orishko84e77a82011-02-07 09:45:10 +0000785 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000786 /* wait for more frames */
787 /* push variables */
788 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +0000789 /* set the pending count */
790 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000791 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
Alexey Orishko900d4952010-11-29 23:23:28 +0000792 goto exit_no_skb;
793
794 } else {
795 /* frame goes out */
796 /* variables will be reset at next call */
797 }
798
Bjørn Mork20572222013-11-01 11:16:38 +0100799 /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
800 * bytes, we send buffers as it is. If we get more data, it
801 * would be more efficient for USB HS mobile device with DMA
802 * engine to receive a full size NTB, than canceling DMA
803 * transfer and receiving a short packet.
Alexey Orishko900d4952010-11-29 23:23:28 +0000804 */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000805 if (skb_out->len > CDC_NCM_MIN_TX_PKT)
Bjørn Mork20572222013-11-01 11:16:38 +0100806 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
807 ctx->tx_max - skb_out->len);
808 else if ((skb_out->len % dev->maxpacket) == 0)
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000809 *skb_put(skb_out, 1) = 0; /* force short packet */
Alexey Orishko900d4952010-11-29 23:23:28 +0000810
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000811 /* set final frame length */
812 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
813 nth16->wBlockLength = cpu_to_le16(skb_out->len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000814
815 /* return skb */
816 ctx->tx_curr_skb = NULL;
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000817 ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
Alexey Orishko900d4952010-11-29 23:23:28 +0000818 return skb_out;
819
820exit_no_skb:
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000821 /* Start timer, if there is a remaining skb */
822 if (ctx->tx_curr_skb != NULL)
823 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000824 return NULL;
825}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000826EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
Alexey Orishko900d4952010-11-29 23:23:28 +0000827
828static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
829{
830 /* start timer, if not already started */
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000831 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
832 hrtimer_start(&ctx->tx_timer,
833 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
834 HRTIMER_MODE_REL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000835}
836
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000837static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
Alexey Orishko900d4952010-11-29 23:23:28 +0000838{
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000839 struct cdc_ncm_ctx *ctx =
840 container_of(timer, struct cdc_ncm_ctx, tx_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +0000841
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000842 if (!atomic_read(&ctx->stop))
843 tasklet_schedule(&ctx->bh);
844 return HRTIMER_NORESTART;
845}
846
847static void cdc_ncm_txpath_bh(unsigned long param)
848{
849 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
850
851 spin_lock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000852 if (ctx->tx_timer_pending != 0) {
853 ctx->tx_timer_pending--;
Alexey Orishko900d4952010-11-29 23:23:28 +0000854 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000855 spin_unlock_bh(&ctx->mtx);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000856 } else if (ctx->netdev != NULL) {
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000857 spin_unlock_bh(&ctx->mtx);
858 netif_tx_lock_bh(ctx->netdev);
Alexey Orishko900d4952010-11-29 23:23:28 +0000859 usbnet_start_xmit(NULL, ctx->netdev);
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000860 netif_tx_unlock_bh(ctx->netdev);
Bjørn Mork7b1e0cb2012-10-25 21:44:09 +0000861 } else {
862 spin_unlock_bh(&ctx->mtx);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000863 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000864}
865
866static struct sk_buff *
867cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
868{
869 struct sk_buff *skb_out;
870 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko900d4952010-11-29 23:23:28 +0000871
872 /*
873 * The Ethernet API we are using does not support transmitting
874 * multiple Ethernet frames in a single call. This driver will
875 * accumulate multiple Ethernet frames and send out a larger
876 * USB frame when the USB buffer is full or when a single jiffies
877 * timeout happens.
878 */
879 if (ctx == NULL)
880 goto error;
881
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000882 spin_lock_bh(&ctx->mtx);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000883 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000884 spin_unlock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000885 return skb_out;
886
887error:
888 if (skb != NULL)
889 dev_kfree_skb_any(skb);
890
891 return NULL;
892}
893
Bjørn Morkff06ab12012-10-22 10:56:33 +0000894/* verify NTB header and return offset of first NDP, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000895int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
Alexey Orishko900d4952010-11-29 23:23:28 +0000896{
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000897 struct usb_cdc_ncm_nth16 *nth16;
Bjørn Morkff06ab12012-10-22 10:56:33 +0000898 int len;
899 int ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000900
Alexey Orishko900d4952010-11-29 23:23:28 +0000901 if (ctx == NULL)
902 goto error;
903
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000904 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
905 sizeof(struct usb_cdc_ncm_ndp16))) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000906 pr_debug("frame too short\n");
907 goto error;
908 }
909
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000910 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
Alexey Orishko900d4952010-11-29 23:23:28 +0000911
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000912 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000913 pr_debug("invalid NTH16 signature <%u>\n",
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000914 le32_to_cpu(nth16->dwSignature));
Alexey Orishko900d4952010-11-29 23:23:28 +0000915 goto error;
916 }
917
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000918 len = le16_to_cpu(nth16->wBlockLength);
919 if (len > ctx->rx_max) {
920 pr_debug("unsupported NTB block length %u/%u\n", len,
921 ctx->rx_max);
Alexey Orishko900d4952010-11-29 23:23:28 +0000922 goto error;
923 }
924
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000925 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
926 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
927 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
928 pr_debug("sequence number glitch prev=%d curr=%d\n",
929 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
930 }
931 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
932
Bjørn Morkff06ab12012-10-22 10:56:33 +0000933 ret = le16_to_cpu(nth16->wNdpIndex);
934error:
935 return ret;
936}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000937EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000938
939/* verify NDP header and return number of datagrams, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000940int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
Bjørn Morkff06ab12012-10-22 10:56:33 +0000941{
942 struct usb_cdc_ncm_ndp16 *ndp16;
943 int ret = -EINVAL;
944
Bjørn Mork75d67d32012-10-22 10:56:32 +0000945 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
946 pr_debug("invalid NDP offset <%u>\n", ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +0000947 goto error;
948 }
Bjørn Mork75d67d32012-10-22 10:56:32 +0000949 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +0000950
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000951 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000952 pr_debug("invalid DPT16 length <%u>\n",
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000953 le32_to_cpu(ndp16->dwSignature));
Bjørn Morkff06ab12012-10-22 10:56:33 +0000954 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +0000955 }
956
Bjørn Morkff06ab12012-10-22 10:56:33 +0000957 ret = ((le16_to_cpu(ndp16->wLength) -
Alexey Orishko900d4952010-11-29 23:23:28 +0000958 sizeof(struct usb_cdc_ncm_ndp16)) /
959 sizeof(struct usb_cdc_ncm_dpe16));
Bjørn Morkff06ab12012-10-22 10:56:33 +0000960 ret--; /* we process NDP entries except for the last one */
Alexey Orishko900d4952010-11-29 23:23:28 +0000961
Bjørn Morkff06ab12012-10-22 10:56:33 +0000962 if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000963 skb_in->len) {
Bjørn Morkff06ab12012-10-22 10:56:33 +0000964 pr_debug("Invalid nframes = %d\n", ret);
965 ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000966 }
967
Bjørn Morkff06ab12012-10-22 10:56:33 +0000968error:
969 return ret;
970}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000971EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000972
973static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
974{
975 struct sk_buff *skb;
976 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
977 int len;
978 int nframes;
979 int x;
980 int offset;
981 struct usb_cdc_ncm_ndp16 *ndp16;
982 struct usb_cdc_ncm_dpe16 *dpe16;
983 int ndpoffset;
984 int loopcount = 50; /* arbitrary max preventing infinite loop */
985
986 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
987 if (ndpoffset < 0)
988 goto error;
989
990next_ndp:
991 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
992 if (nframes < 0)
993 goto error;
994
995 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
996
997 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
998 pr_debug("invalid DPT16 signature <%u>\n",
999 le32_to_cpu(ndp16->dwSignature));
1000 goto err_ndp;
1001 }
1002 dpe16 = ndp16->dpe16;
Alexey Orishko900d4952010-11-29 23:23:28 +00001003
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001004 for (x = 0; x < nframes; x++, dpe16++) {
1005 offset = le16_to_cpu(dpe16->wDatagramIndex);
1006 len = le16_to_cpu(dpe16->wDatagramLength);
Alexey Orishko900d4952010-11-29 23:23:28 +00001007
1008 /*
1009 * CDC NCM ch. 3.7
1010 * All entries after first NULL entry are to be ignored
1011 */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001012 if ((offset == 0) || (len == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +00001013 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +00001014 goto err_ndp; /* empty NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +00001015 break;
1016 }
1017
1018 /* sanity checking */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001019 if (((offset + len) > skb_in->len) ||
1020 (len > ctx->rx_max) || (len < ETH_HLEN)) {
Alexey Orishko900d4952010-11-29 23:23:28 +00001021 pr_debug("invalid frame detected (ignored)"
Alexey Orishkof742aa82011-01-17 07:07:25 +00001022 "offset[%u]=%u, length=%u, skb=%p\n",
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001023 x, offset, len, skb_in);
Alexey Orishko900d4952010-11-29 23:23:28 +00001024 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +00001025 goto err_ndp;
Alexey Orishko900d4952010-11-29 23:23:28 +00001026 break;
1027
1028 } else {
1029 skb = skb_clone(skb_in, GFP_ATOMIC);
Jesper Juhl9e567902011-01-13 11:40:11 +00001030 if (!skb)
1031 goto error;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001032 skb->len = len;
Alexey Orishko900d4952010-11-29 23:23:28 +00001033 skb->data = ((u8 *)skb_in->data) + offset;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001034 skb_set_tail_pointer(skb, len);
Alexey Orishko900d4952010-11-29 23:23:28 +00001035 usbnet_skb_return(dev, skb);
1036 }
1037 }
Bjørn Mork75d67d32012-10-22 10:56:32 +00001038err_ndp:
1039 /* are there more NDPs to process? */
1040 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1041 if (ndpoffset && loopcount--)
1042 goto next_ndp;
1043
Alexey Orishko900d4952010-11-29 23:23:28 +00001044 return 1;
1045error:
1046 return 0;
1047}
1048
1049static void
1050cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001051 struct usb_cdc_speed_change *data)
Alexey Orishko900d4952010-11-29 23:23:28 +00001052{
Alexey Orishko84e77a82011-02-07 09:45:10 +00001053 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1054 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
Alexey Orishko900d4952010-11-29 23:23:28 +00001055
1056 /*
1057 * Currently the USB-NET API does not support reporting the actual
1058 * device speed. Do print it instead.
1059 */
1060 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1061 ctx->tx_speed = tx_speed;
1062 ctx->rx_speed = rx_speed;
1063
1064 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1065 printk(KERN_INFO KBUILD_MODNAME
1066 ": %s: %u mbit/s downlink "
1067 "%u mbit/s uplink\n",
1068 ctx->netdev->name,
1069 (unsigned int)(rx_speed / 1000000U),
1070 (unsigned int)(tx_speed / 1000000U));
1071 } else {
1072 printk(KERN_INFO KBUILD_MODNAME
1073 ": %s: %u kbit/s downlink "
1074 "%u kbit/s uplink\n",
1075 ctx->netdev->name,
1076 (unsigned int)(rx_speed / 1000U),
1077 (unsigned int)(tx_speed / 1000U));
1078 }
1079 }
1080}
1081
1082static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1083{
1084 struct cdc_ncm_ctx *ctx;
1085 struct usb_cdc_notification *event;
1086
1087 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1088
1089 if (urb->actual_length < sizeof(*event))
1090 return;
1091
1092 /* test for split data in 8-byte chunks */
1093 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1094 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001095 (struct usb_cdc_speed_change *)urb->transfer_buffer);
Alexey Orishko900d4952010-11-29 23:23:28 +00001096 return;
1097 }
1098
1099 event = urb->transfer_buffer;
1100
1101 switch (event->bNotificationType) {
1102 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1103 /*
1104 * According to the CDC NCM specification ch.7.1
1105 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1106 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1107 */
Bjørn Mork1a7c6cc2012-10-25 21:44:08 +00001108 ctx->connected = le16_to_cpu(event->wValue);
Alexey Orishko900d4952010-11-29 23:23:28 +00001109
1110 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1111 " %sconnected\n",
1112 ctx->netdev->name, ctx->connected ? "" : "dis");
1113
Ming Lei8a34b0a2013-04-11 04:40:33 +00001114 usbnet_link_change(dev, ctx->connected, 0);
1115 if (!ctx->connected)
Alexey Orishko900d4952010-11-29 23:23:28 +00001116 ctx->tx_speed = ctx->rx_speed = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +00001117 break;
1118
1119 case USB_CDC_NOTIFY_SPEED_CHANGE:
Alexey Orishko84e77a82011-02-07 09:45:10 +00001120 if (urb->actual_length < (sizeof(*event) +
1121 sizeof(struct usb_cdc_speed_change)))
Alexey Orishko900d4952010-11-29 23:23:28 +00001122 set_bit(EVENT_STS_SPLIT, &dev->flags);
1123 else
1124 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001125 (struct usb_cdc_speed_change *) &event[1]);
Alexey Orishko900d4952010-11-29 23:23:28 +00001126 break;
1127
1128 default:
Bjørn Mork67a36602013-04-08 08:26:23 +00001129 dev_dbg(&dev->udev->dev,
1130 "NCM: unexpected notification 0x%02x!\n",
1131 event->bNotificationType);
Alexey Orishko900d4952010-11-29 23:23:28 +00001132 break;
1133 }
1134}
1135
1136static int cdc_ncm_check_connect(struct usbnet *dev)
1137{
1138 struct cdc_ncm_ctx *ctx;
1139
1140 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1141 if (ctx == NULL)
1142 return 1; /* disconnected */
1143
1144 return !ctx->connected;
1145}
1146
1147static int
1148cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1149{
1150 return usbnet_probe(udev, prod);
1151}
1152
1153static void cdc_ncm_disconnect(struct usb_interface *intf)
1154{
1155 struct usbnet *dev = usb_get_intfdata(intf);
1156
1157 if (dev == NULL)
1158 return; /* already disconnected */
1159
1160 usbnet_disconnect(intf);
1161}
1162
Alexey Orishko900d4952010-11-29 23:23:28 +00001163static const struct driver_info cdc_ncm_info = {
1164 .description = "CDC NCM",
Arnd Bergmannc2613442011-04-01 20:12:02 -07001165 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
Alexey Orishko900d4952010-11-29 23:23:28 +00001166 .bind = cdc_ncm_bind,
1167 .unbind = cdc_ncm_unbind,
1168 .check_connect = cdc_ncm_check_connect,
Oliver Neukuma5e40702012-12-18 04:46:12 +00001169 .manage_power = usbnet_manage_power,
Alexey Orishko900d4952010-11-29 23:23:28 +00001170 .status = cdc_ncm_status,
1171 .rx_fixup = cdc_ncm_rx_fixup,
1172 .tx_fixup = cdc_ncm_tx_fixup,
1173};
1174
Dan Williamsf94898e2012-07-24 08:43:22 +00001175/* Same as cdc_ncm_info, but with FLAG_WWAN */
1176static const struct driver_info wwan_info = {
1177 .description = "Mobile Broadband Network Device",
1178 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1179 | FLAG_WWAN,
1180 .bind = cdc_ncm_bind,
1181 .unbind = cdc_ncm_unbind,
1182 .check_connect = cdc_ncm_check_connect,
Oliver Neukuma5e40702012-12-18 04:46:12 +00001183 .manage_power = usbnet_manage_power,
Dan Williamsf94898e2012-07-24 08:43:22 +00001184 .status = cdc_ncm_status,
1185 .rx_fixup = cdc_ncm_rx_fixup,
1186 .tx_fixup = cdc_ncm_tx_fixup,
1187};
1188
Wei Shuai2f62d5a2013-01-21 06:00:32 +00001189/* Same as wwan_info, but with FLAG_NOARP */
1190static const struct driver_info wwan_noarp_info = {
1191 .description = "Mobile Broadband Network Device (NO ARP)",
1192 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1193 | FLAG_WWAN | FLAG_NOARP,
1194 .bind = cdc_ncm_bind,
1195 .unbind = cdc_ncm_unbind,
1196 .check_connect = cdc_ncm_check_connect,
1197 .manage_power = usbnet_manage_power,
1198 .status = cdc_ncm_status,
1199 .rx_fixup = cdc_ncm_rx_fixup,
1200 .tx_fixup = cdc_ncm_tx_fixup,
1201};
1202
Dan Williamsf94898e2012-07-24 08:43:22 +00001203static const struct usb_device_id cdc_devs[] = {
1204 /* Ericsson MBM devices like F5521gw */
1205 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1206 | USB_DEVICE_ID_MATCH_VENDOR,
1207 .idVendor = 0x0bdb,
1208 .bInterfaceClass = USB_CLASS_COMM,
1209 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1210 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1211 .driver_info = (unsigned long) &wwan_info,
1212 },
1213
Peter Meiserf3a1ef92012-08-02 02:30:20 +00001214 /* Dell branded MBM devices like DW5550 */
1215 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1216 | USB_DEVICE_ID_MATCH_VENDOR,
1217 .idVendor = 0x413c,
1218 .bInterfaceClass = USB_CLASS_COMM,
1219 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1220 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1221 .driver_info = (unsigned long) &wwan_info,
1222 },
1223
1224 /* Toshiba branded MBM devices */
1225 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1226 | USB_DEVICE_ID_MATCH_VENDOR,
1227 .idVendor = 0x0930,
1228 .bInterfaceClass = USB_CLASS_COMM,
1229 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1230 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1231 .driver_info = (unsigned long) &wwan_info,
1232 },
1233
Bjørn Mork1f84eab2013-02-27 03:08:58 +00001234 /* tag Huawei devices as wwan */
1235 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1236 USB_CLASS_COMM,
1237 USB_CDC_SUBCLASS_NCM,
1238 USB_CDC_PROTO_NONE),
1239 .driver_info = (unsigned long)&wwan_info,
1240 },
1241
Bjørn Morkbbc8d922012-11-13 03:19:43 +00001242 /* Huawei NCM devices disguised as vendor specific */
1243 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
1244 .driver_info = (unsigned long)&wwan_info,
1245 },
1246 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
1247 .driver_info = (unsigned long)&wwan_info,
1248 },
Bjørn Mork96316c52013-02-06 05:21:53 +00001249 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
1250 .driver_info = (unsigned long)&wwan_info,
1251 },
Bjørn Morkbbc8d922012-11-13 03:19:43 +00001252
Wei Shuai2f62d5a2013-01-21 06:00:32 +00001253 /* Infineon(now Intel) HSPA Modem platform */
1254 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1255 USB_CLASS_COMM,
1256 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1257 .driver_info = (unsigned long)&wwan_noarp_info,
1258 },
1259
Dan Williamsf94898e2012-07-24 08:43:22 +00001260 /* Generic CDC-NCM devices */
1261 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1262 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1263 .driver_info = (unsigned long)&cdc_ncm_info,
1264 },
1265 {
1266 },
1267};
1268MODULE_DEVICE_TABLE(usb, cdc_devs);
1269
Alexey Orishko900d4952010-11-29 23:23:28 +00001270static struct usb_driver cdc_ncm_driver = {
1271 .name = "cdc_ncm",
1272 .id_table = cdc_devs,
1273 .probe = cdc_ncm_probe,
1274 .disconnect = cdc_ncm_disconnect,
1275 .suspend = usbnet_suspend,
1276 .resume = usbnet_resume,
Stefan Metzmacher85e3c652011-06-01 02:01:41 +00001277 .reset_resume = usbnet_resume,
Alexey Orishko900d4952010-11-29 23:23:28 +00001278 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001279 .disable_hub_initiated_lpm = 1,
Alexey Orishko900d4952010-11-29 23:23:28 +00001280};
1281
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001282module_usb_driver(cdc_ncm_driver);
Alexey Orishko900d4952010-11-29 23:23:28 +00001283
1284MODULE_AUTHOR("Hans Petter Selasky");
1285MODULE_DESCRIPTION("USB CDC NCM host driver");
1286MODULE_LICENSE("Dual BSD/GPL");