xref: /openbmc/linux/drivers/net/usb/cdc_ncm.c (revision 0d456bad)
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/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>
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 #define	DRIVER_VERSION				"14-Mar-2012"
57 
58 static void cdc_ncm_txpath_bh(unsigned long param);
59 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
60 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
61 static struct usb_driver cdc_ncm_driver;
62 
63 static void
64 cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
65 {
66 	struct usbnet *dev = netdev_priv(net);
67 
68 	strncpy(info->driver, dev->driver_name, sizeof(info->driver));
69 	strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
70 	strncpy(info->fw_version, dev->driver_info->description,
71 		sizeof(info->fw_version));
72 	usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
73 }
74 
75 static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
76 {
77 	u32 val;
78 	u8 flags;
79 	u8 iface_no;
80 	int err;
81 	int eth_hlen;
82 	u16 ntb_fmt_supported;
83 	u32 min_dgram_size;
84 	u32 min_hdr_size;
85 	struct usbnet *dev = netdev_priv(ctx->netdev);
86 
87 	iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
88 
89 	err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
90 			      USB_TYPE_CLASS | USB_DIR_IN
91 			      |USB_RECIP_INTERFACE,
92 			      0, iface_no, &ctx->ncm_parm,
93 			      sizeof(ctx->ncm_parm));
94 	if (err < 0) {
95 		pr_debug("failed GET_NTB_PARAMETERS\n");
96 		return 1;
97 	}
98 
99 	/* read correct set of parameters according to device mode */
100 	ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
101 	ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
102 	ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
103 	ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
104 	ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
105 	/* devices prior to NCM Errata shall set this field to zero */
106 	ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
107 	ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
108 
109 	eth_hlen = ETH_HLEN;
110 	min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
111 	min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
112 	if (ctx->mbim_desc != NULL) {
113 		flags = ctx->mbim_desc->bmNetworkCapabilities;
114 		eth_hlen = 0;
115 		min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
116 		min_hdr_size = 0;
117 	} else if (ctx->func_desc != NULL) {
118 		flags = ctx->func_desc->bmNetworkCapabilities;
119 	} else {
120 		flags = 0;
121 	}
122 
123 	pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
124 		 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
125 		 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
126 		 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
127 		 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
128 
129 	/* max count of tx datagrams */
130 	if ((ctx->tx_max_datagrams == 0) ||
131 			(ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
132 		ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
133 
134 	/* verify maximum size of received NTB in bytes */
135 	if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
136 		pr_debug("Using min receive length=%d\n",
137 						USB_CDC_NCM_NTB_MIN_IN_SIZE);
138 		ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
139 	}
140 
141 	if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
142 		pr_debug("Using default maximum receive length=%d\n",
143 						CDC_NCM_NTB_MAX_SIZE_RX);
144 		ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
145 	}
146 
147 	/* inform device about NTB input size changes */
148 	if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
149 		__le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
150 
151 		err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
152 				       USB_TYPE_CLASS | USB_DIR_OUT
153 				       | USB_RECIP_INTERFACE,
154 				       0, iface_no, &dwNtbInMaxSize, 4);
155 		if (err < 0)
156 			pr_debug("Setting NTB Input Size failed\n");
157 	}
158 
159 	/* verify maximum size of transmitted NTB in bytes */
160 	if ((ctx->tx_max <
161 	    (min_hdr_size + min_dgram_size)) ||
162 	    (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
163 		pr_debug("Using default maximum transmit length=%d\n",
164 						CDC_NCM_NTB_MAX_SIZE_TX);
165 		ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
166 	}
167 
168 	/*
169 	 * verify that the structure alignment is:
170 	 * - power of two
171 	 * - not greater than the maximum transmit length
172 	 * - not less than four bytes
173 	 */
174 	val = ctx->tx_ndp_modulus;
175 
176 	if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
177 	    (val != ((-val) & val)) || (val >= ctx->tx_max)) {
178 		pr_debug("Using default alignment: 4 bytes\n");
179 		ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
180 	}
181 
182 	/*
183 	 * verify that the payload alignment is:
184 	 * - power of two
185 	 * - not greater than the maximum transmit length
186 	 * - not less than four bytes
187 	 */
188 	val = ctx->tx_modulus;
189 
190 	if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
191 	    (val != ((-val) & val)) || (val >= ctx->tx_max)) {
192 		pr_debug("Using default transmit modulus: 4 bytes\n");
193 		ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
194 	}
195 
196 	/* verify the payload remainder */
197 	if (ctx->tx_remainder >= ctx->tx_modulus) {
198 		pr_debug("Using default transmit remainder: 0 bytes\n");
199 		ctx->tx_remainder = 0;
200 	}
201 
202 	/* adjust TX-remainder according to NCM specification. */
203 	ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
204 			     (ctx->tx_modulus - 1));
205 
206 	/* additional configuration */
207 
208 	/* set CRC Mode */
209 	if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
210 		err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
211 				       USB_TYPE_CLASS | USB_DIR_OUT
212 				       | USB_RECIP_INTERFACE,
213 				       USB_CDC_NCM_CRC_NOT_APPENDED,
214 				       iface_no, NULL, 0);
215 		if (err < 0)
216 			pr_debug("Setting CRC mode off failed\n");
217 	}
218 
219 	/* set NTB format, if both formats are supported */
220 	if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
221 		err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
222 				       USB_TYPE_CLASS | USB_DIR_OUT
223 				       | USB_RECIP_INTERFACE,
224 				       USB_CDC_NCM_NTB16_FORMAT,
225 				       iface_no, NULL, 0);
226 		if (err < 0)
227 			pr_debug("Setting NTB format to 16-bit failed\n");
228 	}
229 
230 	ctx->max_datagram_size = min_dgram_size;
231 
232 	/* set Max Datagram Size (MTU) */
233 	if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
234 		__le16 max_datagram_size;
235 		u16 eth_max_sz;
236 		if (ctx->ether_desc != NULL)
237 			eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
238 		else if (ctx->mbim_desc != NULL)
239 			eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
240 		else
241 			goto max_dgram_err;
242 
243 		err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
244 				      USB_TYPE_CLASS | USB_DIR_IN
245 				      | USB_RECIP_INTERFACE,
246 				      0, iface_no, &max_datagram_size, 2);
247 		if (err < 0) {
248 			pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
249 				 min_dgram_size);
250 		} else {
251 			ctx->max_datagram_size =
252 				le16_to_cpu(max_datagram_size);
253 			/* Check Eth descriptor value */
254 			if (ctx->max_datagram_size > eth_max_sz)
255 					ctx->max_datagram_size = eth_max_sz;
256 
257 			if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
258 				ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
259 
260 			if (ctx->max_datagram_size < min_dgram_size)
261 				ctx->max_datagram_size = min_dgram_size;
262 
263 			/* if value changed, update device */
264 			if (ctx->max_datagram_size !=
265 					le16_to_cpu(max_datagram_size)) {
266 				err = usbnet_write_cmd(dev,
267 						USB_CDC_SET_MAX_DATAGRAM_SIZE,
268 						USB_TYPE_CLASS | USB_DIR_OUT
269 						 | USB_RECIP_INTERFACE,
270 						0,
271 						iface_no, &max_datagram_size,
272 						2);
273 				if (err < 0)
274 					pr_debug("SET_MAX_DGRAM_SIZE failed\n");
275 			}
276 		}
277 	}
278 
279 max_dgram_err:
280 	if (ctx->netdev->mtu != (ctx->max_datagram_size - eth_hlen))
281 		ctx->netdev->mtu = ctx->max_datagram_size - eth_hlen;
282 
283 	return 0;
284 }
285 
286 static void
287 cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
288 {
289 	struct usb_host_endpoint *e;
290 	u8 ep;
291 
292 	for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
293 
294 		e = intf->cur_altsetting->endpoint + ep;
295 		switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
296 		case USB_ENDPOINT_XFER_INT:
297 			if (usb_endpoint_dir_in(&e->desc)) {
298 				if (ctx->status_ep == NULL)
299 					ctx->status_ep = e;
300 			}
301 			break;
302 
303 		case USB_ENDPOINT_XFER_BULK:
304 			if (usb_endpoint_dir_in(&e->desc)) {
305 				if (ctx->in_ep == NULL)
306 					ctx->in_ep = e;
307 			} else {
308 				if (ctx->out_ep == NULL)
309 					ctx->out_ep = e;
310 			}
311 			break;
312 
313 		default:
314 			break;
315 		}
316 	}
317 }
318 
319 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
320 {
321 	if (ctx == NULL)
322 		return;
323 
324 	if (ctx->tx_rem_skb != NULL) {
325 		dev_kfree_skb_any(ctx->tx_rem_skb);
326 		ctx->tx_rem_skb = NULL;
327 	}
328 
329 	if (ctx->tx_curr_skb != NULL) {
330 		dev_kfree_skb_any(ctx->tx_curr_skb);
331 		ctx->tx_curr_skb = NULL;
332 	}
333 
334 	kfree(ctx);
335 }
336 
337 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
338 	.get_drvinfo = cdc_ncm_get_drvinfo,
339 	.get_link = usbnet_get_link,
340 	.get_msglevel = usbnet_get_msglevel,
341 	.set_msglevel = usbnet_set_msglevel,
342 	.get_settings = usbnet_get_settings,
343 	.set_settings = usbnet_set_settings,
344 	.nway_reset = usbnet_nway_reset,
345 };
346 
347 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
348 {
349 	struct cdc_ncm_ctx *ctx;
350 	struct usb_driver *driver;
351 	u8 *buf;
352 	int len;
353 	int temp;
354 	u8 iface_no;
355 
356 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
357 	if (ctx == NULL)
358 		return -ENODEV;
359 
360 	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
361 	ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
362 	ctx->bh.data = (unsigned long)ctx;
363 	ctx->bh.func = cdc_ncm_txpath_bh;
364 	atomic_set(&ctx->stop, 0);
365 	spin_lock_init(&ctx->mtx);
366 	ctx->netdev = dev->net;
367 
368 	/* store ctx pointer in device data field */
369 	dev->data[0] = (unsigned long)ctx;
370 
371 	/* get some pointers */
372 	driver = driver_of(intf);
373 	buf = intf->cur_altsetting->extra;
374 	len = intf->cur_altsetting->extralen;
375 
376 	ctx->udev = dev->udev;
377 	ctx->intf = intf;
378 
379 	/* parse through descriptors associated with control interface */
380 	while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
381 
382 		if (buf[1] != USB_DT_CS_INTERFACE)
383 			goto advance;
384 
385 		switch (buf[2]) {
386 		case USB_CDC_UNION_TYPE:
387 			if (buf[0] < sizeof(*(ctx->union_desc)))
388 				break;
389 
390 			ctx->union_desc =
391 					(const struct usb_cdc_union_desc *)buf;
392 
393 			ctx->control = usb_ifnum_to_if(dev->udev,
394 					ctx->union_desc->bMasterInterface0);
395 			ctx->data = usb_ifnum_to_if(dev->udev,
396 					ctx->union_desc->bSlaveInterface0);
397 			break;
398 
399 		case USB_CDC_ETHERNET_TYPE:
400 			if (buf[0] < sizeof(*(ctx->ether_desc)))
401 				break;
402 
403 			ctx->ether_desc =
404 					(const struct usb_cdc_ether_desc *)buf;
405 			dev->hard_mtu =
406 				le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
407 
408 			if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
409 				dev->hard_mtu =	CDC_NCM_MIN_DATAGRAM_SIZE;
410 			else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
411 				dev->hard_mtu =	CDC_NCM_MAX_DATAGRAM_SIZE;
412 			break;
413 
414 		case USB_CDC_NCM_TYPE:
415 			if (buf[0] < sizeof(*(ctx->func_desc)))
416 				break;
417 
418 			ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
419 			break;
420 
421 		case USB_CDC_MBIM_TYPE:
422 			if (buf[0] < sizeof(*(ctx->mbim_desc)))
423 				break;
424 
425 			ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
426 			break;
427 
428 		default:
429 			break;
430 		}
431 advance:
432 		/* advance to next descriptor */
433 		temp = buf[0];
434 		buf += temp;
435 		len -= temp;
436 	}
437 
438 	/* check if we got everything */
439 	if ((ctx->control == NULL) || (ctx->data == NULL) ||
440 	    ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
441 		goto error;
442 
443 	/* claim data interface, if different from control */
444 	if (ctx->data != ctx->control) {
445 		temp = usb_driver_claim_interface(driver, ctx->data, dev);
446 		if (temp)
447 			goto error;
448 	}
449 
450 	iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
451 
452 	/* reset data interface */
453 	temp = usb_set_interface(dev->udev, iface_no, 0);
454 	if (temp)
455 		goto error2;
456 
457 	/* initialize data interface */
458 	if (cdc_ncm_setup(ctx))
459 		goto error2;
460 
461 	/* configure data interface */
462 	temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
463 	if (temp)
464 		goto error2;
465 
466 	cdc_ncm_find_endpoints(ctx, ctx->data);
467 	cdc_ncm_find_endpoints(ctx, ctx->control);
468 
469 	if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
470 	    (ctx->status_ep == NULL))
471 		goto error2;
472 
473 	dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
474 
475 	usb_set_intfdata(ctx->data, dev);
476 	usb_set_intfdata(ctx->control, dev);
477 	usb_set_intfdata(ctx->intf, dev);
478 
479 	if (ctx->ether_desc) {
480 		temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
481 		if (temp)
482 			goto error2;
483 		dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
484 	}
485 
486 
487 	dev->in = usb_rcvbulkpipe(dev->udev,
488 		ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
489 	dev->out = usb_sndbulkpipe(dev->udev,
490 		ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
491 	dev->status = ctx->status_ep;
492 	dev->rx_urb_size = ctx->rx_max;
493 
494 	ctx->tx_speed = ctx->rx_speed = 0;
495 	return 0;
496 
497 error2:
498 	usb_set_intfdata(ctx->control, NULL);
499 	usb_set_intfdata(ctx->data, NULL);
500 	usb_driver_release_interface(driver, ctx->data);
501 error:
502 	cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
503 	dev->data[0] = 0;
504 	dev_info(&dev->udev->dev, "bind() failure\n");
505 	return -ENODEV;
506 }
507 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
508 
509 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
510 {
511 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
512 	struct usb_driver *driver = driver_of(intf);
513 
514 	if (ctx == NULL)
515 		return;		/* no setup */
516 
517 	atomic_set(&ctx->stop, 1);
518 
519 	if (hrtimer_active(&ctx->tx_timer))
520 		hrtimer_cancel(&ctx->tx_timer);
521 
522 	tasklet_kill(&ctx->bh);
523 
524 	/* handle devices with combined control and data interface */
525 	if (ctx->control == ctx->data)
526 		ctx->data = NULL;
527 
528 	/* disconnect master --> disconnect slave */
529 	if (intf == ctx->control && ctx->data) {
530 		usb_set_intfdata(ctx->data, NULL);
531 		usb_driver_release_interface(driver, ctx->data);
532 		ctx->data = NULL;
533 
534 	} else if (intf == ctx->data && ctx->control) {
535 		usb_set_intfdata(ctx->control, NULL);
536 		usb_driver_release_interface(driver, ctx->control);
537 		ctx->control = NULL;
538 	}
539 
540 	usb_set_intfdata(ctx->intf, NULL);
541 	cdc_ncm_free(ctx);
542 }
543 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
544 
545 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
546 {
547 	int ret;
548 
549 	/* The MBIM spec defines a NCM compatible default altsetting,
550 	 * which we may have matched:
551 	 *
552 	 *  "Functions that implement both NCM 1.0 and MBIM (an
553 	 *   “NCM/MBIM function”) according to this recommendation
554 	 *   shall provide two alternate settings for the
555 	 *   Communication Interface.  Alternate setting 0, and the
556 	 *   associated class and endpoint descriptors, shall be
557 	 *   constructed according to the rules given for the
558 	 *   Communication Interface in section 5 of [USBNCM10].
559 	 *   Alternate setting 1, and the associated class and
560 	 *   endpoint descriptors, shall be constructed according to
561 	 *   the rules given in section 6 (USB Device Model) of this
562 	 *   specification."
563 	 *
564 	 * Do not bind to such interfaces, allowing cdc_mbim to handle
565 	 * them
566 	 */
567 #if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
568 	if ((intf->num_altsetting == 2) &&
569 	    !usb_set_interface(dev->udev,
570 			       intf->cur_altsetting->desc.bInterfaceNumber,
571 			       CDC_NCM_COMM_ALTSETTING_MBIM) &&
572 	    cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
573 		return -ENODEV;
574 #endif
575 
576 	/* NCM data altsetting is always 1 */
577 	ret = cdc_ncm_bind_common(dev, intf, 1);
578 
579 	/*
580 	 * We should get an event when network connection is "connected" or
581 	 * "disconnected". Set network connection in "disconnected" state
582 	 * (carrier is OFF) during attach, so the IP network stack does not
583 	 * start IPv6 negotiation and more.
584 	 */
585 	netif_carrier_off(dev->net);
586 	return ret;
587 }
588 
589 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
590 {
591 	size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
592 
593 	if (skb->len + align > max)
594 		align = max - skb->len;
595 	if (align && skb_tailroom(skb) >= align)
596 		memset(skb_put(skb, align), 0, align);
597 }
598 
599 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
600  * allocating a new one within skb
601  */
602 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
603 {
604 	struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
605 	struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
606 	size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
607 
608 	/* follow the chain of NDPs, looking for a match */
609 	while (ndpoffset) {
610 		ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
611 		if  (ndp16->dwSignature == sign)
612 			return ndp16;
613 		ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
614 	}
615 
616 	/* align new NDP */
617 	cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
618 
619 	/* verify that there is room for the NDP and the datagram (reserve) */
620 	if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
621 		return NULL;
622 
623 	/* link to it */
624 	if (ndp16)
625 		ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
626 	else
627 		nth16->wNdpIndex = cpu_to_le16(skb->len);
628 
629 	/* push a new empty NDP */
630 	ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
631 	ndp16->dwSignature = sign;
632 	ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
633 	return ndp16;
634 }
635 
636 struct sk_buff *
637 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
638 {
639 	struct usb_cdc_ncm_nth16 *nth16;
640 	struct usb_cdc_ncm_ndp16 *ndp16;
641 	struct sk_buff *skb_out;
642 	u16 n = 0, index, ndplen;
643 	u8 ready2send = 0;
644 
645 	/* if there is a remaining skb, it gets priority */
646 	if (skb != NULL) {
647 		swap(skb, ctx->tx_rem_skb);
648 		swap(sign, ctx->tx_rem_sign);
649 	} else {
650 		ready2send = 1;
651 	}
652 
653 	/* check if we are resuming an OUT skb */
654 	skb_out = ctx->tx_curr_skb;
655 
656 	/* allocate a new OUT skb */
657 	if (!skb_out) {
658 		skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
659 		if (skb_out == NULL) {
660 			if (skb != NULL) {
661 				dev_kfree_skb_any(skb);
662 				ctx->netdev->stats.tx_dropped++;
663 			}
664 			goto exit_no_skb;
665 		}
666 		/* fill out the initial 16-bit NTB header */
667 		nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
668 		nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
669 		nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
670 		nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
671 
672 		/* count total number of frames in this NTB */
673 		ctx->tx_curr_frame_num = 0;
674 	}
675 
676 	for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
677 		/* send any remaining skb first */
678 		if (skb == NULL) {
679 			skb = ctx->tx_rem_skb;
680 			sign = ctx->tx_rem_sign;
681 			ctx->tx_rem_skb = NULL;
682 
683 			/* check for end of skb */
684 			if (skb == NULL)
685 				break;
686 		}
687 
688 		/* get the appropriate NDP for this skb */
689 		ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
690 
691 		/* align beginning of next frame */
692 		cdc_ncm_align_tail(skb_out,  ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
693 
694 		/* check if we had enough room left for both NDP and frame */
695 		if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
696 			if (n == 0) {
697 				/* won't fit, MTU problem? */
698 				dev_kfree_skb_any(skb);
699 				skb = NULL;
700 				ctx->netdev->stats.tx_dropped++;
701 			} else {
702 				/* no room for skb - store for later */
703 				if (ctx->tx_rem_skb != NULL) {
704 					dev_kfree_skb_any(ctx->tx_rem_skb);
705 					ctx->netdev->stats.tx_dropped++;
706 				}
707 				ctx->tx_rem_skb = skb;
708 				ctx->tx_rem_sign = sign;
709 				skb = NULL;
710 				ready2send = 1;
711 			}
712 			break;
713 		}
714 
715 		/* calculate frame number withing this NDP */
716 		ndplen = le16_to_cpu(ndp16->wLength);
717 		index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
718 
719 		/* OK, add this skb */
720 		ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
721 		ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
722 		ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
723 		memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
724 		dev_kfree_skb_any(skb);
725 		skb = NULL;
726 
727 		/* send now if this NDP is full */
728 		if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
729 			ready2send = 1;
730 			break;
731 		}
732 	}
733 
734 	/* free up any dangling skb */
735 	if (skb != NULL) {
736 		dev_kfree_skb_any(skb);
737 		skb = NULL;
738 		ctx->netdev->stats.tx_dropped++;
739 	}
740 
741 	ctx->tx_curr_frame_num = n;
742 
743 	if (n == 0) {
744 		/* wait for more frames */
745 		/* push variables */
746 		ctx->tx_curr_skb = skb_out;
747 		goto exit_no_skb;
748 
749 	} else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
750 		/* wait for more frames */
751 		/* push variables */
752 		ctx->tx_curr_skb = skb_out;
753 		/* set the pending count */
754 		if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
755 			ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
756 		goto exit_no_skb;
757 
758 	} else {
759 		/* frame goes out */
760 		/* variables will be reset at next call */
761 	}
762 
763 	/*
764 	 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
765 	 * we send buffers as it is. If we get more data, it would be more
766 	 * efficient for USB HS mobile device with DMA engine to receive a full
767 	 * size NTB, than canceling DMA transfer and receiving a short packet.
768 	 */
769 	if (skb_out->len > CDC_NCM_MIN_TX_PKT)
770 		/* final zero padding */
771 		memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, ctx->tx_max - skb_out->len);
772 
773 	/* do we need to prevent a ZLP? */
774 	if (((skb_out->len % le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0) &&
775 	    (skb_out->len < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)) && skb_tailroom(skb_out))
776 		*skb_put(skb_out, 1) = 0;	/* force short packet */
777 
778 	/* set final frame length */
779 	nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
780 	nth16->wBlockLength = cpu_to_le16(skb_out->len);
781 
782 	/* return skb */
783 	ctx->tx_curr_skb = NULL;
784 	ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
785 	return skb_out;
786 
787 exit_no_skb:
788 	/* Start timer, if there is a remaining skb */
789 	if (ctx->tx_curr_skb != NULL)
790 		cdc_ncm_tx_timeout_start(ctx);
791 	return NULL;
792 }
793 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
794 
795 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
796 {
797 	/* start timer, if not already started */
798 	if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
799 		hrtimer_start(&ctx->tx_timer,
800 				ktime_set(0, CDC_NCM_TIMER_INTERVAL),
801 				HRTIMER_MODE_REL);
802 }
803 
804 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
805 {
806 	struct cdc_ncm_ctx *ctx =
807 			container_of(timer, struct cdc_ncm_ctx, tx_timer);
808 
809 	if (!atomic_read(&ctx->stop))
810 		tasklet_schedule(&ctx->bh);
811 	return HRTIMER_NORESTART;
812 }
813 
814 static void cdc_ncm_txpath_bh(unsigned long param)
815 {
816 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
817 
818 	spin_lock_bh(&ctx->mtx);
819 	if (ctx->tx_timer_pending != 0) {
820 		ctx->tx_timer_pending--;
821 		cdc_ncm_tx_timeout_start(ctx);
822 		spin_unlock_bh(&ctx->mtx);
823 	} else if (ctx->netdev != NULL) {
824 		spin_unlock_bh(&ctx->mtx);
825 		netif_tx_lock_bh(ctx->netdev);
826 		usbnet_start_xmit(NULL, ctx->netdev);
827 		netif_tx_unlock_bh(ctx->netdev);
828 	} else {
829 		spin_unlock_bh(&ctx->mtx);
830 	}
831 }
832 
833 static struct sk_buff *
834 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
835 {
836 	struct sk_buff *skb_out;
837 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
838 
839 	/*
840 	 * The Ethernet API we are using does not support transmitting
841 	 * multiple Ethernet frames in a single call. This driver will
842 	 * accumulate multiple Ethernet frames and send out a larger
843 	 * USB frame when the USB buffer is full or when a single jiffies
844 	 * timeout happens.
845 	 */
846 	if (ctx == NULL)
847 		goto error;
848 
849 	spin_lock_bh(&ctx->mtx);
850 	skb_out = cdc_ncm_fill_tx_frame(ctx, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
851 	spin_unlock_bh(&ctx->mtx);
852 	return skb_out;
853 
854 error:
855 	if (skb != NULL)
856 		dev_kfree_skb_any(skb);
857 
858 	return NULL;
859 }
860 
861 /* verify NTB header and return offset of first NDP, or negative error */
862 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
863 {
864 	struct usb_cdc_ncm_nth16 *nth16;
865 	int len;
866 	int ret = -EINVAL;
867 
868 	if (ctx == NULL)
869 		goto error;
870 
871 	if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
872 					sizeof(struct usb_cdc_ncm_ndp16))) {
873 		pr_debug("frame too short\n");
874 		goto error;
875 	}
876 
877 	nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
878 
879 	if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
880 		pr_debug("invalid NTH16 signature <%u>\n",
881 					le32_to_cpu(nth16->dwSignature));
882 		goto error;
883 	}
884 
885 	len = le16_to_cpu(nth16->wBlockLength);
886 	if (len > ctx->rx_max) {
887 		pr_debug("unsupported NTB block length %u/%u\n", len,
888 								ctx->rx_max);
889 		goto error;
890 	}
891 
892 	if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
893 		(ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
894 		!((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
895 		pr_debug("sequence number glitch prev=%d curr=%d\n",
896 				ctx->rx_seq, le16_to_cpu(nth16->wSequence));
897 	}
898 	ctx->rx_seq = le16_to_cpu(nth16->wSequence);
899 
900 	ret = le16_to_cpu(nth16->wNdpIndex);
901 error:
902 	return ret;
903 }
904 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
905 
906 /* verify NDP header and return number of datagrams, or negative error */
907 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
908 {
909 	struct usb_cdc_ncm_ndp16 *ndp16;
910 	int ret = -EINVAL;
911 
912 	if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
913 		pr_debug("invalid NDP offset  <%u>\n", ndpoffset);
914 		goto error;
915 	}
916 	ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
917 
918 	if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
919 		pr_debug("invalid DPT16 length <%u>\n",
920 					le32_to_cpu(ndp16->dwSignature));
921 		goto error;
922 	}
923 
924 	ret = ((le16_to_cpu(ndp16->wLength) -
925 					sizeof(struct usb_cdc_ncm_ndp16)) /
926 					sizeof(struct usb_cdc_ncm_dpe16));
927 	ret--; /* we process NDP entries except for the last one */
928 
929 	if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
930 								skb_in->len) {
931 		pr_debug("Invalid nframes = %d\n", ret);
932 		ret = -EINVAL;
933 	}
934 
935 error:
936 	return ret;
937 }
938 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
939 
940 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
941 {
942 	struct sk_buff *skb;
943 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
944 	int len;
945 	int nframes;
946 	int x;
947 	int offset;
948 	struct usb_cdc_ncm_ndp16 *ndp16;
949 	struct usb_cdc_ncm_dpe16 *dpe16;
950 	int ndpoffset;
951 	int loopcount = 50; /* arbitrary max preventing infinite loop */
952 
953 	ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
954 	if (ndpoffset < 0)
955 		goto error;
956 
957 next_ndp:
958 	nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
959 	if (nframes < 0)
960 		goto error;
961 
962 	ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
963 
964 	if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
965 		pr_debug("invalid DPT16 signature <%u>\n",
966 			 le32_to_cpu(ndp16->dwSignature));
967 		goto err_ndp;
968 	}
969 	dpe16 = ndp16->dpe16;
970 
971 	for (x = 0; x < nframes; x++, dpe16++) {
972 		offset = le16_to_cpu(dpe16->wDatagramIndex);
973 		len = le16_to_cpu(dpe16->wDatagramLength);
974 
975 		/*
976 		 * CDC NCM ch. 3.7
977 		 * All entries after first NULL entry are to be ignored
978 		 */
979 		if ((offset == 0) || (len == 0)) {
980 			if (!x)
981 				goto err_ndp; /* empty NTB */
982 			break;
983 		}
984 
985 		/* sanity checking */
986 		if (((offset + len) > skb_in->len) ||
987 				(len > ctx->rx_max) || (len < ETH_HLEN)) {
988 			pr_debug("invalid frame detected (ignored)"
989 					"offset[%u]=%u, length=%u, skb=%p\n",
990 					x, offset, len, skb_in);
991 			if (!x)
992 				goto err_ndp;
993 			break;
994 
995 		} else {
996 			skb = skb_clone(skb_in, GFP_ATOMIC);
997 			if (!skb)
998 				goto error;
999 			skb->len = len;
1000 			skb->data = ((u8 *)skb_in->data) + offset;
1001 			skb_set_tail_pointer(skb, len);
1002 			usbnet_skb_return(dev, skb);
1003 		}
1004 	}
1005 err_ndp:
1006 	/* are there more NDPs to process? */
1007 	ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1008 	if (ndpoffset && loopcount--)
1009 		goto next_ndp;
1010 
1011 	return 1;
1012 error:
1013 	return 0;
1014 }
1015 
1016 static void
1017 cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1018 		     struct usb_cdc_speed_change *data)
1019 {
1020 	uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1021 	uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1022 
1023 	/*
1024 	 * Currently the USB-NET API does not support reporting the actual
1025 	 * device speed. Do print it instead.
1026 	 */
1027 	if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1028 		ctx->tx_speed = tx_speed;
1029 		ctx->rx_speed = rx_speed;
1030 
1031 		if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1032 			printk(KERN_INFO KBUILD_MODNAME
1033 				": %s: %u mbit/s downlink "
1034 				"%u mbit/s uplink\n",
1035 				ctx->netdev->name,
1036 				(unsigned int)(rx_speed / 1000000U),
1037 				(unsigned int)(tx_speed / 1000000U));
1038 		} else {
1039 			printk(KERN_INFO KBUILD_MODNAME
1040 				": %s: %u kbit/s downlink "
1041 				"%u kbit/s uplink\n",
1042 				ctx->netdev->name,
1043 				(unsigned int)(rx_speed / 1000U),
1044 				(unsigned int)(tx_speed / 1000U));
1045 		}
1046 	}
1047 }
1048 
1049 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1050 {
1051 	struct cdc_ncm_ctx *ctx;
1052 	struct usb_cdc_notification *event;
1053 
1054 	ctx = (struct cdc_ncm_ctx *)dev->data[0];
1055 
1056 	if (urb->actual_length < sizeof(*event))
1057 		return;
1058 
1059 	/* test for split data in 8-byte chunks */
1060 	if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1061 		cdc_ncm_speed_change(ctx,
1062 		      (struct usb_cdc_speed_change *)urb->transfer_buffer);
1063 		return;
1064 	}
1065 
1066 	event = urb->transfer_buffer;
1067 
1068 	switch (event->bNotificationType) {
1069 	case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1070 		/*
1071 		 * According to the CDC NCM specification ch.7.1
1072 		 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1073 		 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1074 		 */
1075 		ctx->connected = le16_to_cpu(event->wValue);
1076 
1077 		printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1078 			" %sconnected\n",
1079 			ctx->netdev->name, ctx->connected ? "" : "dis");
1080 
1081 		if (ctx->connected)
1082 			netif_carrier_on(dev->net);
1083 		else {
1084 			netif_carrier_off(dev->net);
1085 			ctx->tx_speed = ctx->rx_speed = 0;
1086 		}
1087 		break;
1088 
1089 	case USB_CDC_NOTIFY_SPEED_CHANGE:
1090 		if (urb->actual_length < (sizeof(*event) +
1091 					sizeof(struct usb_cdc_speed_change)))
1092 			set_bit(EVENT_STS_SPLIT, &dev->flags);
1093 		else
1094 			cdc_ncm_speed_change(ctx,
1095 				(struct usb_cdc_speed_change *) &event[1]);
1096 		break;
1097 
1098 	default:
1099 		dev_err(&dev->udev->dev, "NCM: unexpected "
1100 			"notification 0x%02x!\n", event->bNotificationType);
1101 		break;
1102 	}
1103 }
1104 
1105 static int cdc_ncm_check_connect(struct usbnet *dev)
1106 {
1107 	struct cdc_ncm_ctx *ctx;
1108 
1109 	ctx = (struct cdc_ncm_ctx *)dev->data[0];
1110 	if (ctx == NULL)
1111 		return 1;	/* disconnected */
1112 
1113 	return !ctx->connected;
1114 }
1115 
1116 static int
1117 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1118 {
1119 	return usbnet_probe(udev, prod);
1120 }
1121 
1122 static void cdc_ncm_disconnect(struct usb_interface *intf)
1123 {
1124 	struct usbnet *dev = usb_get_intfdata(intf);
1125 
1126 	if (dev == NULL)
1127 		return;		/* already disconnected */
1128 
1129 	usbnet_disconnect(intf);
1130 }
1131 
1132 static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1133 {
1134 	dev->intf->needs_remote_wakeup = status;
1135 	return 0;
1136 }
1137 
1138 static const struct driver_info cdc_ncm_info = {
1139 	.description = "CDC NCM",
1140 	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1141 	.bind = cdc_ncm_bind,
1142 	.unbind = cdc_ncm_unbind,
1143 	.check_connect = cdc_ncm_check_connect,
1144 	.manage_power = cdc_ncm_manage_power,
1145 	.status = cdc_ncm_status,
1146 	.rx_fixup = cdc_ncm_rx_fixup,
1147 	.tx_fixup = cdc_ncm_tx_fixup,
1148 };
1149 
1150 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1151 static const struct driver_info wwan_info = {
1152 	.description = "Mobile Broadband Network Device",
1153 	.flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1154 			| FLAG_WWAN,
1155 	.bind = cdc_ncm_bind,
1156 	.unbind = cdc_ncm_unbind,
1157 	.check_connect = cdc_ncm_check_connect,
1158 	.manage_power = cdc_ncm_manage_power,
1159 	.status = cdc_ncm_status,
1160 	.rx_fixup = cdc_ncm_rx_fixup,
1161 	.tx_fixup = cdc_ncm_tx_fixup,
1162 };
1163 
1164 static const struct usb_device_id cdc_devs[] = {
1165 	/* Ericsson MBM devices like F5521gw */
1166 	{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1167 		| USB_DEVICE_ID_MATCH_VENDOR,
1168 	  .idVendor = 0x0bdb,
1169 	  .bInterfaceClass = USB_CLASS_COMM,
1170 	  .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1171 	  .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1172 	  .driver_info = (unsigned long) &wwan_info,
1173 	},
1174 
1175 	/* Dell branded MBM devices like DW5550 */
1176 	{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1177 		| USB_DEVICE_ID_MATCH_VENDOR,
1178 	  .idVendor = 0x413c,
1179 	  .bInterfaceClass = USB_CLASS_COMM,
1180 	  .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1181 	  .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1182 	  .driver_info = (unsigned long) &wwan_info,
1183 	},
1184 
1185 	/* Toshiba branded MBM devices */
1186 	{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1187 		| USB_DEVICE_ID_MATCH_VENDOR,
1188 	  .idVendor = 0x0930,
1189 	  .bInterfaceClass = USB_CLASS_COMM,
1190 	  .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1191 	  .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1192 	  .driver_info = (unsigned long) &wwan_info,
1193 	},
1194 
1195 	/* Huawei NCM devices disguised as vendor specific */
1196 	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
1197 	  .driver_info = (unsigned long)&wwan_info,
1198 	},
1199 	{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
1200 	  .driver_info = (unsigned long)&wwan_info,
1201 	},
1202 
1203 	/* Generic CDC-NCM devices */
1204 	{ USB_INTERFACE_INFO(USB_CLASS_COMM,
1205 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1206 		.driver_info = (unsigned long)&cdc_ncm_info,
1207 	},
1208 	{
1209 	},
1210 };
1211 MODULE_DEVICE_TABLE(usb, cdc_devs);
1212 
1213 static struct usb_driver cdc_ncm_driver = {
1214 	.name = "cdc_ncm",
1215 	.id_table = cdc_devs,
1216 	.probe = cdc_ncm_probe,
1217 	.disconnect = cdc_ncm_disconnect,
1218 	.suspend = usbnet_suspend,
1219 	.resume = usbnet_resume,
1220 	.reset_resume =	usbnet_resume,
1221 	.supports_autosuspend = 1,
1222 	.disable_hub_initiated_lpm = 1,
1223 };
1224 
1225 module_usb_driver(cdc_ncm_driver);
1226 
1227 MODULE_AUTHOR("Hans Petter Selasky");
1228 MODULE_DESCRIPTION("USB CDC NCM host driver");
1229 MODULE_LICENSE("Dual BSD/GPL");
1230