SDK7: kernel updates for Tuxera module
[tomato.git] / release / src-rt-7.x.main / src / linux / linux-2.6.36 / drivers / net / usb / cdc_ncm.c
blobf690a995b1c7dcef18dffd7a66e3a15ea0620bf6
1 /*
2 * cdc_ncm.c
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>
8 * USB Host Driver for Network Control Model (NCM)
9 * http://www.usb.org/developers/devclass_docs/NCM10.zip
11 * The NCM encoding, decoding and initialization logic
12 * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
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:
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.
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.
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 <asm/uaccess.h>
53 #include <linux/usb/usbnet.h>
54 #include <linux/usb/cdc.h>
55 #include <linux/usb/cdc_ncm.h>
57 #define DRIVER_VERSION "14-Mar-2012"
59 #ifdef CONFIG_USB_NET_CDC_MBIM
60 static int prefer_mbim = true;
61 #else
62 static int prefer_mbim = false;
63 #endif
64 module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
65 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
67 static void cdc_ncm_txpath_bh(unsigned long param);
68 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
69 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
70 static struct usb_driver cdc_ncm_driver;
72 static void
73 cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
75 struct usbnet *dev = netdev_priv(net);
77 strncpy(info->driver, dev->driver_name, sizeof(info->driver));
78 strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
79 strncpy(info->fw_version, dev->driver_info->description,
80 sizeof(info->fw_version));
81 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
84 static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
86 u32 val;
87 u8 flags;
88 u8 iface_no;
89 int err;
90 int eth_hlen;
91 u16 ntb_fmt_supported;
92 u32 min_dgram_size;
93 u32 min_hdr_size;
95 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
97 err = usb_control_msg(ctx->udev,
98 usb_rcvctrlpipe(ctx->udev, 0),
99 USB_CDC_GET_NTB_PARAMETERS,
100 USB_TYPE_CLASS | USB_DIR_IN
101 | USB_RECIP_INTERFACE,
102 0, iface_no, &ctx->ncm_parm,
103 sizeof(ctx->ncm_parm), 10000);
104 if (err < 0) {
105 pr_debug("failed GET_NTB_PARAMETERS\n");
106 return 1;
109 /* read correct set of parameters according to device mode */
110 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
111 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
112 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
113 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
114 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
115 /* devices prior to NCM Errata shall set this field to zero */
116 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
117 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
119 eth_hlen = ETH_HLEN;
120 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
121 min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
122 if (ctx->mbim_desc != NULL) {
123 flags = ctx->mbim_desc->bmNetworkCapabilities;
124 eth_hlen = 0;
125 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
126 min_hdr_size = 0;
127 } else if (ctx->func_desc != NULL) {
128 flags = ctx->func_desc->bmNetworkCapabilities;
129 } else {
130 flags = 0;
133 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
134 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
135 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
136 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
137 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
139 /* max count of tx datagrams */
140 if ((ctx->tx_max_datagrams == 0) ||
141 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
142 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
144 /* verify maximum size of received NTB in bytes */
145 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
146 pr_debug("Using min receive length=%d\n",
147 USB_CDC_NCM_NTB_MIN_IN_SIZE);
148 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
151 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
152 pr_debug("Using default maximum receive length=%d\n",
153 CDC_NCM_NTB_MAX_SIZE_RX);
154 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
157 /* inform device about NTB input size changes */
158 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
159 __le32 *dwNtbInMaxSize;
161 dwNtbInMaxSize = kzalloc(sizeof(*dwNtbInMaxSize), GFP_KERNEL);
162 if (!dwNtbInMaxSize) {
163 err = -ENOMEM;
164 goto size_err;
166 *dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
167 err = usb_control_msg(ctx->udev,
168 usb_sndctrlpipe(ctx->udev, 0),
169 USB_CDC_SET_NTB_INPUT_SIZE,
170 USB_TYPE_CLASS | USB_DIR_OUT
171 | USB_RECIP_INTERFACE,
172 0, iface_no, dwNtbInMaxSize, 4, 1000);
173 kfree(dwNtbInMaxSize);
174 size_err:
175 if (err < 0)
176 pr_debug("Setting NTB Input Size failed\n");
179 /* verify maximum size of transmitted NTB in bytes */
180 if ((ctx->tx_max <
181 (min_hdr_size + min_dgram_size)) ||
182 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
183 pr_debug("Using default maximum transmit length=%d\n",
184 CDC_NCM_NTB_MAX_SIZE_TX);
185 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
189 * verify that the structure alignment is:
190 * - power of two
191 * - not greater than the maximum transmit length
192 * - not less than four bytes
194 val = ctx->tx_ndp_modulus;
196 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
197 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
198 pr_debug("Using default alignment: 4 bytes\n");
199 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
203 * verify that the payload alignment is:
204 * - power of two
205 * - not greater than the maximum transmit length
206 * - not less than four bytes
208 val = ctx->tx_modulus;
210 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
211 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
212 pr_debug("Using default transmit modulus: 4 bytes\n");
213 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
216 /* verify the payload remainder */
217 if (ctx->tx_remainder >= ctx->tx_modulus) {
218 pr_debug("Using default transmit remainder: 0 bytes\n");
219 ctx->tx_remainder = 0;
222 /* adjust TX-remainder according to NCM specification. */
223 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
224 (ctx->tx_modulus - 1));
226 /* additional configuration */
228 /* set CRC Mode */
229 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
230 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
231 USB_CDC_SET_CRC_MODE,
232 USB_TYPE_CLASS | USB_DIR_OUT
233 | USB_RECIP_INTERFACE,
234 USB_CDC_NCM_CRC_NOT_APPENDED,
235 iface_no, NULL, 0, 1000);
236 if (err < 0)
237 pr_debug("Setting CRC mode off failed\n");
240 /* set NTB format, if both formats are supported */
241 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
242 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
243 USB_CDC_SET_NTB_FORMAT, USB_TYPE_CLASS
244 | USB_DIR_OUT | USB_RECIP_INTERFACE,
245 USB_CDC_NCM_NTB16_FORMAT,
246 iface_no, NULL, 0, 1000);
247 if (err < 0)
248 pr_debug("Setting NTB format to 16-bit failed\n");
251 ctx->max_datagram_size = min_dgram_size;
253 /* set Max Datagram Size (MTU) */
254 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
255 __le16 *max_datagram_size;
256 u16 eth_max_sz;
257 if (ctx->ether_desc != NULL)
258 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
259 else if (ctx->mbim_desc != NULL)
260 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
261 else
262 goto max_dgram_err;
264 max_datagram_size = kzalloc(sizeof(*max_datagram_size),
265 GFP_KERNEL);
266 if (!max_datagram_size) {
267 err = -ENOMEM;
268 goto max_dgram_err;
271 err = usb_control_msg(ctx->udev, usb_rcvctrlpipe(ctx->udev, 0),
272 USB_CDC_GET_MAX_DATAGRAM_SIZE,
273 USB_TYPE_CLASS | USB_DIR_IN
274 | USB_RECIP_INTERFACE,
275 0, iface_no, max_datagram_size,
276 2, 1000);
277 if (err < 0) {
278 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
279 min_dgram_size);
280 } else {
281 ctx->max_datagram_size =
282 le16_to_cpu(*max_datagram_size);
283 /* Check Eth descriptor value */
284 if (ctx->max_datagram_size > eth_max_sz)
285 ctx->max_datagram_size = eth_max_sz;
287 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
288 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
290 if (ctx->max_datagram_size < min_dgram_size)
291 ctx->max_datagram_size = min_dgram_size;
293 /* if value changed, update device */
294 if (ctx->max_datagram_size !=
295 le16_to_cpu(*max_datagram_size)) {
296 err = usb_control_msg(ctx->udev,
297 usb_sndctrlpipe(ctx->udev, 0),
298 USB_CDC_SET_MAX_DATAGRAM_SIZE,
299 USB_TYPE_CLASS | USB_DIR_OUT
300 | USB_RECIP_INTERFACE,
302 iface_no, max_datagram_size,
303 2, 1000);
304 if (err < 0)
305 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
308 kfree(max_datagram_size);
311 max_dgram_err:
312 if (ctx->netdev->mtu != (ctx->max_datagram_size - eth_hlen))
313 ctx->netdev->mtu = ctx->max_datagram_size - eth_hlen;
315 return 0;
318 static void
319 cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
321 struct usb_host_endpoint *e;
322 u8 ep;
324 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
326 e = intf->cur_altsetting->endpoint + ep;
327 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
328 case USB_ENDPOINT_XFER_INT:
329 if (usb_endpoint_dir_in(&e->desc)) {
330 if (ctx->status_ep == NULL)
331 ctx->status_ep = e;
333 break;
335 case USB_ENDPOINT_XFER_BULK:
336 if (usb_endpoint_dir_in(&e->desc)) {
337 if (ctx->in_ep == NULL)
338 ctx->in_ep = e;
339 } else {
340 if (ctx->out_ep == NULL)
341 ctx->out_ep = e;
343 break;
345 default:
346 break;
351 static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
353 if (ctx == NULL)
354 return;
356 if (ctx->tx_rem_skb != NULL) {
357 dev_kfree_skb_any(ctx->tx_rem_skb);
358 ctx->tx_rem_skb = NULL;
361 if (ctx->tx_curr_skb != NULL) {
362 dev_kfree_skb_any(ctx->tx_curr_skb);
363 ctx->tx_curr_skb = NULL;
366 kfree(ctx);
369 static const struct ethtool_ops cdc_ncm_ethtool_ops = {
370 .get_drvinfo = cdc_ncm_get_drvinfo,
371 #ifdef HAVE_MII
372 .get_settings = usbnet_get_settings,
373 .set_settings = usbnet_set_settings,
374 .nway_reset = usbnet_nway_reset,
375 .get_link = usbnet_get_link,
376 #endif
377 .get_msglevel = usbnet_get_msglevel,
378 .set_msglevel = usbnet_set_msglevel,
381 int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
383 struct cdc_ncm_ctx *ctx;
384 struct usb_driver *driver;
385 u8 *buf;
386 int len;
387 int temp;
388 u8 iface_no;
390 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
391 if (!ctx)
392 return -ENOMEM;
394 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
395 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
396 ctx->bh.data = (unsigned long)ctx;
397 ctx->bh.func = cdc_ncm_txpath_bh;
398 atomic_set(&ctx->stop, 0);
399 spin_lock_init(&ctx->mtx);
400 ctx->netdev = dev->net;
402 /* store ctx pointer in device data field */
403 dev->data[0] = (unsigned long)ctx;
405 /* get some pointers */
406 driver = driver_of(intf);
407 buf = intf->cur_altsetting->extra;
408 len = intf->cur_altsetting->extralen;
410 ctx->udev = dev->udev;
411 ctx->intf = intf;
413 /* parse through descriptors associated with control interface */
414 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
416 if (buf[1] != USB_DT_CS_INTERFACE)
417 goto advance;
419 switch (buf[2]) {
420 case USB_CDC_UNION_TYPE:
421 if (buf[0] < sizeof(*(ctx->union_desc)))
422 break;
424 ctx->union_desc =
425 (const struct usb_cdc_union_desc *)buf;
427 ctx->control = usb_ifnum_to_if(dev->udev,
428 ctx->union_desc->bMasterInterface0);
429 ctx->data = usb_ifnum_to_if(dev->udev,
430 ctx->union_desc->bSlaveInterface0);
431 break;
433 case USB_CDC_ETHERNET_TYPE:
434 if (buf[0] < sizeof(*(ctx->ether_desc)))
435 break;
437 ctx->ether_desc =
438 (const struct usb_cdc_ether_desc *)buf;
439 dev->hard_mtu =
440 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
442 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
443 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
444 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
445 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
446 break;
448 case USB_CDC_NCM_TYPE:
449 if (buf[0] < sizeof(*(ctx->func_desc)))
450 break;
452 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
453 break;
455 case USB_CDC_MBIM_TYPE:
456 if (buf[0] < sizeof(*(ctx->mbim_desc)))
457 break;
459 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
460 break;
462 default:
463 break;
465 advance:
466 /* advance to next descriptor */
467 temp = buf[0];
468 buf += temp;
469 len -= temp;
472 /* some buggy devices have an IAD but no CDC Union */
473 if (!ctx->union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
474 ctx->control = intf;
475 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
476 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
479 /* check if we got everything */
480 if ((ctx->control == NULL) || (ctx->data == NULL) ||
481 ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
482 goto error;
484 /* claim data interface, if different from control */
485 if (ctx->data != ctx->control) {
486 temp = usb_driver_claim_interface(driver, ctx->data, dev);
487 if (temp)
488 goto error;
491 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
493 /* reset data interface */
494 temp = usb_set_interface(dev->udev, iface_no, 0);
495 if (temp)
496 goto error2;
498 /* initialize data interface */
499 if (cdc_ncm_setup(ctx))
500 goto error2;
502 /* configure data interface */
503 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
504 if (temp)
505 goto error2;
507 cdc_ncm_find_endpoints(ctx, ctx->data);
508 cdc_ncm_find_endpoints(ctx, ctx->control);
510 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
511 (ctx->status_ep == NULL))
512 goto error2;
514 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
516 usb_set_intfdata(ctx->data, dev);
517 usb_set_intfdata(ctx->control, dev);
518 usb_set_intfdata(ctx->intf, dev);
520 if (ctx->ether_desc) {
521 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
522 if (temp)
523 goto error2;
524 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
528 dev->in = usb_rcvbulkpipe(dev->udev,
529 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
530 dev->out = usb_sndbulkpipe(dev->udev,
531 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
532 dev->status = ctx->status_ep;
533 dev->rx_urb_size = ctx->rx_max;
535 ctx->tx_speed = ctx->rx_speed = 0;
536 return 0;
538 error2:
539 usb_set_intfdata(ctx->control, NULL);
540 usb_set_intfdata(ctx->data, NULL);
541 if (ctx->data != ctx->control)
542 usb_driver_release_interface(driver, ctx->data);
543 error:
544 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
545 dev->data[0] = 0;
546 dev_info(&dev->udev->dev, "bind() failure\n");
547 return -ENODEV;
549 EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
551 void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
553 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
554 struct usb_driver *driver = driver_of(intf);
556 if (ctx == NULL)
557 return; /* no setup */
559 atomic_set(&ctx->stop, 1);
561 if (hrtimer_active(&ctx->tx_timer))
562 hrtimer_cancel(&ctx->tx_timer);
564 tasklet_kill(&ctx->bh);
566 /* handle devices with combined control and data interface */
567 if (ctx->control == ctx->data)
568 ctx->data = NULL;
570 /* disconnect master --> disconnect slave */
571 if (intf == ctx->control && ctx->data) {
572 usb_set_intfdata(ctx->data, NULL);
573 usb_driver_release_interface(driver, ctx->data);
574 ctx->data = NULL;
576 } else if (intf == ctx->data && ctx->control) {
577 usb_set_intfdata(ctx->control, NULL);
578 usb_driver_release_interface(driver, ctx->control);
579 ctx->control = NULL;
582 usb_set_intfdata(ctx->intf, NULL);
583 cdc_ncm_free(ctx);
585 EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
587 /* Select the MBIM altsetting iff it is preferred and available,
588 * returning the number of the corresponding data interface altsetting
590 u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf)
592 struct usb_host_interface *alt;
594 /* The MBIM spec defines a NCM compatible default altsetting,
595 * which we may have matched:
597 * "Functions that implement both NCM 1.0 and MBIM (an
598 * “NCM/MBIM function”) according to this recommendation
599 * shall provide two alternate settings for the
600 * Communication Interface. Alternate setting 0, and the
601 * associated class and endpoint descriptors, shall be
602 * constructed according to the rules given for the
603 * Communication Interface in section 5 of [USBNCM10].
604 * Alternate setting 1, and the associated class and
605 * endpoint descriptors, shall be constructed according to
606 * the rules given in section 6 (USB Device Model) of this
607 * specification."
609 if (prefer_mbim && intf->num_altsetting == 2) {
610 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
611 if (alt && cdc_ncm_comm_intf_is_mbim(alt) &&
612 !usb_set_interface(dev->udev,
613 intf->cur_altsetting->desc.bInterfaceNumber,
614 CDC_NCM_COMM_ALTSETTING_MBIM))
615 return CDC_NCM_DATA_ALTSETTING_MBIM;
617 return CDC_NCM_DATA_ALTSETTING_NCM;
619 EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
621 static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
623 int ret;
625 /* MBIM backwards compatible function? */
626 cdc_ncm_select_altsetting(dev, intf);
627 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
628 return -ENODEV;
630 /* NCM data altsetting is always 1 */
631 ret = cdc_ncm_bind_common(dev, intf, 1);
634 * We should get an event when network connection is "connected" or
635 * "disconnected". Set network connection in "disconnected" state
636 * (carrier is OFF) during attach, so the IP network stack does not
637 * start IPv6 negotiation and more.
639 netif_carrier_off(dev->net);
640 return ret;
643 static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
645 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
647 if (skb->len + align > max)
648 align = max - skb->len;
649 if (align && skb_tailroom(skb) >= align)
650 memset(skb_put(skb, align), 0, align);
653 /* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
654 * allocating a new one within skb
656 static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
658 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
659 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
660 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
662 /* follow the chain of NDPs, looking for a match */
663 while (ndpoffset) {
664 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
665 if (ndp16->dwSignature == sign)
666 return ndp16;
667 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
670 /* align new NDP */
671 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
673 /* verify that there is room for the NDP and the datagram (reserve) */
674 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
675 return NULL;
677 /* link to it */
678 if (ndp16)
679 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
680 else
681 nth16->wNdpIndex = cpu_to_le16(skb->len);
683 /* push a new empty NDP */
684 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
685 ndp16->dwSignature = sign;
686 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
687 return ndp16;
690 struct sk_buff *
691 cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
693 struct usb_cdc_ncm_nth16 *nth16;
694 struct usb_cdc_ncm_ndp16 *ndp16;
695 struct sk_buff *skb_out;
696 u16 n = 0, index, ndplen;
697 u8 ready2send = 0;
699 /* if there is a remaining skb, it gets priority */
700 if (skb != NULL) {
701 swap(skb, ctx->tx_rem_skb);
702 swap(sign, ctx->tx_rem_sign);
703 } else {
704 ready2send = 1;
707 /* check if we are resuming an OUT skb */
708 skb_out = ctx->tx_curr_skb;
710 /* allocate a new OUT skb */
711 if (!skb_out) {
712 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
713 if (skb_out == NULL) {
714 if (skb != NULL) {
715 dev_kfree_skb_any(skb);
716 ctx->netdev->stats.tx_dropped++;
718 goto exit_no_skb;
720 /* fill out the initial 16-bit NTB header */
721 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
722 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
723 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
724 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
726 /* count total number of frames in this NTB */
727 ctx->tx_curr_frame_num = 0;
730 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
731 /* send any remaining skb first */
732 if (skb == NULL) {
733 skb = ctx->tx_rem_skb;
734 sign = ctx->tx_rem_sign;
735 ctx->tx_rem_skb = NULL;
737 /* check for end of skb */
738 if (skb == NULL)
739 break;
742 /* get the appropriate NDP for this skb */
743 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
745 /* align beginning of next frame */
746 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
748 /* check if we had enough room left for both NDP and frame */
749 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
750 if (n == 0) {
751 /* won't fit, MTU problem? */
752 dev_kfree_skb_any(skb);
753 skb = NULL;
754 ctx->netdev->stats.tx_dropped++;
755 } else {
756 /* no room for skb - store for later */
757 if (ctx->tx_rem_skb != NULL) {
758 dev_kfree_skb_any(ctx->tx_rem_skb);
759 ctx->netdev->stats.tx_dropped++;
761 ctx->tx_rem_skb = skb;
762 ctx->tx_rem_sign = sign;
763 skb = NULL;
764 ready2send = 1;
766 break;
769 /* calculate frame number withing this NDP */
770 ndplen = le16_to_cpu(ndp16->wLength);
771 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
773 /* OK, add this skb */
774 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
775 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
776 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
777 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
778 dev_kfree_skb_any(skb);
779 skb = NULL;
781 /* send now if this NDP is full */
782 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
783 ready2send = 1;
784 break;
788 /* free up any dangling skb */
789 if (skb != NULL) {
790 dev_kfree_skb_any(skb);
791 skb = NULL;
792 ctx->netdev->stats.tx_dropped++;
795 ctx->tx_curr_frame_num = n;
797 if (n == 0) {
798 /* wait for more frames */
799 /* push variables */
800 ctx->tx_curr_skb = skb_out;
801 goto exit_no_skb;
803 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
804 /* wait for more frames */
805 /* push variables */
806 ctx->tx_curr_skb = skb_out;
807 /* set the pending count */
808 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
809 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
810 goto exit_no_skb;
812 } else {
813 /* frame goes out */
814 /* variables will be reset at next call */
818 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
819 * we send buffers as it is. If we get more data, it would be more
820 * efficient for USB HS mobile device with DMA engine to receive a full
821 * size NTB, than canceling DMA transfer and receiving a short packet.
823 if (skb_out->len > CDC_NCM_MIN_TX_PKT)
824 /* final zero padding */
825 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, ctx->tx_max - skb_out->len);
827 /* do we need to prevent a ZLP? */
828 if (((skb_out->len % le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0) &&
829 (skb_out->len < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)) && skb_tailroom(skb_out))
830 *skb_put(skb_out, 1) = 0; /* force short packet */
832 /* set final frame length */
833 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
834 nth16->wBlockLength = cpu_to_le16(skb_out->len);
836 /* return skb */
837 ctx->tx_curr_skb = NULL;
838 ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
839 return skb_out;
841 exit_no_skb:
842 /* Start timer, if there is a remaining skb */
843 if (ctx->tx_curr_skb != NULL)
844 cdc_ncm_tx_timeout_start(ctx);
845 return NULL;
847 EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
849 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
851 /* start timer, if not already started */
852 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
853 hrtimer_start(&ctx->tx_timer,
854 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
855 HRTIMER_MODE_REL);
858 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
860 struct cdc_ncm_ctx *ctx =
861 container_of(timer, struct cdc_ncm_ctx, tx_timer);
863 if (!atomic_read(&ctx->stop))
864 tasklet_schedule(&ctx->bh);
865 return HRTIMER_NORESTART;
868 static void cdc_ncm_txpath_bh(unsigned long param)
870 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
872 spin_lock_bh(&ctx->mtx);
873 if (ctx->tx_timer_pending != 0) {
874 ctx->tx_timer_pending--;
875 cdc_ncm_tx_timeout_start(ctx);
876 spin_unlock_bh(&ctx->mtx);
877 } else if (ctx->netdev != NULL) {
878 spin_unlock_bh(&ctx->mtx);
879 netif_tx_lock_bh(ctx->netdev);
880 usbnet_start_xmit(NULL, ctx->netdev);
881 netif_tx_unlock_bh(ctx->netdev);
882 } else {
883 spin_unlock_bh(&ctx->mtx);
887 static struct sk_buff *
888 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
890 struct sk_buff *skb_out;
891 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
894 * The Ethernet API we are using does not support transmitting
895 * multiple Ethernet frames in a single call. This driver will
896 * accumulate multiple Ethernet frames and send out a larger
897 * USB frame when the USB buffer is full or when a single jiffies
898 * timeout happens.
900 if (ctx == NULL)
901 goto error;
903 spin_lock_bh(&ctx->mtx);
904 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
905 spin_unlock_bh(&ctx->mtx);
906 return skb_out;
908 error:
909 if (skb != NULL)
910 dev_kfree_skb_any(skb);
912 return NULL;
915 /* verify NTB header and return offset of first NDP, or negative error */
916 int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
918 struct usb_cdc_ncm_nth16 *nth16;
919 int len;
920 int ret = -EINVAL;
922 if (ctx == NULL)
923 goto error;
925 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
926 sizeof(struct usb_cdc_ncm_ndp16))) {
927 pr_debug("frame too short\n");
928 goto error;
931 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
933 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
934 pr_debug("invalid NTH16 signature <%u>\n",
935 le32_to_cpu(nth16->dwSignature));
936 goto error;
939 len = le16_to_cpu(nth16->wBlockLength);
940 if (len > ctx->rx_max) {
941 pr_debug("unsupported NTB block length %u/%u\n", len,
942 ctx->rx_max);
943 goto error;
946 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
947 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
948 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
949 pr_debug("sequence number glitch prev=%d curr=%d\n",
950 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
952 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
954 ret = le16_to_cpu(nth16->wNdpIndex);
955 error:
956 return ret;
958 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
960 /* verify NDP header and return number of datagrams, or negative error */
961 int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
963 struct usb_cdc_ncm_ndp16 *ndp16;
964 int ret = -EINVAL;
966 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
967 pr_debug("invalid NDP offset <%u>\n", ndpoffset);
968 goto error;
970 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
972 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
973 pr_debug("invalid DPT16 length <%u>\n",
974 le32_to_cpu(ndp16->dwSignature));
975 goto error;
978 ret = ((le16_to_cpu(ndp16->wLength) -
979 sizeof(struct usb_cdc_ncm_ndp16)) /
980 sizeof(struct usb_cdc_ncm_dpe16));
981 ret--; /* we process NDP entries except for the last one */
983 if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
984 skb_in->len) {
985 pr_debug("Invalid nframes = %d\n", ret);
986 ret = -EINVAL;
989 error:
990 return ret;
992 EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
994 static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
996 struct sk_buff *skb;
997 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
998 int len;
999 int nframes;
1000 int x;
1001 int offset;
1002 struct usb_cdc_ncm_ndp16 *ndp16;
1003 struct usb_cdc_ncm_dpe16 *dpe16;
1004 int ndpoffset;
1005 int loopcount = 50; /* arbitrary max preventing infinite loop */
1007 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
1008 if (ndpoffset < 0)
1009 goto error;
1011 next_ndp:
1012 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
1013 if (nframes < 0)
1014 goto error;
1016 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
1018 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
1019 pr_debug("invalid DPT16 signature <%u>\n",
1020 le32_to_cpu(ndp16->dwSignature));
1021 goto err_ndp;
1023 dpe16 = ndp16->dpe16;
1025 for (x = 0; x < nframes; x++, dpe16++) {
1026 offset = le16_to_cpu(dpe16->wDatagramIndex);
1027 len = le16_to_cpu(dpe16->wDatagramLength);
1030 * CDC NCM ch. 3.7
1031 * All entries after first NULL entry are to be ignored
1033 if ((offset == 0) || (len == 0)) {
1034 if (!x)
1035 goto err_ndp; /* empty NTB */
1036 break;
1039 /* sanity checking */
1040 if (((offset + len) > skb_in->len) ||
1041 (len > ctx->rx_max) || (len < ETH_HLEN)) {
1042 pr_debug("invalid frame detected (ignored)"
1043 "offset[%u]=%u, length=%u, skb=%p\n",
1044 x, offset, len, skb_in);
1045 if (!x)
1046 goto err_ndp;
1047 break;
1049 } else {
1050 skb = skb_clone(skb_in, GFP_ATOMIC);
1051 if (!skb)
1052 goto error;
1053 skb->len = len;
1054 skb->data = ((u8 *)skb_in->data) + offset;
1055 skb_set_tail_pointer(skb, len);
1056 usbnet_skb_return(dev, skb);
1059 err_ndp:
1060 /* are there more NDPs to process? */
1061 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1062 if (ndpoffset && loopcount--)
1063 goto next_ndp;
1065 return 1;
1066 error:
1067 return 0;
1070 static void
1071 cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1072 struct usb_cdc_speed_change *data)
1074 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1075 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
1078 * Currently the USB-NET API does not support reporting the actual
1079 * device speed. Do print it instead.
1081 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1082 ctx->tx_speed = tx_speed;
1083 ctx->rx_speed = rx_speed;
1085 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1086 printk(KERN_INFO KBUILD_MODNAME
1087 ": %s: %u mbit/s downlink "
1088 "%u mbit/s uplink\n",
1089 ctx->netdev->name,
1090 (unsigned int)(rx_speed / 1000000U),
1091 (unsigned int)(tx_speed / 1000000U));
1092 } else {
1093 printk(KERN_INFO KBUILD_MODNAME
1094 ": %s: %u kbit/s downlink "
1095 "%u kbit/s uplink\n",
1096 ctx->netdev->name,
1097 (unsigned int)(rx_speed / 1000U),
1098 (unsigned int)(tx_speed / 1000U));
1103 static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1105 struct cdc_ncm_ctx *ctx;
1106 struct usb_cdc_notification *event;
1108 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1110 if (urb->actual_length < sizeof(*event))
1111 return;
1113 /* test for split data in 8-byte chunks */
1114 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1115 cdc_ncm_speed_change(ctx,
1116 (struct usb_cdc_speed_change *)urb->transfer_buffer);
1117 return;
1120 event = urb->transfer_buffer;
1122 switch (event->bNotificationType) {
1123 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1125 * According to the CDC NCM specification ch.7.1
1126 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1127 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1129 ctx->connected = le16_to_cpu(event->wValue);
1131 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1132 " %sconnected\n",
1133 ctx->netdev->name, ctx->connected ? "" : "dis");
1135 if (ctx->connected)
1136 netif_carrier_on(dev->net);
1137 else {
1138 netif_carrier_off(dev->net);
1139 ctx->tx_speed = ctx->rx_speed = 0;
1141 break;
1143 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
1144 /* Not implemented yet */
1145 dev_dbg(&dev->udev->dev, "NCM: ResponseAvailable - Value=%d, Index=%d\n",
1146 le16_to_cpu(event->wValue), le16_to_cpu(event->wIndex));
1147 break;
1149 case USB_CDC_NOTIFY_SPEED_CHANGE:
1150 if (urb->actual_length < (sizeof(*event) +
1151 sizeof(struct usb_cdc_speed_change)))
1152 set_bit(EVENT_STS_SPLIT, &dev->flags);
1153 else
1154 cdc_ncm_speed_change(ctx,
1155 (struct usb_cdc_speed_change *) &event[1]);
1156 break;
1158 default:
1159 dev_err(&dev->udev->dev, "NCM: unexpected "
1160 "notification 0x%02x!\n", event->bNotificationType);
1161 break;
1165 static int cdc_ncm_check_connect(struct usbnet *dev)
1167 struct cdc_ncm_ctx *ctx;
1169 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1170 if (ctx == NULL)
1171 return 1; /* disconnected */
1173 return !ctx->connected;
1176 static int
1177 cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1179 return usbnet_probe(udev, prod);
1182 static void cdc_ncm_disconnect(struct usb_interface *intf)
1184 struct usbnet *dev = usb_get_intfdata(intf);
1186 if (dev == NULL)
1187 return; /* already disconnected */
1189 usbnet_disconnect(intf);
1192 static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1194 dev->intf->needs_remote_wakeup = status;
1195 return 0;
1198 static const struct driver_info cdc_ncm_info = {
1199 .description = "CDC NCM",
1200 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1201 .bind = cdc_ncm_bind,
1202 .unbind = cdc_ncm_unbind,
1203 .check_connect = cdc_ncm_check_connect,
1204 // .manage_power = cdc_ncm_manage_power,
1205 .status = cdc_ncm_status,
1206 .rx_fixup = cdc_ncm_rx_fixup,
1207 .tx_fixup = cdc_ncm_tx_fixup,
1210 /* Same as cdc_ncm_info, but with FLAG_WWAN */
1211 static const struct driver_info wwan_info = {
1212 .description = "Mobile Broadband Network Device",
1213 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1214 .bind = cdc_ncm_bind,
1215 .unbind = cdc_ncm_unbind,
1216 .check_connect = cdc_ncm_check_connect,
1217 // .manage_power = cdc_ncm_manage_power,
1218 .status = cdc_ncm_status,
1219 .rx_fixup = cdc_ncm_rx_fixup,
1220 .tx_fixup = cdc_ncm_tx_fixup,
1223 /* Same as wwan_info, but with FLAG_NOARP */
1224 static const struct driver_info wwan_noarp_info = {
1225 .description = "Mobile Broadband Network Device (NO ARP)",
1226 .flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET | FLAG_NOARP,
1227 .bind = cdc_ncm_bind,
1228 .unbind = cdc_ncm_unbind,
1229 .check_connect = cdc_ncm_check_connect,
1230 // .manage_power = cdc_ncm_manage_power,
1231 .status = cdc_ncm_status,
1232 .rx_fixup = cdc_ncm_rx_fixup,
1233 .tx_fixup = cdc_ncm_tx_fixup,
1236 static const struct usb_device_id cdc_devs[] = {
1237 /* Ericsson MBM devices like F5521gw */
1238 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1239 | USB_DEVICE_ID_MATCH_VENDOR,
1240 .idVendor = 0x0bdb,
1241 .bInterfaceClass = USB_CLASS_COMM,
1242 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1243 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1244 .driver_info = (unsigned long) &wwan_info,
1247 /* Dell branded MBM devices like DW5550 */
1248 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1249 | USB_DEVICE_ID_MATCH_VENDOR,
1250 .idVendor = 0x413c,
1251 .bInterfaceClass = USB_CLASS_COMM,
1252 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1253 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1254 .driver_info = (unsigned long) &wwan_info,
1257 /* Toshiba branded MBM devices */
1258 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1259 | USB_DEVICE_ID_MATCH_VENDOR,
1260 .idVendor = 0x0930,
1261 .bInterfaceClass = USB_CLASS_COMM,
1262 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1263 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1264 .driver_info = (unsigned long) &wwan_info,
1267 /* Huawei NCM devices disguised as vendor specific */
1268 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
1269 .driver_info = (unsigned long)&wwan_info,
1271 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
1272 .driver_info = (unsigned long)&wwan_info,
1274 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
1275 .driver_info = (unsigned long)&wwan_info,
1278 /* Infineon(now Intel) HSPA Modem platform */
1279 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1280 USB_CLASS_COMM,
1281 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1282 .driver_info = (unsigned long)&wwan_noarp_info,
1285 /* Generic CDC-NCM devices */
1286 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1287 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1288 .driver_info = (unsigned long)&cdc_ncm_info,
1293 MODULE_DEVICE_TABLE(usb, cdc_devs);
1295 static struct usb_driver cdc_ncm_driver = {
1296 .name = "cdc_ncm",
1297 .id_table = cdc_devs,
1298 .probe = cdc_ncm_probe,
1299 .disconnect = cdc_ncm_disconnect,
1300 .suspend = usbnet_suspend,
1301 .resume = usbnet_resume,
1302 .reset_resume = usbnet_resume,
1303 .supports_autosuspend = 1,
1306 static int __init cdc_ncm_init(void)
1308 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
1309 return usb_register(&cdc_ncm_driver);
1312 module_init(cdc_ncm_init);
1314 static void __exit cdc_ncm_exit(void)
1316 usb_deregister(&cdc_ncm_driver);
1319 module_exit(cdc_ncm_exit);
1321 MODULE_AUTHOR("Hans Petter Selasky");
1322 MODULE_DESCRIPTION("USB CDC NCM host driver");
1323 MODULE_LICENSE("Dual BSD/GPL");