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