• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * cdc_ncm.c
3  *
4  * Copyright (C) ST-Ericsson 2010-2012
5  * 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/docs/devclass_docs/NCM10_012011.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/netdevice.h>
43 #include <linux/ctype.h>
44 #include <linux/etherdevice.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>
50 #include <linux/hrtimer.h>
51 #include <linux/atomic.h>
52 #include <linux/usb/usbnet.h>
53 #include <linux/usb/cdc.h>
54 #include <linux/usb/cdc_ncm.h>
55 
56 #if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
57 static bool prefer_mbim = true;
58 #else
59 static bool prefer_mbim;
60 #endif
61 module_param(prefer_mbim, bool, 0644);
62 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
63 
64 static void cdc_ncm_txpath_bh(struct tasklet_struct *t);
65 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
66 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
67 static struct usb_driver cdc_ncm_driver;
68 
69 struct cdc_ncm_stats {
70 	char stat_string[ETH_GSTRING_LEN];
71 	int sizeof_stat;
72 	int stat_offset;
73 };
74 
75 #define CDC_NCM_STAT(str, m) { \
76 		.stat_string = str, \
77 		.sizeof_stat = sizeof(((struct cdc_ncm_ctx *)0)->m), \
78 		.stat_offset = offsetof(struct cdc_ncm_ctx, m) }
79 #define CDC_NCM_SIMPLE_STAT(m)	CDC_NCM_STAT(__stringify(m), m)
80 
81 static const struct cdc_ncm_stats cdc_ncm_gstrings_stats[] = {
82 	CDC_NCM_SIMPLE_STAT(tx_reason_ntb_full),
83 	CDC_NCM_SIMPLE_STAT(tx_reason_ndp_full),
84 	CDC_NCM_SIMPLE_STAT(tx_reason_timeout),
85 	CDC_NCM_SIMPLE_STAT(tx_reason_max_datagram),
86 	CDC_NCM_SIMPLE_STAT(tx_overhead),
87 	CDC_NCM_SIMPLE_STAT(tx_ntbs),
88 	CDC_NCM_SIMPLE_STAT(rx_overhead),
89 	CDC_NCM_SIMPLE_STAT(rx_ntbs),
90 };
91 
92 #define CDC_NCM_LOW_MEM_MAX_CNT 10
93 
cdc_ncm_get_sset_count(struct net_device __always_unused * netdev,int sset)94 static int cdc_ncm_get_sset_count(struct net_device __always_unused *netdev, int sset)
95 {
96 	switch (sset) {
97 	case ETH_SS_STATS:
98 		return ARRAY_SIZE(cdc_ncm_gstrings_stats);
99 	default:
100 		return -EOPNOTSUPP;
101 	}
102 }
103 
cdc_ncm_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats __always_unused * stats,u64 * data)104 static void cdc_ncm_get_ethtool_stats(struct net_device *netdev,
105 				    struct ethtool_stats __always_unused *stats,
106 				    u64 *data)
107 {
108 	struct usbnet *dev = netdev_priv(netdev);
109 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
110 	int i;
111 	char *p = NULL;
112 
113 	for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
114 		p = (char *)ctx + cdc_ncm_gstrings_stats[i].stat_offset;
115 		data[i] = (cdc_ncm_gstrings_stats[i].sizeof_stat == sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
116 	}
117 }
118 
cdc_ncm_get_strings(struct net_device __always_unused * netdev,u32 stringset,u8 * data)119 static void cdc_ncm_get_strings(struct net_device __always_unused *netdev, u32 stringset, u8 *data)
120 {
121 	u8 *p = data;
122 	int i;
123 
124 	switch (stringset) {
125 	case ETH_SS_STATS:
126 		for (i = 0; i < ARRAY_SIZE(cdc_ncm_gstrings_stats); i++) {
127 			memcpy(p, cdc_ncm_gstrings_stats[i].stat_string, ETH_GSTRING_LEN);
128 			p += ETH_GSTRING_LEN;
129 		}
130 	}
131 }
132 
133 static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx);
134 
135 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
136 	.get_link		= usbnet_get_link,
137 	.nway_reset		= usbnet_nway_reset,
138 	.get_drvinfo		= usbnet_get_drvinfo,
139 	.get_msglevel		= usbnet_get_msglevel,
140 	.set_msglevel		= usbnet_set_msglevel,
141 	.get_ts_info		= ethtool_op_get_ts_info,
142 	.get_sset_count		= cdc_ncm_get_sset_count,
143 	.get_strings		= cdc_ncm_get_strings,
144 	.get_ethtool_stats	= cdc_ncm_get_ethtool_stats,
145 	.get_link_ksettings	= usbnet_get_link_ksettings_internal,
146 	.set_link_ksettings	= NULL,
147 };
148 
cdc_ncm_check_rx_max(struct usbnet * dev,u32 new_rx)149 static u32 cdc_ncm_check_rx_max(struct usbnet *dev, u32 new_rx)
150 {
151 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
152 	u32 val, max, min;
153 
154 	/* clamp new_rx to sane values */
155 	min = USB_CDC_NCM_NTB_MIN_IN_SIZE;
156 	max = min_t(u32, CDC_NCM_NTB_MAX_SIZE_RX, le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
157 
158 	/* dwNtbInMaxSize spec violation? Use MIN size for both limits */
159 	if (max < min) {
160 		dev_warn(&dev->intf->dev, "dwNtbInMaxSize=%u is too small. Using %u\n",
161 			 le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize), min);
162 		max = min;
163 	}
164 
165 	val = clamp_t(u32, new_rx, min, max);
166 	if (val != new_rx)
167 		dev_dbg(&dev->intf->dev, "rx_max must be in the [%u, %u] range\n", min, max);
168 
169 	return val;
170 }
171 
cdc_ncm_check_tx_max(struct usbnet * dev,u32 new_tx)172 static u32 cdc_ncm_check_tx_max(struct usbnet *dev, u32 new_tx)
173 {
174 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
175 	u32 val, max, min;
176 
177 	/* clamp new_tx to sane values */
178 	if (ctx->is_ndp16)
179 		min = ctx->max_datagram_size + ctx->max_ndp_size + sizeof(struct usb_cdc_ncm_nth16);
180 	else
181 		min = ctx->max_datagram_size + ctx->max_ndp_size + sizeof(struct usb_cdc_ncm_nth32);
182 
183 	if (le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) == 0)
184 		max = CDC_NCM_NTB_MAX_SIZE_TX; /* dwNtbOutMaxSize not set */
185 	else
186 		max = clamp_t(u32, le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize),
187 			      USB_CDC_NCM_NTB_MIN_OUT_SIZE,
188 			      CDC_NCM_NTB_MAX_SIZE_TX);
189 
190 	/* some devices set dwNtbOutMaxSize too low for the above default */
191 	min = min(min, max);
192 
193 	val = clamp_t(u32, new_tx, min, max);
194 	if (val != new_tx)
195 		dev_dbg(&dev->intf->dev, "tx_max must be in the [%u, %u] range\n", min, max);
196 
197 	return val;
198 }
199 
min_tx_pkt_show(struct device * d,struct device_attribute * attr,char * buf)200 static ssize_t min_tx_pkt_show(struct device *d,
201 			       struct device_attribute *attr, char *buf)
202 {
203 	struct usbnet *dev = netdev_priv(to_net_dev(d));
204 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
205 
206 	return sprintf(buf, "%u\n", ctx->min_tx_pkt);
207 }
208 
rx_max_show(struct device * d,struct device_attribute * attr,char * buf)209 static ssize_t rx_max_show(struct device *d,
210 			   struct device_attribute *attr, char *buf)
211 {
212 	struct usbnet *dev = netdev_priv(to_net_dev(d));
213 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
214 
215 	return sprintf(buf, "%u\n", ctx->rx_max);
216 }
217 
tx_max_show(struct device * d,struct device_attribute * attr,char * buf)218 static ssize_t tx_max_show(struct device *d,
219 			   struct device_attribute *attr, char *buf)
220 {
221 	struct usbnet *dev = netdev_priv(to_net_dev(d));
222 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
223 
224 	return sprintf(buf, "%u\n", ctx->tx_max);
225 }
226 
tx_timer_usecs_show(struct device * d,struct device_attribute * attr,char * buf)227 static ssize_t tx_timer_usecs_show(struct device *d,
228 				   struct device_attribute *attr, char *buf)
229 {
230 	struct usbnet *dev = netdev_priv(to_net_dev(d));
231 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
232 
233 	return sprintf(buf, "%u\n", ctx->timer_interval / (u32)NSEC_PER_USEC);
234 }
235 
min_tx_pkt_store(struct device * d,struct device_attribute * attr,const char * buf,size_t len)236 static ssize_t min_tx_pkt_store(struct device *d,
237 				struct device_attribute *attr,
238 				const char *buf, size_t len)
239 {
240 	struct usbnet *dev = netdev_priv(to_net_dev(d));
241 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
242 	unsigned long val;
243 
244 	/* no need to restrict values - anything from 0 to infinity is OK */
245 	if (kstrtoul(buf, 0, &val))
246 		return -EINVAL;
247 
248 	ctx->min_tx_pkt = val;
249 	return len;
250 }
251 
rx_max_store(struct device * d,struct device_attribute * attr,const char * buf,size_t len)252 static ssize_t rx_max_store(struct device *d,
253 			    struct device_attribute *attr,
254 			    const char *buf, size_t len)
255 {
256 	struct usbnet *dev = netdev_priv(to_net_dev(d));
257 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
258 	unsigned long val;
259 
260 	if (kstrtoul(buf, 0, &val) || cdc_ncm_check_rx_max(dev, val) != val)
261 		return -EINVAL;
262 
263 	cdc_ncm_update_rxtx_max(dev, val, ctx->tx_max);
264 	return len;
265 }
266 
tx_max_store(struct device * d,struct device_attribute * attr,const char * buf,size_t len)267 static ssize_t tx_max_store(struct device *d,
268 			    struct device_attribute *attr,
269 			    const char *buf, size_t len)
270 {
271 	struct usbnet *dev = netdev_priv(to_net_dev(d));
272 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
273 	unsigned long val;
274 
275 	if (kstrtoul(buf, 0, &val) || cdc_ncm_check_tx_max(dev, val) != val)
276 		return -EINVAL;
277 
278 	cdc_ncm_update_rxtx_max(dev, ctx->rx_max, val);
279 	return len;
280 }
281 
tx_timer_usecs_store(struct device * d,struct device_attribute * attr,const char * buf,size_t len)282 static ssize_t tx_timer_usecs_store(struct device *d,
283 				    struct device_attribute *attr,
284 				    const char *buf, size_t len)
285 {
286 	struct usbnet *dev = netdev_priv(to_net_dev(d));
287 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
288 	ssize_t ret;
289 	unsigned long val;
290 
291 	ret = kstrtoul(buf, 0, &val);
292 	if (ret)
293 		return ret;
294 	if (val && (val < CDC_NCM_TIMER_INTERVAL_MIN || val > CDC_NCM_TIMER_INTERVAL_MAX))
295 		return -EINVAL;
296 
297 	spin_lock_bh(&ctx->mtx);
298 	ctx->timer_interval = val * NSEC_PER_USEC;
299 	if (!ctx->timer_interval)
300 		ctx->tx_timer_pending = 0;
301 	spin_unlock_bh(&ctx->mtx);
302 	return len;
303 }
304 
305 static DEVICE_ATTR_RW(min_tx_pkt);
306 static DEVICE_ATTR_RW(rx_max);
307 static DEVICE_ATTR_RW(tx_max);
308 static DEVICE_ATTR_RW(tx_timer_usecs);
309 
ndp_to_end_show(struct device * d,struct device_attribute * attr,char * buf)310 static ssize_t ndp_to_end_show(struct device *d, struct device_attribute *attr, char *buf)
311 {
312 	struct usbnet *dev = netdev_priv(to_net_dev(d));
313 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
314 
315 	return sprintf(buf, "%c\n", ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END ? 'Y' : 'N');
316 }
317 
ndp_to_end_store(struct device * d,struct device_attribute * attr,const char * buf,size_t len)318 static ssize_t ndp_to_end_store(struct device *d,  struct device_attribute *attr, const char *buf, size_t len)
319 {
320 	struct usbnet *dev = netdev_priv(to_net_dev(d));
321 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
322 	bool enable;
323 
324 	if (strtobool(buf, &enable))
325 		return -EINVAL;
326 
327 	/* no change? */
328 	if (enable == (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
329 		return len;
330 
331 	if (enable) {
332 		if (ctx->is_ndp16 && !ctx->delayed_ndp16) {
333 			ctx->delayed_ndp16 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
334 			if (!ctx->delayed_ndp16)
335 				return -ENOMEM;
336 		}
337 		if (!ctx->is_ndp16 && !ctx->delayed_ndp32) {
338 			ctx->delayed_ndp32 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
339 			if (!ctx->delayed_ndp32)
340 				return -ENOMEM;
341 		}
342 	}
343 
344 	/* flush pending data before changing flag */
345 	netif_tx_lock_bh(dev->net);
346 	usbnet_start_xmit(NULL, dev->net);
347 	spin_lock_bh(&ctx->mtx);
348 	if (enable)
349 		ctx->drvflags |= CDC_NCM_FLAG_NDP_TO_END;
350 	else
351 		ctx->drvflags &= ~CDC_NCM_FLAG_NDP_TO_END;
352 	spin_unlock_bh(&ctx->mtx);
353 	netif_tx_unlock_bh(dev->net);
354 
355 	return len;
356 }
357 static DEVICE_ATTR_RW(ndp_to_end);
358 
359 #define NCM_PARM_ATTR(name, format, tocpu)				\
360 static ssize_t cdc_ncm_show_##name(struct device *d, struct device_attribute *attr, char *buf) \
361 { \
362 	struct usbnet *dev = netdev_priv(to_net_dev(d)); \
363 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0]; \
364 	return sprintf(buf, format "\n", tocpu(ctx->ncm_parm.name));	\
365 } \
366 static DEVICE_ATTR(name, 0444, cdc_ncm_show_##name, NULL)
367 
368 NCM_PARM_ATTR(bmNtbFormatsSupported, "0x%04x", le16_to_cpu);
369 NCM_PARM_ATTR(dwNtbInMaxSize, "%u", le32_to_cpu);
370 NCM_PARM_ATTR(wNdpInDivisor, "%u", le16_to_cpu);
371 NCM_PARM_ATTR(wNdpInPayloadRemainder, "%u", le16_to_cpu);
372 NCM_PARM_ATTR(wNdpInAlignment, "%u", le16_to_cpu);
373 NCM_PARM_ATTR(dwNtbOutMaxSize, "%u", le32_to_cpu);
374 NCM_PARM_ATTR(wNdpOutDivisor, "%u", le16_to_cpu);
375 NCM_PARM_ATTR(wNdpOutPayloadRemainder, "%u", le16_to_cpu);
376 NCM_PARM_ATTR(wNdpOutAlignment, "%u", le16_to_cpu);
377 NCM_PARM_ATTR(wNtbOutMaxDatagrams, "%u", le16_to_cpu);
378 
379 static struct attribute *cdc_ncm_sysfs_attrs[] = {
380 	&dev_attr_min_tx_pkt.attr,
381 	&dev_attr_ndp_to_end.attr,
382 	&dev_attr_rx_max.attr,
383 	&dev_attr_tx_max.attr,
384 	&dev_attr_tx_timer_usecs.attr,
385 	&dev_attr_bmNtbFormatsSupported.attr,
386 	&dev_attr_dwNtbInMaxSize.attr,
387 	&dev_attr_wNdpInDivisor.attr,
388 	&dev_attr_wNdpInPayloadRemainder.attr,
389 	&dev_attr_wNdpInAlignment.attr,
390 	&dev_attr_dwNtbOutMaxSize.attr,
391 	&dev_attr_wNdpOutDivisor.attr,
392 	&dev_attr_wNdpOutPayloadRemainder.attr,
393 	&dev_attr_wNdpOutAlignment.attr,
394 	&dev_attr_wNtbOutMaxDatagrams.attr,
395 	NULL,
396 };
397 
398 static const struct attribute_group cdc_ncm_sysfs_attr_group = {
399 	.name = "cdc_ncm",
400 	.attrs = cdc_ncm_sysfs_attrs,
401 };
402 
403 /* handle rx_max and tx_max changes */
cdc_ncm_update_rxtx_max(struct usbnet * dev,u32 new_rx,u32 new_tx)404 static void cdc_ncm_update_rxtx_max(struct usbnet *dev, u32 new_rx, u32 new_tx)
405 {
406 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
407 	u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
408 	u32 val;
409 
410 	val = cdc_ncm_check_rx_max(dev, new_rx);
411 
412 	/* inform device about NTB input size changes */
413 	if (val != ctx->rx_max) {
414 		__le32 dwNtbInMaxSize = cpu_to_le32(val);
415 
416 		dev_info(&dev->intf->dev, "setting rx_max = %u\n", val);
417 
418 		/* tell device to use new size */
419 		if (usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
420 				     USB_TYPE_CLASS | USB_DIR_OUT
421 				     | USB_RECIP_INTERFACE,
422 				     0, iface_no, &dwNtbInMaxSize, 4) < 0)
423 			dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
424 		else
425 			ctx->rx_max = val;
426 	}
427 
428 	/* usbnet use these values for sizing rx queues */
429 	if (dev->rx_urb_size != ctx->rx_max) {
430 		dev->rx_urb_size = ctx->rx_max;
431 		if (netif_running(dev->net))
432 			usbnet_unlink_rx_urbs(dev);
433 	}
434 
435 	val = cdc_ncm_check_tx_max(dev, new_tx);
436 	if (val != ctx->tx_max)
437 		dev_info(&dev->intf->dev, "setting tx_max = %u\n", val);
438 
439 	/* Adding a pad byte here if necessary simplifies the handling
440 	 * in cdc_ncm_fill_tx_frame, making tx_max always represent
441 	 * the real skb max size.
442 	 *
443 	 * We cannot use dev->maxpacket here because this is called from
444 	 * .bind which is called before usbnet sets up dev->maxpacket
445 	 */
446 	if (val != le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize) &&
447 	    val % usb_maxpacket(dev->udev, dev->out, 1) == 0)
448 		val++;
449 
450 	/* we might need to flush any pending tx buffers if running */
451 	if (netif_running(dev->net) && val > ctx->tx_max) {
452 		netif_tx_lock_bh(dev->net);
453 		usbnet_start_xmit(NULL, dev->net);
454 		/* make sure tx_curr_skb is reallocated if it was empty */
455 		if (ctx->tx_curr_skb) {
456 			dev_kfree_skb_any(ctx->tx_curr_skb);
457 			ctx->tx_curr_skb = NULL;
458 		}
459 		ctx->tx_max = val;
460 		netif_tx_unlock_bh(dev->net);
461 	} else {
462 		ctx->tx_max = val;
463 	}
464 
465 	dev->hard_mtu = ctx->tx_max;
466 
467 	/* max qlen depend on hard_mtu and rx_urb_size */
468 	usbnet_update_max_qlen(dev);
469 
470 	/* never pad more than 3 full USB packets per transfer */
471 	ctx->min_tx_pkt = clamp_t(u16, ctx->tx_max - 3 * usb_maxpacket(dev->udev, dev->out, 1),
472 				  CDC_NCM_MIN_TX_PKT, ctx->tx_max);
473 }
474 
475 /* helpers for NCM and MBIM differences */
cdc_ncm_flags(struct usbnet * dev)476 static u8 cdc_ncm_flags(struct usbnet *dev)
477 {
478 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
479 
480 	if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
481 		return ctx->mbim_desc->bmNetworkCapabilities;
482 	if (ctx->func_desc)
483 		return ctx->func_desc->bmNetworkCapabilities;
484 	return 0;
485 }
486 
cdc_ncm_eth_hlen(struct usbnet * dev)487 static int cdc_ncm_eth_hlen(struct usbnet *dev)
488 {
489 	if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
490 		return 0;
491 	return ETH_HLEN;
492 }
493 
cdc_ncm_min_dgram_size(struct usbnet * dev)494 static u32 cdc_ncm_min_dgram_size(struct usbnet *dev)
495 {
496 	if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting))
497 		return CDC_MBIM_MIN_DATAGRAM_SIZE;
498 	return CDC_NCM_MIN_DATAGRAM_SIZE;
499 }
500 
cdc_ncm_max_dgram_size(struct usbnet * dev)501 static u32 cdc_ncm_max_dgram_size(struct usbnet *dev)
502 {
503 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
504 
505 	if (cdc_ncm_comm_intf_is_mbim(dev->intf->cur_altsetting) && ctx->mbim_desc)
506 		return le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
507 	if (ctx->ether_desc)
508 		return le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
509 	return CDC_NCM_MAX_DATAGRAM_SIZE;
510 }
511 
512 /* initial one-time device setup.  MUST be called with the data interface
513  * in altsetting 0
514  */
cdc_ncm_init(struct usbnet * dev)515 static int cdc_ncm_init(struct usbnet *dev)
516 {
517 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
518 	u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
519 	int err;
520 
521 	err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
522 			      USB_TYPE_CLASS | USB_DIR_IN
523 			      |USB_RECIP_INTERFACE,
524 			      0, iface_no, &ctx->ncm_parm,
525 			      sizeof(ctx->ncm_parm));
526 	if (err < 0) {
527 		dev_err(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
528 		return err; /* GET_NTB_PARAMETERS is required */
529 	}
530 
531 	/* set CRC Mode */
532 	if (cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_CRC_MODE) {
533 		dev_dbg(&dev->intf->dev, "Setting CRC mode off\n");
534 		err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
535 				       USB_TYPE_CLASS | USB_DIR_OUT
536 				       | USB_RECIP_INTERFACE,
537 				       USB_CDC_NCM_CRC_NOT_APPENDED,
538 				       iface_no, NULL, 0);
539 		if (err < 0)
540 			dev_err(&dev->intf->dev, "SET_CRC_MODE failed\n");
541 	}
542 
543 	/* use ndp16 by default */
544 	ctx->is_ndp16 = 1;
545 
546 	/* set NTB format, if both formats are supported.
547 	 *
548 	 * "The host shall only send this command while the NCM Data
549 	 *  Interface is in alternate setting 0."
550 	 */
551 	if (le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported) &
552 						USB_CDC_NCM_NTB32_SUPPORTED) {
553 		if (ctx->drvflags & CDC_NCM_FLAG_PREFER_NTB32) {
554 			ctx->is_ndp16 = 0;
555 			dev_dbg(&dev->intf->dev, "Setting NTB format to 32-bit\n");
556 			err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
557 					       USB_TYPE_CLASS | USB_DIR_OUT
558 					       | USB_RECIP_INTERFACE,
559 					       USB_CDC_NCM_NTB32_FORMAT,
560 					       iface_no, NULL, 0);
561 		} else {
562 			ctx->is_ndp16 = 1;
563 			dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit\n");
564 			err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
565 					       USB_TYPE_CLASS | USB_DIR_OUT
566 					       | USB_RECIP_INTERFACE,
567 					       USB_CDC_NCM_NTB16_FORMAT,
568 					       iface_no, NULL, 0);
569 		}
570 		if (err < 0) {
571 			ctx->is_ndp16 = 1;
572 			dev_err(&dev->intf->dev, "SET_NTB_FORMAT failed\n");
573 		}
574 	}
575 
576 	/* set initial device values */
577 	ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
578 	ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
579 	ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
580 	ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
581 	ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
582 	/* devices prior to NCM Errata shall set this field to zero */
583 	ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
584 
585 	dev_dbg(&dev->intf->dev,
586 		"dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
587 		ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
588 		ctx->tx_ndp_modulus, ctx->tx_max_datagrams, cdc_ncm_flags(dev));
589 
590 	/* max count of tx datagrams */
591 	if ((ctx->tx_max_datagrams == 0) ||
592 			(ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
593 		ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
594 
595 	/* set up maximum NDP size */
596 	if (ctx->is_ndp16)
597 		ctx->max_ndp_size = sizeof(struct usb_cdc_ncm_ndp16) + (ctx->tx_max_datagrams + 1) * sizeof(struct usb_cdc_ncm_dpe16);
598 	else
599 		ctx->max_ndp_size = sizeof(struct usb_cdc_ncm_ndp32) + (ctx->tx_max_datagrams + 1) * sizeof(struct usb_cdc_ncm_dpe32);
600 
601 	/* initial coalescing timer interval */
602 	ctx->timer_interval = CDC_NCM_TIMER_INTERVAL_USEC * NSEC_PER_USEC;
603 
604 	return 0;
605 }
606 
607 /* set a new max datagram size */
cdc_ncm_set_dgram_size(struct usbnet * dev,int new_size)608 static void cdc_ncm_set_dgram_size(struct usbnet *dev, int new_size)
609 {
610 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
611 	u8 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
612 	__le16 max_datagram_size;
613 	u16 mbim_mtu;
614 	int err;
615 
616 	/* set default based on descriptors */
617 	ctx->max_datagram_size = clamp_t(u32, new_size,
618 					 cdc_ncm_min_dgram_size(dev),
619 					 CDC_NCM_MAX_DATAGRAM_SIZE);
620 
621 	/* inform the device about the selected Max Datagram Size? */
622 	if (!(cdc_ncm_flags(dev) & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE))
623 		goto out;
624 
625 	/* read current mtu value from device */
626 	err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
627 			      USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
628 			      0, iface_no, &max_datagram_size, sizeof(max_datagram_size));
629 	if (err != sizeof(max_datagram_size)) {
630 		dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed\n");
631 		goto out;
632 	}
633 
634 	if (le16_to_cpu(max_datagram_size) == ctx->max_datagram_size)
635 		goto out;
636 
637 	max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
638 	err = usbnet_write_cmd(dev, USB_CDC_SET_MAX_DATAGRAM_SIZE,
639 			       USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
640 			       0, iface_no, &max_datagram_size, sizeof(max_datagram_size));
641 	if (err < 0)
642 		dev_dbg(&dev->intf->dev, "SET_MAX_DATAGRAM_SIZE failed\n");
643 
644 out:
645 	/* set MTU to max supported by the device if necessary */
646 	dev->net->mtu = min_t(int, dev->net->mtu, ctx->max_datagram_size - cdc_ncm_eth_hlen(dev));
647 
648 	/* do not exceed operator preferred MTU */
649 	if (ctx->mbim_extended_desc) {
650 		mbim_mtu = le16_to_cpu(ctx->mbim_extended_desc->wMTU);
651 		if (mbim_mtu != 0 && mbim_mtu < dev->net->mtu)
652 			dev->net->mtu = mbim_mtu;
653 	}
654 }
655 
cdc_ncm_fix_modulus(struct usbnet * dev)656 static void cdc_ncm_fix_modulus(struct usbnet *dev)
657 {
658 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
659 	u32 val;
660 
661 	/*
662 	 * verify that the structure alignment is:
663 	 * - power of two
664 	 * - not greater than the maximum transmit length
665 	 * - not less than four bytes
666 	 */
667 	val = ctx->tx_ndp_modulus;
668 
669 	if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
670 	    (val != ((-val) & val)) || (val >= ctx->tx_max)) {
671 		dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
672 		ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
673 	}
674 
675 	/*
676 	 * verify that the payload alignment is:
677 	 * - power of two
678 	 * - not greater than the maximum transmit length
679 	 * - not less than four bytes
680 	 */
681 	val = ctx->tx_modulus;
682 
683 	if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
684 	    (val != ((-val) & val)) || (val >= ctx->tx_max)) {
685 		dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
686 		ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
687 	}
688 
689 	/* verify the payload remainder */
690 	if (ctx->tx_remainder >= ctx->tx_modulus) {
691 		dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
692 		ctx->tx_remainder = 0;
693 	}
694 
695 	/* adjust TX-remainder according to NCM specification. */
696 	ctx->tx_remainder = ((ctx->tx_remainder - cdc_ncm_eth_hlen(dev)) &
697 			     (ctx->tx_modulus - 1));
698 }
699 
cdc_ncm_setup(struct usbnet * dev)700 static int cdc_ncm_setup(struct usbnet *dev)
701 {
702 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
703 	u32 def_rx, def_tx;
704 
705 	/* be conservative when selecting initial buffer size to
706 	 * increase the number of hosts this will work for
707 	 */
708 	def_rx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_RX,
709 		       le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize));
710 	def_tx = min_t(u32, CDC_NCM_NTB_DEF_SIZE_TX,
711 		       le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize));
712 
713 	/* clamp rx_max and tx_max and inform device */
714 	cdc_ncm_update_rxtx_max(dev, def_rx, def_tx);
715 
716 	/* sanitize the modulus and remainder values */
717 	cdc_ncm_fix_modulus(dev);
718 
719 	/* set max datagram size */
720 	cdc_ncm_set_dgram_size(dev, cdc_ncm_max_dgram_size(dev));
721 	return 0;
722 }
723 
724 static void
cdc_ncm_find_endpoints(struct usbnet * dev,struct usb_interface * intf)725 cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
726 {
727 	struct usb_host_endpoint *e, *in = NULL, *out = NULL;
728 	u8 ep;
729 
730 	for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
731 		e = intf->cur_altsetting->endpoint + ep;
732 
733 		/* ignore endpoints which cannot transfer data */
734 		if (!usb_endpoint_maxp(&e->desc))
735 			continue;
736 
737 		switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
738 		case USB_ENDPOINT_XFER_INT:
739 			if (usb_endpoint_dir_in(&e->desc)) {
740 				if (!dev->status)
741 					dev->status = e;
742 			}
743 			break;
744 
745 		case USB_ENDPOINT_XFER_BULK:
746 			if (usb_endpoint_dir_in(&e->desc)) {
747 				if (!in)
748 					in = e;
749 			} else {
750 				if (!out)
751 					out = e;
752 			}
753 			break;
754 
755 		default:
756 			break;
757 		}
758 	}
759 	if (in && !dev->in)
760 		dev->in = usb_rcvbulkpipe(dev->udev,
761 					  in->desc.bEndpointAddress &
762 					  USB_ENDPOINT_NUMBER_MASK);
763 	if (out && !dev->out)
764 		dev->out = usb_sndbulkpipe(dev->udev,
765 					   out->desc.bEndpointAddress &
766 					   USB_ENDPOINT_NUMBER_MASK);
767 }
768 
cdc_ncm_free(struct cdc_ncm_ctx * ctx)769 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
770 {
771 	if (ctx == NULL)
772 		return;
773 
774 	if (ctx->tx_rem_skb != NULL) {
775 		dev_kfree_skb_any(ctx->tx_rem_skb);
776 		ctx->tx_rem_skb = NULL;
777 	}
778 
779 	if (ctx->tx_curr_skb != NULL) {
780 		dev_kfree_skb_any(ctx->tx_curr_skb);
781 		ctx->tx_curr_skb = NULL;
782 	}
783 
784 	if (ctx->is_ndp16)
785 		kfree(ctx->delayed_ndp16);
786 	else
787 		kfree(ctx->delayed_ndp32);
788 
789 	kfree(ctx);
790 }
791 
792 /* we need to override the usbnet change_mtu ndo for two reasons:
793  *  - respect the negotiated maximum datagram size
794  *  - avoid unwanted changes to rx and tx buffers
795  */
cdc_ncm_change_mtu(struct net_device * net,int new_mtu)796 int cdc_ncm_change_mtu(struct net_device *net, int new_mtu)
797 {
798 	struct usbnet *dev = netdev_priv(net);
799 
800 	net->mtu = new_mtu;
801 	cdc_ncm_set_dgram_size(dev, new_mtu + cdc_ncm_eth_hlen(dev));
802 
803 	return 0;
804 }
805 EXPORT_SYMBOL_GPL(cdc_ncm_change_mtu);
806 
807 static const struct net_device_ops cdc_ncm_netdev_ops = {
808 	.ndo_open	     = usbnet_open,
809 	.ndo_stop	     = usbnet_stop,
810 	.ndo_start_xmit	     = usbnet_start_xmit,
811 	.ndo_tx_timeout	     = usbnet_tx_timeout,
812 	.ndo_set_rx_mode     = usbnet_set_rx_mode,
813 	.ndo_get_stats64     = dev_get_tstats64,
814 	.ndo_change_mtu	     = cdc_ncm_change_mtu,
815 	.ndo_set_mac_address = eth_mac_addr,
816 	.ndo_validate_addr   = eth_validate_addr,
817 };
818 
cdc_ncm_bind_common(struct usbnet * dev,struct usb_interface * intf,u8 data_altsetting,int drvflags)819 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting, int drvflags)
820 {
821 	struct cdc_ncm_ctx *ctx;
822 	struct usb_driver *driver;
823 	u8 *buf;
824 	int len;
825 	int temp;
826 	u8 iface_no;
827 	struct usb_cdc_parsed_header hdr;
828 
829 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
830 	if (!ctx)
831 		return -ENOMEM;
832 
833 	ctx->dev = dev;
834 
835 	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
836 	ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
837 	tasklet_setup(&ctx->bh, cdc_ncm_txpath_bh);
838 	atomic_set(&ctx->stop, 0);
839 	spin_lock_init(&ctx->mtx);
840 
841 	/* store ctx pointer in device data field */
842 	dev->data[0] = (unsigned long)ctx;
843 
844 	/* only the control interface can be successfully probed */
845 	ctx->control = intf;
846 
847 	/* get some pointers */
848 	driver = driver_of(intf);
849 	buf = intf->cur_altsetting->extra;
850 	len = intf->cur_altsetting->extralen;
851 
852 	/* parse through descriptors associated with control interface */
853 	cdc_parse_cdc_header(&hdr, intf, buf, len);
854 
855 	if (hdr.usb_cdc_union_desc)
856 		ctx->data = usb_ifnum_to_if(dev->udev,
857 					    hdr.usb_cdc_union_desc->bSlaveInterface0);
858 	ctx->ether_desc = hdr.usb_cdc_ether_desc;
859 	ctx->func_desc = hdr.usb_cdc_ncm_desc;
860 	ctx->mbim_desc = hdr.usb_cdc_mbim_desc;
861 	ctx->mbim_extended_desc = hdr.usb_cdc_mbim_extended_desc;
862 
863 	/* some buggy devices have an IAD but no CDC Union */
864 	if (!hdr.usb_cdc_union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
865 		ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
866 		dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
867 	}
868 
869 	/* check if we got everything */
870 	if (!ctx->data) {
871 		dev_err(&intf->dev, "CDC Union missing and no IAD found\n");
872 		goto error;
873 	}
874 	if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting)) {
875 		if (!ctx->mbim_desc) {
876 			dev_err(&intf->dev, "MBIM functional descriptor missing\n");
877 			goto error;
878 		}
879 	} else {
880 		if (!ctx->ether_desc || !ctx->func_desc) {
881 			dev_err(&intf->dev, "NCM or ECM functional descriptors missing\n");
882 			goto error;
883 		}
884 	}
885 
886 	/* claim data interface, if different from control */
887 	if (ctx->data != ctx->control) {
888 		temp = usb_driver_claim_interface(driver, ctx->data, dev);
889 		if (temp) {
890 			dev_err(&intf->dev, "failed to claim data intf\n");
891 			goto error;
892 		}
893 	}
894 
895 	iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
896 
897 	/* Device-specific flags */
898 	ctx->drvflags = drvflags;
899 
900 	/* Reset data interface. Some devices will not reset properly
901 	 * unless they are configured first.  Toggle the altsetting to
902 	 * force a reset.
903 	 * Some other devices do not work properly with this procedure
904 	 * that can be avoided using quirk CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE
905 	 */
906 	if (!(ctx->drvflags & CDC_MBIM_FLAG_AVOID_ALTSETTING_TOGGLE))
907 		usb_set_interface(dev->udev, iface_no, data_altsetting);
908 
909 	temp = usb_set_interface(dev->udev, iface_no, 0);
910 	if (temp) {
911 		dev_dbg(&intf->dev, "set interface failed\n");
912 		goto error2;
913 	}
914 
915 	/* initialize basic device settings */
916 	if (cdc_ncm_init(dev))
917 		goto error2;
918 
919 	/* Some firmwares need a pause here or they will silently fail
920 	 * to set up the interface properly.  This value was decided
921 	 * empirically on a Sierra Wireless MC7455 running 02.08.02.00
922 	 * firmware.
923 	 */
924 	usleep_range(10000, 20000);
925 
926 	/* configure data interface */
927 	temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
928 	if (temp) {
929 		dev_dbg(&intf->dev, "set interface failed\n");
930 		goto error2;
931 	}
932 
933 	cdc_ncm_find_endpoints(dev, ctx->data);
934 	cdc_ncm_find_endpoints(dev, ctx->control);
935 	if (!dev->in || !dev->out || !dev->status) {
936 		dev_dbg(&intf->dev, "failed to collect endpoints\n");
937 		goto error2;
938 	}
939 
940 	usb_set_intfdata(ctx->control, dev);
941 
942 	if (ctx->ether_desc) {
943 		temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
944 		if (temp) {
945 			dev_err(&intf->dev, "failed to get mac address\n");
946 			goto error2;
947 		}
948 		dev_info(&intf->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
949 	}
950 
951 	/* finish setting up the device specific data */
952 	cdc_ncm_setup(dev);
953 
954 	/* Allocate the delayed NDP if needed. */
955 	if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
956 		if (ctx->is_ndp16) {
957 			ctx->delayed_ndp16 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
958 			if (!ctx->delayed_ndp16)
959 				goto error2;
960 		} else {
961 			ctx->delayed_ndp32 = kzalloc(ctx->max_ndp_size, GFP_KERNEL);
962 			if (!ctx->delayed_ndp32)
963 				goto error2;
964 		}
965 		dev_info(&intf->dev, "NDP will be placed at end of frame for this device.");
966 	}
967 
968 	/* override ethtool_ops */
969 	dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
970 
971 	/* add our sysfs attrs */
972 	dev->net->sysfs_groups[0] = &cdc_ncm_sysfs_attr_group;
973 
974 	/* must handle MTU changes */
975 	dev->net->netdev_ops = &cdc_ncm_netdev_ops;
976 	dev->net->max_mtu = cdc_ncm_max_dgram_size(dev) - cdc_ncm_eth_hlen(dev);
977 
978 	return 0;
979 
980 error2:
981 	usb_set_intfdata(ctx->control, NULL);
982 	usb_set_intfdata(ctx->data, NULL);
983 	if (ctx->data != ctx->control)
984 		usb_driver_release_interface(driver, ctx->data);
985 error:
986 	cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
987 	dev->data[0] = 0;
988 	dev_info(&intf->dev, "bind() failure\n");
989 	return -ENODEV;
990 }
991 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
992 
cdc_ncm_unbind(struct usbnet * dev,struct usb_interface * intf)993 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
994 {
995 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
996 	struct usb_driver *driver = driver_of(intf);
997 
998 	if (ctx == NULL)
999 		return;		/* no setup */
1000 
1001 	atomic_set(&ctx->stop, 1);
1002 
1003 	hrtimer_cancel(&ctx->tx_timer);
1004 
1005 	tasklet_kill(&ctx->bh);
1006 
1007 	/* handle devices with combined control and data interface */
1008 	if (ctx->control == ctx->data)
1009 		ctx->data = NULL;
1010 
1011 	/* disconnect master --> disconnect slave */
1012 	if (intf == ctx->control && ctx->data) {
1013 		usb_set_intfdata(ctx->data, NULL);
1014 		usb_driver_release_interface(driver, ctx->data);
1015 		ctx->data = NULL;
1016 
1017 	} else if (intf == ctx->data && ctx->control) {
1018 		usb_set_intfdata(ctx->control, NULL);
1019 		usb_driver_release_interface(driver, ctx->control);
1020 		ctx->control = NULL;
1021 	}
1022 
1023 	usb_set_intfdata(intf, NULL);
1024 	cdc_ncm_free(ctx);
1025 }
1026 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
1027 
1028 /* Return the number of the MBIM control interface altsetting iff it
1029  * is preferred and available,
1030  */
cdc_ncm_select_altsetting(struct usb_interface * intf)1031 u8 cdc_ncm_select_altsetting(struct usb_interface *intf)
1032 {
1033 	struct usb_host_interface *alt;
1034 
1035 	/* The MBIM spec defines a NCM compatible default altsetting,
1036 	 * which we may have matched:
1037 	 *
1038 	 *  "Functions that implement both NCM 1.0 and MBIM (an
1039 	 *   “NCM/MBIM function”) according to this recommendation
1040 	 *   shall provide two alternate settings for the
1041 	 *   Communication Interface.  Alternate setting 0, and the
1042 	 *   associated class and endpoint descriptors, shall be
1043 	 *   constructed according to the rules given for the
1044 	 *   Communication Interface in section 5 of [USBNCM10].
1045 	 *   Alternate setting 1, and the associated class and
1046 	 *   endpoint descriptors, shall be constructed according to
1047 	 *   the rules given in section 6 (USB Device Model) of this
1048 	 *   specification."
1049 	 */
1050 	if (intf->num_altsetting < 2)
1051 		return intf->cur_altsetting->desc.bAlternateSetting;
1052 
1053 	if (prefer_mbim) {
1054 		alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
1055 		if (alt && cdc_ncm_comm_intf_is_mbim(alt))
1056 			return CDC_NCM_COMM_ALTSETTING_MBIM;
1057 	}
1058 	return CDC_NCM_COMM_ALTSETTING_NCM;
1059 }
1060 EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
1061 
cdc_ncm_bind(struct usbnet * dev,struct usb_interface * intf)1062 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
1063 {
1064 	/* MBIM backwards compatible function? */
1065 	if (cdc_ncm_select_altsetting(intf) != CDC_NCM_COMM_ALTSETTING_NCM)
1066 		return -ENODEV;
1067 
1068 	/* The NCM data altsetting is fixed, so we hard-coded it.
1069 	 * Additionally, generic NCM devices are assumed to accept arbitrarily
1070 	 * placed NDP.
1071 	 */
1072 	return cdc_ncm_bind_common(dev, intf, CDC_NCM_DATA_ALTSETTING_NCM, 0);
1073 }
1074 
cdc_ncm_align_tail(struct sk_buff * skb,size_t modulus,size_t remainder,size_t max)1075 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
1076 {
1077 	size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
1078 
1079 	if (skb->len + align > max)
1080 		align = max - skb->len;
1081 	if (align && skb_tailroom(skb) >= align)
1082 		skb_put_zero(skb, align);
1083 }
1084 
1085 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
1086  * allocating a new one within skb
1087  */
cdc_ncm_ndp16(struct cdc_ncm_ctx * ctx,struct sk_buff * skb,__le32 sign,size_t reserve)1088 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
1089 {
1090 	struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
1091 	struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
1092 	size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
1093 
1094 	/* If NDP should be moved to the end of the NCM package, we can't follow the
1095 	* NTH16 header as we would normally do. NDP isn't written to the SKB yet, and
1096 	* the wNdpIndex field in the header is actually not consistent with reality. It will be later.
1097 	*/
1098 	if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
1099 		if (ctx->delayed_ndp16->dwSignature == sign)
1100 			return ctx->delayed_ndp16;
1101 
1102 		/* We can only push a single NDP to the end. Return
1103 		 * NULL to send what we've already got and queue this
1104 		 * skb for later.
1105 		 */
1106 		else if (ctx->delayed_ndp16->dwSignature)
1107 			return NULL;
1108 	}
1109 
1110 	/* follow the chain of NDPs, looking for a match */
1111 	while (ndpoffset) {
1112 		ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
1113 		if  (ndp16->dwSignature == sign)
1114 			return ndp16;
1115 		ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1116 	}
1117 
1118 	/* align new NDP */
1119 	if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
1120 		cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size);
1121 
1122 	/* verify that there is room for the NDP and the datagram (reserve) */
1123 	if ((ctx->tx_curr_size - skb->len - reserve) < ctx->max_ndp_size)
1124 		return NULL;
1125 
1126 	/* link to it */
1127 	if (ndp16)
1128 		ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
1129 	else
1130 		nth16->wNdpIndex = cpu_to_le16(skb->len);
1131 
1132 	/* push a new empty NDP */
1133 	if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
1134 		ndp16 = skb_put_zero(skb, ctx->max_ndp_size);
1135 	else
1136 		ndp16 = ctx->delayed_ndp16;
1137 
1138 	ndp16->dwSignature = sign;
1139 	ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
1140 	return ndp16;
1141 }
1142 
cdc_ncm_ndp32(struct cdc_ncm_ctx * ctx,struct sk_buff * skb,__le32 sign,size_t reserve)1143 static struct usb_cdc_ncm_ndp32 *cdc_ncm_ndp32(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
1144 {
1145 	struct usb_cdc_ncm_ndp32 *ndp32 = NULL;
1146 	struct usb_cdc_ncm_nth32 *nth32 = (void *)skb->data;
1147 	size_t ndpoffset = le32_to_cpu(nth32->dwNdpIndex);
1148 
1149 	/* If NDP should be moved to the end of the NCM package, we can't follow the
1150 	 * NTH32 header as we would normally do. NDP isn't written to the SKB yet, and
1151 	 * the wNdpIndex field in the header is actually not consistent with reality. It will be later.
1152 	 */
1153 	if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
1154 		if (ctx->delayed_ndp32->dwSignature == sign)
1155 			return ctx->delayed_ndp32;
1156 
1157 		/* We can only push a single NDP to the end. Return
1158 		 * NULL to send what we've already got and queue this
1159 		 * skb for later.
1160 		 */
1161 		else if (ctx->delayed_ndp32->dwSignature)
1162 			return NULL;
1163 	}
1164 
1165 	/* follow the chain of NDPs, looking for a match */
1166 	while (ndpoffset) {
1167 		ndp32 = (struct usb_cdc_ncm_ndp32 *)(skb->data + ndpoffset);
1168 		if  (ndp32->dwSignature == sign)
1169 			return ndp32;
1170 		ndpoffset = le32_to_cpu(ndp32->dwNextNdpIndex);
1171 	}
1172 
1173 	/* align new NDP */
1174 	if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
1175 		cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size);
1176 
1177 	/* verify that there is room for the NDP and the datagram (reserve) */
1178 	if ((ctx->tx_curr_size - skb->len - reserve) < ctx->max_ndp_size)
1179 		return NULL;
1180 
1181 	/* link to it */
1182 	if (ndp32)
1183 		ndp32->dwNextNdpIndex = cpu_to_le32(skb->len);
1184 	else
1185 		nth32->dwNdpIndex = cpu_to_le32(skb->len);
1186 
1187 	/* push a new empty NDP */
1188 	if (!(ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END))
1189 		ndp32 = skb_put_zero(skb, ctx->max_ndp_size);
1190 	else
1191 		ndp32 = ctx->delayed_ndp32;
1192 
1193 	ndp32->dwSignature = sign;
1194 	ndp32->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp32) + sizeof(struct usb_cdc_ncm_dpe32));
1195 	return ndp32;
1196 }
1197 
1198 struct sk_buff *
cdc_ncm_fill_tx_frame(struct usbnet * dev,struct sk_buff * skb,__le32 sign)1199 cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
1200 {
1201 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1202 	union {
1203 		struct usb_cdc_ncm_nth16 *nth16;
1204 		struct usb_cdc_ncm_nth32 *nth32;
1205 	} nth;
1206 	union {
1207 		struct usb_cdc_ncm_ndp16 *ndp16;
1208 		struct usb_cdc_ncm_ndp32 *ndp32;
1209 	} ndp;
1210 	struct sk_buff *skb_out;
1211 	u16 n = 0, index, ndplen;
1212 	u8 ready2send = 0;
1213 	u32 delayed_ndp_size;
1214 	size_t padding_count;
1215 
1216 	/* When our NDP gets written in cdc_ncm_ndp(), then skb_out->len gets updated
1217 	 * accordingly. Otherwise, we should check here.
1218 	 */
1219 	if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END)
1220 		delayed_ndp_size = ctx->max_ndp_size +
1221 			max_t(u32,
1222 			      ctx->tx_ndp_modulus,
1223 			      ctx->tx_modulus + ctx->tx_remainder) - 1;
1224 	else
1225 		delayed_ndp_size = 0;
1226 
1227 	/* if there is a remaining skb, it gets priority */
1228 	if (skb != NULL) {
1229 		swap(skb, ctx->tx_rem_skb);
1230 		swap(sign, ctx->tx_rem_sign);
1231 	} else {
1232 		ready2send = 1;
1233 	}
1234 
1235 	/* check if we are resuming an OUT skb */
1236 	skb_out = ctx->tx_curr_skb;
1237 
1238 	/* allocate a new OUT skb */
1239 	if (!skb_out) {
1240 		if (ctx->tx_low_mem_val == 0) {
1241 			ctx->tx_curr_size = ctx->tx_max;
1242 			skb_out = alloc_skb(ctx->tx_curr_size, GFP_ATOMIC);
1243 			/* If the memory allocation fails we will wait longer
1244 			 * each time before attempting another full size
1245 			 * allocation again to not overload the system
1246 			 * further.
1247 			 */
1248 			if (skb_out == NULL) {
1249 				/* If even the smallest allocation fails, abort. */
1250 				if (ctx->tx_curr_size == USB_CDC_NCM_NTB_MIN_OUT_SIZE)
1251 					goto alloc_failed;
1252 				ctx->tx_low_mem_max_cnt = min(ctx->tx_low_mem_max_cnt + 1,
1253 							      (unsigned)CDC_NCM_LOW_MEM_MAX_CNT);
1254 				ctx->tx_low_mem_val = ctx->tx_low_mem_max_cnt;
1255 			}
1256 		}
1257 		if (skb_out == NULL) {
1258 			/* See if a very small allocation is possible.
1259 			 * We will send this packet immediately and hope
1260 			 * that there is more memory available later.
1261 			 */
1262 			if (skb)
1263 				ctx->tx_curr_size = max(skb->len,
1264 					(u32)USB_CDC_NCM_NTB_MIN_OUT_SIZE);
1265 			else
1266 				ctx->tx_curr_size = USB_CDC_NCM_NTB_MIN_OUT_SIZE;
1267 			skb_out = alloc_skb(ctx->tx_curr_size, GFP_ATOMIC);
1268 
1269 			/* No allocation possible so we will abort */
1270 			if (!skb_out)
1271 				goto alloc_failed;
1272 			ctx->tx_low_mem_val--;
1273 		}
1274 		if (ctx->is_ndp16) {
1275 			/* fill out the initial 16-bit NTB header */
1276 			nth.nth16 = skb_put_zero(skb_out, sizeof(struct usb_cdc_ncm_nth16));
1277 			nth.nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
1278 			nth.nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
1279 			nth.nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
1280 		} else {
1281 			/* fill out the initial 32-bit NTB header */
1282 			nth.nth32 = skb_put_zero(skb_out, sizeof(struct usb_cdc_ncm_nth32));
1283 			nth.nth32->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH32_SIGN);
1284 			nth.nth32->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth32));
1285 			nth.nth32->wSequence = cpu_to_le16(ctx->tx_seq++);
1286 		}
1287 
1288 		/* count total number of frames in this NTB */
1289 		ctx->tx_curr_frame_num = 0;
1290 
1291 		/* recent payload counter for this skb_out */
1292 		ctx->tx_curr_frame_payload = 0;
1293 	}
1294 
1295 	for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
1296 		/* send any remaining skb first */
1297 		if (skb == NULL) {
1298 			skb = ctx->tx_rem_skb;
1299 			sign = ctx->tx_rem_sign;
1300 			ctx->tx_rem_skb = NULL;
1301 
1302 			/* check for end of skb */
1303 			if (skb == NULL)
1304 				break;
1305 		}
1306 
1307 		/* get the appropriate NDP for this skb */
1308 		if (ctx->is_ndp16)
1309 			ndp.ndp16 = cdc_ncm_ndp16(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
1310 		else
1311 			ndp.ndp32 = cdc_ncm_ndp32(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
1312 
1313 		/* align beginning of next frame */
1314 		cdc_ncm_align_tail(skb_out,  ctx->tx_modulus, ctx->tx_remainder, ctx->tx_curr_size);
1315 
1316 		/* check if we had enough room left for both NDP and frame */
1317 		if ((ctx->is_ndp16 && !ndp.ndp16) || (!ctx->is_ndp16 && !ndp.ndp32) ||
1318 		    skb_out->len + skb->len + delayed_ndp_size > ctx->tx_curr_size) {
1319 			if (n == 0) {
1320 				/* won't fit, MTU problem? */
1321 				dev_kfree_skb_any(skb);
1322 				skb = NULL;
1323 				dev->net->stats.tx_dropped++;
1324 			} else {
1325 				/* no room for skb - store for later */
1326 				if (ctx->tx_rem_skb != NULL) {
1327 					dev_kfree_skb_any(ctx->tx_rem_skb);
1328 					dev->net->stats.tx_dropped++;
1329 				}
1330 				ctx->tx_rem_skb = skb;
1331 				ctx->tx_rem_sign = sign;
1332 				skb = NULL;
1333 				ready2send = 1;
1334 				ctx->tx_reason_ntb_full++;	/* count reason for transmitting */
1335 			}
1336 			break;
1337 		}
1338 
1339 		/* calculate frame number within this NDP */
1340 		if (ctx->is_ndp16) {
1341 			ndplen = le16_to_cpu(ndp.ndp16->wLength);
1342 			index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
1343 
1344 			/* OK, add this skb */
1345 			ndp.ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
1346 			ndp.ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
1347 			ndp.ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
1348 		} else {
1349 			ndplen = le16_to_cpu(ndp.ndp32->wLength);
1350 			index = (ndplen - sizeof(struct usb_cdc_ncm_ndp32)) / sizeof(struct usb_cdc_ncm_dpe32) - 1;
1351 
1352 			ndp.ndp32->dpe32[index].dwDatagramLength = cpu_to_le32(skb->len);
1353 			ndp.ndp32->dpe32[index].dwDatagramIndex = cpu_to_le32(skb_out->len);
1354 			ndp.ndp32->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe32));
1355 		}
1356 		skb_put_data(skb_out, skb->data, skb->len);
1357 		ctx->tx_curr_frame_payload += skb->len;	/* count real tx payload data */
1358 		dev_kfree_skb_any(skb);
1359 		skb = NULL;
1360 
1361 		/* send now if this NDP is full */
1362 		if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
1363 			ready2send = 1;
1364 			ctx->tx_reason_ndp_full++;	/* count reason for transmitting */
1365 			break;
1366 		}
1367 	}
1368 
1369 	/* free up any dangling skb */
1370 	if (skb != NULL) {
1371 		dev_kfree_skb_any(skb);
1372 		skb = NULL;
1373 		dev->net->stats.tx_dropped++;
1374 	}
1375 
1376 	ctx->tx_curr_frame_num = n;
1377 
1378 	if (n == 0) {
1379 		/* wait for more frames */
1380 		/* push variables */
1381 		ctx->tx_curr_skb = skb_out;
1382 		goto exit_no_skb;
1383 
1384 	} else if ((n < ctx->tx_max_datagrams) && (ready2send == 0) && (ctx->timer_interval > 0)) {
1385 		/* wait for more frames */
1386 		/* push variables */
1387 		ctx->tx_curr_skb = skb_out;
1388 		/* set the pending count */
1389 		if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
1390 			ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
1391 		goto exit_no_skb;
1392 
1393 	} else {
1394 		if (n == ctx->tx_max_datagrams)
1395 			ctx->tx_reason_max_datagram++;	/* count reason for transmitting */
1396 		/* frame goes out */
1397 		/* variables will be reset at next call */
1398 	}
1399 
1400 	/* If requested, put NDP at end of frame. */
1401 	if (ctx->drvflags & CDC_NCM_FLAG_NDP_TO_END) {
1402 		if (ctx->is_ndp16) {
1403 			nth.nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
1404 			cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size - ctx->max_ndp_size);
1405 			nth.nth16->wNdpIndex = cpu_to_le16(skb_out->len);
1406 			skb_put_data(skb_out, ctx->delayed_ndp16, ctx->max_ndp_size);
1407 
1408 			/* Zero out delayed NDP - signature checking will naturally fail. */
1409 			ndp.ndp16 = memset(ctx->delayed_ndp16, 0, ctx->max_ndp_size);
1410 		} else {
1411 			nth.nth32 = (struct usb_cdc_ncm_nth32 *)skb_out->data;
1412 			cdc_ncm_align_tail(skb_out, ctx->tx_ndp_modulus, 0, ctx->tx_curr_size - ctx->max_ndp_size);
1413 			nth.nth32->dwNdpIndex = cpu_to_le32(skb_out->len);
1414 			skb_put_data(skb_out, ctx->delayed_ndp32, ctx->max_ndp_size);
1415 
1416 			ndp.ndp32 = memset(ctx->delayed_ndp32, 0, ctx->max_ndp_size);
1417 		}
1418 	}
1419 
1420 	/* If collected data size is less or equal ctx->min_tx_pkt
1421 	 * bytes, we send buffers as it is. If we get more data, it
1422 	 * would be more efficient for USB HS mobile device with DMA
1423 	 * engine to receive a full size NTB, than canceling DMA
1424 	 * transfer and receiving a short packet.
1425 	 *
1426 	 * This optimization support is pointless if we end up sending
1427 	 * a ZLP after full sized NTBs.
1428 	 */
1429 	if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
1430 	    skb_out->len > ctx->min_tx_pkt) {
1431 		padding_count = ctx->tx_curr_size - skb_out->len;
1432 		if (!WARN_ON(padding_count > ctx->tx_curr_size))
1433 			skb_put_zero(skb_out, padding_count);
1434 	} else if (skb_out->len < ctx->tx_curr_size &&
1435 		   (skb_out->len % dev->maxpacket) == 0) {
1436 		skb_put_u8(skb_out, 0);	/* force short packet */
1437 	}
1438 
1439 	/* set final frame length */
1440 	if (ctx->is_ndp16) {
1441 		nth.nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
1442 		nth.nth16->wBlockLength = cpu_to_le16(skb_out->len);
1443 	} else {
1444 		nth.nth32 = (struct usb_cdc_ncm_nth32 *)skb_out->data;
1445 		nth.nth32->dwBlockLength = cpu_to_le32(skb_out->len);
1446 	}
1447 
1448 	/* return skb */
1449 	ctx->tx_curr_skb = NULL;
1450 
1451 	/* keep private stats: framing overhead and number of NTBs */
1452 	ctx->tx_overhead += skb_out->len - ctx->tx_curr_frame_payload;
1453 	ctx->tx_ntbs++;
1454 
1455 	/* usbnet will count all the framing overhead by default.
1456 	 * Adjust the stats so that the tx_bytes counter show real
1457 	 * payload data instead.
1458 	 */
1459 	usbnet_set_skb_tx_stats(skb_out, n,
1460 				(long)ctx->tx_curr_frame_payload - skb_out->len);
1461 
1462 	return skb_out;
1463 
1464 alloc_failed:
1465 	if (skb) {
1466 		dev_kfree_skb_any(skb);
1467 		dev->net->stats.tx_dropped++;
1468 	}
1469 exit_no_skb:
1470 	/* Start timer, if there is a remaining non-empty skb */
1471 	if (ctx->tx_curr_skb != NULL && n > 0)
1472 		cdc_ncm_tx_timeout_start(ctx);
1473 	return NULL;
1474 }
1475 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
1476 
cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx * ctx)1477 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
1478 {
1479 	/* start timer, if not already started */
1480 	if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
1481 		hrtimer_start(&ctx->tx_timer,
1482 				ctx->timer_interval,
1483 				HRTIMER_MODE_REL);
1484 }
1485 
cdc_ncm_tx_timer_cb(struct hrtimer * timer)1486 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
1487 {
1488 	struct cdc_ncm_ctx *ctx =
1489 			container_of(timer, struct cdc_ncm_ctx, tx_timer);
1490 
1491 	if (!atomic_read(&ctx->stop))
1492 		tasklet_schedule(&ctx->bh);
1493 	return HRTIMER_NORESTART;
1494 }
1495 
cdc_ncm_txpath_bh(struct tasklet_struct * t)1496 static void cdc_ncm_txpath_bh(struct tasklet_struct *t)
1497 {
1498 	struct cdc_ncm_ctx *ctx = from_tasklet(ctx, t, bh);
1499 	struct usbnet *dev = ctx->dev;
1500 
1501 	spin_lock_bh(&ctx->mtx);
1502 	if (ctx->tx_timer_pending != 0) {
1503 		ctx->tx_timer_pending--;
1504 		cdc_ncm_tx_timeout_start(ctx);
1505 		spin_unlock_bh(&ctx->mtx);
1506 	} else if (dev->net != NULL) {
1507 		ctx->tx_reason_timeout++;	/* count reason for transmitting */
1508 		spin_unlock_bh(&ctx->mtx);
1509 		netif_tx_lock_bh(dev->net);
1510 		usbnet_start_xmit(NULL, dev->net);
1511 		netif_tx_unlock_bh(dev->net);
1512 	} else {
1513 		spin_unlock_bh(&ctx->mtx);
1514 	}
1515 }
1516 
1517 struct sk_buff *
cdc_ncm_tx_fixup(struct usbnet * dev,struct sk_buff * skb,gfp_t flags)1518 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
1519 {
1520 	struct sk_buff *skb_out;
1521 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1522 
1523 	/*
1524 	 * The Ethernet API we are using does not support transmitting
1525 	 * multiple Ethernet frames in a single call. This driver will
1526 	 * accumulate multiple Ethernet frames and send out a larger
1527 	 * USB frame when the USB buffer is full or when a single jiffies
1528 	 * timeout happens.
1529 	 */
1530 	if (ctx == NULL)
1531 		goto error;
1532 
1533 	spin_lock_bh(&ctx->mtx);
1534 
1535 	if (ctx->is_ndp16)
1536 		skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
1537 	else
1538 		skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP32_NOCRC_SIGN));
1539 
1540 	spin_unlock_bh(&ctx->mtx);
1541 	return skb_out;
1542 
1543 error:
1544 	if (skb != NULL)
1545 		dev_kfree_skb_any(skb);
1546 
1547 	return NULL;
1548 }
1549 EXPORT_SYMBOL_GPL(cdc_ncm_tx_fixup);
1550 
1551 /* verify NTB header and return offset of first NDP, or negative error */
cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx * ctx,struct sk_buff * skb_in)1552 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
1553 {
1554 	struct usbnet *dev = netdev_priv(skb_in->dev);
1555 	struct usb_cdc_ncm_nth16 *nth16;
1556 	int len;
1557 	int ret = -EINVAL;
1558 
1559 	if (ctx == NULL)
1560 		goto error;
1561 
1562 	if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
1563 					sizeof(struct usb_cdc_ncm_ndp16))) {
1564 		netif_dbg(dev, rx_err, dev->net, "frame too short\n");
1565 		goto error;
1566 	}
1567 
1568 	nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
1569 
1570 	if (nth16->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN)) {
1571 		netif_dbg(dev, rx_err, dev->net,
1572 			  "invalid NTH16 signature <%#010x>\n",
1573 			  le32_to_cpu(nth16->dwSignature));
1574 		goto error;
1575 	}
1576 
1577 	len = le16_to_cpu(nth16->wBlockLength);
1578 	if (len > ctx->rx_max) {
1579 		netif_dbg(dev, rx_err, dev->net,
1580 			  "unsupported NTB block length %u/%u\n", len,
1581 			  ctx->rx_max);
1582 		goto error;
1583 	}
1584 
1585 	if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
1586 	    (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
1587 	    !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
1588 		netif_dbg(dev, rx_err, dev->net,
1589 			  "sequence number glitch prev=%d curr=%d\n",
1590 			  ctx->rx_seq, le16_to_cpu(nth16->wSequence));
1591 	}
1592 	ctx->rx_seq = le16_to_cpu(nth16->wSequence);
1593 
1594 	ret = le16_to_cpu(nth16->wNdpIndex);
1595 error:
1596 	return ret;
1597 }
1598 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
1599 
cdc_ncm_rx_verify_nth32(struct cdc_ncm_ctx * ctx,struct sk_buff * skb_in)1600 int cdc_ncm_rx_verify_nth32(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
1601 {
1602 	struct usbnet *dev = netdev_priv(skb_in->dev);
1603 	struct usb_cdc_ncm_nth32 *nth32;
1604 	int len;
1605 	int ret = -EINVAL;
1606 
1607 	if (ctx == NULL)
1608 		goto error;
1609 
1610 	if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth32) +
1611 					sizeof(struct usb_cdc_ncm_ndp32))) {
1612 		netif_dbg(dev, rx_err, dev->net, "frame too short\n");
1613 		goto error;
1614 	}
1615 
1616 	nth32 = (struct usb_cdc_ncm_nth32 *)skb_in->data;
1617 
1618 	if (nth32->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH32_SIGN)) {
1619 		netif_dbg(dev, rx_err, dev->net,
1620 			  "invalid NTH32 signature <%#010x>\n",
1621 			  le32_to_cpu(nth32->dwSignature));
1622 		goto error;
1623 	}
1624 
1625 	len = le32_to_cpu(nth32->dwBlockLength);
1626 	if (len > ctx->rx_max) {
1627 		netif_dbg(dev, rx_err, dev->net,
1628 			  "unsupported NTB block length %u/%u\n", len,
1629 			  ctx->rx_max);
1630 		goto error;
1631 	}
1632 
1633 	if ((ctx->rx_seq + 1) != le16_to_cpu(nth32->wSequence) &&
1634 	    (ctx->rx_seq || le16_to_cpu(nth32->wSequence)) &&
1635 	    !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth32->wSequence))) {
1636 		netif_dbg(dev, rx_err, dev->net,
1637 			  "sequence number glitch prev=%d curr=%d\n",
1638 			  ctx->rx_seq, le16_to_cpu(nth32->wSequence));
1639 	}
1640 	ctx->rx_seq = le16_to_cpu(nth32->wSequence);
1641 
1642 	ret = le32_to_cpu(nth32->dwNdpIndex);
1643 error:
1644 	return ret;
1645 }
1646 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth32);
1647 
1648 /* verify NDP header and return number of datagrams, or negative error */
cdc_ncm_rx_verify_ndp16(struct sk_buff * skb_in,int ndpoffset)1649 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
1650 {
1651 	struct usbnet *dev = netdev_priv(skb_in->dev);
1652 	struct usb_cdc_ncm_ndp16 *ndp16;
1653 	int ret = -EINVAL;
1654 
1655 	if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
1656 		netif_dbg(dev, rx_err, dev->net, "invalid NDP offset  <%u>\n",
1657 			  ndpoffset);
1658 		goto error;
1659 	}
1660 	ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1661 
1662 	if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
1663 		netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
1664 			  le16_to_cpu(ndp16->wLength));
1665 		goto error;
1666 	}
1667 
1668 	ret = ((le16_to_cpu(ndp16->wLength) -
1669 					sizeof(struct usb_cdc_ncm_ndp16)) /
1670 					sizeof(struct usb_cdc_ncm_dpe16));
1671 	ret--; /* we process NDP entries except for the last one */
1672 
1673 	if ((sizeof(struct usb_cdc_ncm_ndp16) +
1674 	     ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
1675 		netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
1676 		ret = -EINVAL;
1677 	}
1678 
1679 error:
1680 	return ret;
1681 }
1682 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
1683 
1684 /* verify NDP header and return number of datagrams, or negative error */
cdc_ncm_rx_verify_ndp32(struct sk_buff * skb_in,int ndpoffset)1685 int cdc_ncm_rx_verify_ndp32(struct sk_buff *skb_in, int ndpoffset)
1686 {
1687 	struct usbnet *dev = netdev_priv(skb_in->dev);
1688 	struct usb_cdc_ncm_ndp32 *ndp32;
1689 	int ret = -EINVAL;
1690 
1691 	if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp32)) > skb_in->len) {
1692 		netif_dbg(dev, rx_err, dev->net, "invalid NDP offset  <%u>\n",
1693 			  ndpoffset);
1694 		goto error;
1695 	}
1696 	ndp32 = (struct usb_cdc_ncm_ndp32 *)(skb_in->data + ndpoffset);
1697 
1698 	if (le16_to_cpu(ndp32->wLength) < USB_CDC_NCM_NDP32_LENGTH_MIN) {
1699 		netif_dbg(dev, rx_err, dev->net, "invalid DPT32 length <%u>\n",
1700 			  le16_to_cpu(ndp32->wLength));
1701 		goto error;
1702 	}
1703 
1704 	ret = ((le16_to_cpu(ndp32->wLength) -
1705 					sizeof(struct usb_cdc_ncm_ndp32)) /
1706 					sizeof(struct usb_cdc_ncm_dpe32));
1707 	ret--; /* we process NDP entries except for the last one */
1708 
1709 	if ((sizeof(struct usb_cdc_ncm_ndp32) +
1710 	     ret * (sizeof(struct usb_cdc_ncm_dpe32))) > skb_in->len) {
1711 		netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
1712 		ret = -EINVAL;
1713 	}
1714 
1715 error:
1716 	return ret;
1717 }
1718 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp32);
1719 
cdc_ncm_rx_fixup(struct usbnet * dev,struct sk_buff * skb_in)1720 int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
1721 {
1722 	struct sk_buff *skb;
1723 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
1724 	unsigned int len;
1725 	int nframes;
1726 	int x;
1727 	unsigned int offset;
1728 	union {
1729 		struct usb_cdc_ncm_ndp16 *ndp16;
1730 		struct usb_cdc_ncm_ndp32 *ndp32;
1731 	} ndp;
1732 	union {
1733 		struct usb_cdc_ncm_dpe16 *dpe16;
1734 		struct usb_cdc_ncm_dpe32 *dpe32;
1735 	} dpe;
1736 
1737 	int ndpoffset;
1738 	int loopcount = 50; /* arbitrary max preventing infinite loop */
1739 	u32 payload = 0;
1740 
1741 	if (ctx->is_ndp16)
1742 		ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
1743 	else
1744 		ndpoffset = cdc_ncm_rx_verify_nth32(ctx, skb_in);
1745 
1746 	if (ndpoffset < 0)
1747 		goto error;
1748 
1749 next_ndp:
1750 	if (ctx->is_ndp16) {
1751 		nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
1752 		if (nframes < 0)
1753 			goto error;
1754 
1755 		ndp.ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1756 
1757 		if (ndp.ndp16->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) {
1758 			netif_dbg(dev, rx_err, dev->net,
1759 				  "invalid DPT16 signature <%#010x>\n",
1760 				  le32_to_cpu(ndp.ndp16->dwSignature));
1761 			goto err_ndp;
1762 		}
1763 		dpe.dpe16 = ndp.ndp16->dpe16;
1764 	} else {
1765 		nframes = cdc_ncm_rx_verify_ndp32(skb_in, ndpoffset);
1766 		if (nframes < 0)
1767 			goto error;
1768 
1769 		ndp.ndp32 = (struct usb_cdc_ncm_ndp32 *)(skb_in->data + ndpoffset);
1770 
1771 		if (ndp.ndp32->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP32_NOCRC_SIGN)) {
1772 			netif_dbg(dev, rx_err, dev->net,
1773 				  "invalid DPT32 signature <%#010x>\n",
1774 				  le32_to_cpu(ndp.ndp32->dwSignature));
1775 			goto err_ndp;
1776 		}
1777 		dpe.dpe32 = ndp.ndp32->dpe32;
1778 	}
1779 
1780 	for (x = 0; x < nframes; x++) {
1781 		if (ctx->is_ndp16) {
1782 			offset = le16_to_cpu(dpe.dpe16->wDatagramIndex);
1783 			len = le16_to_cpu(dpe.dpe16->wDatagramLength);
1784 		} else {
1785 			offset = le32_to_cpu(dpe.dpe32->dwDatagramIndex);
1786 			len = le32_to_cpu(dpe.dpe32->dwDatagramLength);
1787 		}
1788 
1789 		/*
1790 		 * CDC NCM ch. 3.7
1791 		 * All entries after first NULL entry are to be ignored
1792 		 */
1793 		if ((offset == 0) || (len == 0)) {
1794 			if (!x)
1795 				goto err_ndp; /* empty NTB */
1796 			break;
1797 		}
1798 
1799 		/* sanity checking - watch out for integer wrap*/
1800 		if ((offset > skb_in->len) || (len > skb_in->len - offset) ||
1801 				(len > ctx->rx_max) || (len < ETH_HLEN)) {
1802 			netif_dbg(dev, rx_err, dev->net,
1803 				  "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
1804 				  x, offset, len, skb_in);
1805 			if (!x)
1806 				goto err_ndp;
1807 			break;
1808 
1809 		} else {
1810 			/* create a fresh copy to reduce truesize */
1811 			skb = netdev_alloc_skb_ip_align(dev->net,  len);
1812 			if (!skb)
1813 				goto error;
1814 			skb_put_data(skb, skb_in->data + offset, len);
1815 			usbnet_skb_return(dev, skb);
1816 			payload += len;	/* count payload bytes in this NTB */
1817 		}
1818 
1819 		if (ctx->is_ndp16)
1820 			dpe.dpe16++;
1821 		else
1822 			dpe.dpe32++;
1823 	}
1824 err_ndp:
1825 	/* are there more NDPs to process? */
1826 	if (ctx->is_ndp16)
1827 		ndpoffset = le16_to_cpu(ndp.ndp16->wNextNdpIndex);
1828 	else
1829 		ndpoffset = le32_to_cpu(ndp.ndp32->dwNextNdpIndex);
1830 
1831 	if (ndpoffset && loopcount--)
1832 		goto next_ndp;
1833 
1834 	/* update stats */
1835 	ctx->rx_overhead += skb_in->len - payload;
1836 	ctx->rx_ntbs++;
1837 
1838 	return 1;
1839 error:
1840 	return 0;
1841 }
1842 EXPORT_SYMBOL_GPL(cdc_ncm_rx_fixup);
1843 
1844 static void
cdc_ncm_speed_change(struct usbnet * dev,struct usb_cdc_speed_change * data)1845 cdc_ncm_speed_change(struct usbnet *dev,
1846 		     struct usb_cdc_speed_change *data)
1847 {
1848 	/* RTL8156 shipped before 2021 sends notification about every 32ms. */
1849 	dev->rx_speed = le32_to_cpu(data->DLBitRRate);
1850 	dev->tx_speed = le32_to_cpu(data->ULBitRate);
1851 }
1852 
cdc_ncm_status(struct usbnet * dev,struct urb * urb)1853 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1854 {
1855 	struct usb_cdc_notification *event;
1856 
1857 	if (urb->actual_length < sizeof(*event))
1858 		return;
1859 
1860 	/* test for split data in 8-byte chunks */
1861 	if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1862 		cdc_ncm_speed_change(dev,
1863 		      (struct usb_cdc_speed_change *)urb->transfer_buffer);
1864 		return;
1865 	}
1866 
1867 	event = urb->transfer_buffer;
1868 
1869 	switch (event->bNotificationType) {
1870 	case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1871 		/*
1872 		 * According to the CDC NCM specification ch.7.1
1873 		 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1874 		 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1875 		 */
1876 		/* RTL8156 shipped before 2021 sends notification about
1877 		 * every 32ms. Don't forward notification if state is same.
1878 		 */
1879 		if (netif_carrier_ok(dev->net) != !!event->wValue)
1880 			usbnet_link_change(dev, !!event->wValue, 0);
1881 		break;
1882 
1883 	case USB_CDC_NOTIFY_SPEED_CHANGE:
1884 		if (urb->actual_length < (sizeof(*event) +
1885 					sizeof(struct usb_cdc_speed_change)))
1886 			set_bit(EVENT_STS_SPLIT, &dev->flags);
1887 		else
1888 			cdc_ncm_speed_change(dev,
1889 					     (struct usb_cdc_speed_change *)&event[1]);
1890 		break;
1891 
1892 	default:
1893 		dev_dbg(&dev->udev->dev,
1894 			"NCM: unexpected notification 0x%02x!\n",
1895 			event->bNotificationType);
1896 		break;
1897 	}
1898 }
1899 
1900 static const struct driver_info cdc_ncm_info = {
1901 	.description = "CDC NCM",
1902 	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1903 			| FLAG_LINK_INTR | FLAG_ETHER,
1904 	.bind = cdc_ncm_bind,
1905 	.unbind = cdc_ncm_unbind,
1906 	.manage_power = usbnet_manage_power,
1907 	.status = cdc_ncm_status,
1908 	.rx_fixup = cdc_ncm_rx_fixup,
1909 	.tx_fixup = cdc_ncm_tx_fixup,
1910 	.set_rx_mode = usbnet_cdc_update_filter,
1911 };
1912 
1913 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1914 static const struct driver_info wwan_info = {
1915 	.description = "Mobile Broadband Network Device",
1916 	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1917 			| FLAG_LINK_INTR | FLAG_WWAN,
1918 	.bind = cdc_ncm_bind,
1919 	.unbind = cdc_ncm_unbind,
1920 	.manage_power = usbnet_manage_power,
1921 	.status = cdc_ncm_status,
1922 	.rx_fixup = cdc_ncm_rx_fixup,
1923 	.tx_fixup = cdc_ncm_tx_fixup,
1924 	.set_rx_mode = usbnet_cdc_update_filter,
1925 };
1926 
1927 /* Same as wwan_info, but with FLAG_NOARP  */
1928 static const struct driver_info wwan_noarp_info = {
1929 	.description = "Mobile Broadband Network Device (NO ARP)",
1930 	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1931 			| FLAG_LINK_INTR | FLAG_WWAN | FLAG_NOARP,
1932 	.bind = cdc_ncm_bind,
1933 	.unbind = cdc_ncm_unbind,
1934 	.manage_power = usbnet_manage_power,
1935 	.status = cdc_ncm_status,
1936 	.rx_fixup = cdc_ncm_rx_fixup,
1937 	.tx_fixup = cdc_ncm_tx_fixup,
1938 	.set_rx_mode = usbnet_cdc_update_filter,
1939 };
1940 
1941 static const struct usb_device_id cdc_devs[] = {
1942 	/* Ericsson MBM devices like F5521gw */
1943 	{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1944 		| USB_DEVICE_ID_MATCH_VENDOR,
1945 	  .idVendor = 0x0bdb,
1946 	  .bInterfaceClass = USB_CLASS_COMM,
1947 	  .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1948 	  .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1949 	  .driver_info = (unsigned long) &wwan_info,
1950 	},
1951 
1952 	/* Telit LE910 V2 */
1953 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1bc7, 0x0036,
1954 		USB_CLASS_COMM,
1955 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1956 	  .driver_info = (unsigned long)&wwan_noarp_info,
1957 	},
1958 
1959 	/* DW5812 LTE Verizon Mobile Broadband Card
1960 	 * Unlike DW5550 this device requires FLAG_NOARP
1961 	 */
1962 	{ USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bb,
1963 		USB_CLASS_COMM,
1964 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1965 	  .driver_info = (unsigned long)&wwan_noarp_info,
1966 	},
1967 
1968 	/* DW5813 LTE AT&T Mobile Broadband Card
1969 	 * Unlike DW5550 this device requires FLAG_NOARP
1970 	 */
1971 	{ USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bc,
1972 		USB_CLASS_COMM,
1973 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1974 	  .driver_info = (unsigned long)&wwan_noarp_info,
1975 	},
1976 
1977 	/* Dell branded MBM devices like DW5550 */
1978 	{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1979 		| USB_DEVICE_ID_MATCH_VENDOR,
1980 	  .idVendor = 0x413c,
1981 	  .bInterfaceClass = USB_CLASS_COMM,
1982 	  .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1983 	  .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1984 	  .driver_info = (unsigned long) &wwan_info,
1985 	},
1986 
1987 	/* Toshiba branded MBM devices */
1988 	{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1989 		| USB_DEVICE_ID_MATCH_VENDOR,
1990 	  .idVendor = 0x0930,
1991 	  .bInterfaceClass = USB_CLASS_COMM,
1992 	  .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1993 	  .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1994 	  .driver_info = (unsigned long) &wwan_info,
1995 	},
1996 
1997 	/* tag Huawei devices as wwan */
1998 	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1999 					USB_CLASS_COMM,
2000 					USB_CDC_SUBCLASS_NCM,
2001 					USB_CDC_PROTO_NONE),
2002 	  .driver_info = (unsigned long)&wwan_info,
2003 	},
2004 
2005 	/* Infineon(now Intel) HSPA Modem platform */
2006 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
2007 		USB_CLASS_COMM,
2008 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
2009 	  .driver_info = (unsigned long)&wwan_noarp_info,
2010 	},
2011 
2012 	/* u-blox TOBY-L4 */
2013 	{ USB_DEVICE_AND_INTERFACE_INFO(0x1546, 0x1010,
2014 		USB_CLASS_COMM,
2015 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
2016 	  .driver_info = (unsigned long)&wwan_info,
2017 	},
2018 
2019 	/* Generic CDC-NCM devices */
2020 	{ USB_INTERFACE_INFO(USB_CLASS_COMM,
2021 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
2022 		.driver_info = (unsigned long)&cdc_ncm_info,
2023 	},
2024 	{
2025 	},
2026 };
2027 MODULE_DEVICE_TABLE(usb, cdc_devs);
2028 
2029 static struct usb_driver cdc_ncm_driver = {
2030 	.name = "cdc_ncm",
2031 	.id_table = cdc_devs,
2032 	.probe = usbnet_probe,
2033 	.disconnect = usbnet_disconnect,
2034 	.suspend = usbnet_suspend,
2035 	.resume = usbnet_resume,
2036 	.reset_resume =	usbnet_resume,
2037 	.supports_autosuspend = 1,
2038 	.disable_hub_initiated_lpm = 1,
2039 };
2040 
2041 module_usb_driver(cdc_ncm_driver);
2042 
2043 MODULE_AUTHOR("Hans Petter Selasky");
2044 MODULE_DESCRIPTION("USB CDC NCM host driver");
2045 MODULE_LICENSE("Dual BSD/GPL");
2046