blob: 17b4ba03cb55c83c9eb3c4baf857e966df3bdb6f [file] [log] [blame]
Alexey Orishko900d4952010-11-29 23:23:28 +00001/*
2 * cdc_ncm.c
3 *
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +00004 * Copyright (C) ST-Ericsson 2010-2012
Alexey Orishko900d4952010-11-29 23:23:28 +00005 * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
6 * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
7 *
8 * USB Host Driver for Network Control Model (NCM)
9 * http://www.usb.org/developers/devclass_docs/NCM10.zip
10 *
11 * The NCM encoding, decoding and initialization logic
12 * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
13 *
14 * This software is available to you under a choice of one of two
15 * licenses. You may choose this file to be licensed under the terms
16 * of the GNU General Public License (GPL) Version 2 or the 2-clause
17 * BSD license listed below:
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#include <linux/module.h>
42#include <linux/init.h>
43#include <linux/netdevice.h>
44#include <linux/ctype.h>
45#include <linux/ethtool.h>
46#include <linux/workqueue.h>
47#include <linux/mii.h>
48#include <linux/crc32.h>
49#include <linux/usb.h>
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000050#include <linux/hrtimer.h>
Alexey Orishko900d4952010-11-29 23:23:28 +000051#include <linux/atomic.h>
52#include <linux/usb/usbnet.h>
53#include <linux/usb/cdc.h>
Bjørn Morkc91ce3b2012-10-22 10:56:35 +000054#include <linux/usb/cdc_ncm.h>
Alexey Orishko900d4952010-11-29 23:23:28 +000055
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000056#define DRIVER_VERSION "14-Mar-2012"
Alexey Orishko900d4952010-11-29 23:23:28 +000057
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000058static void cdc_ncm_txpath_bh(unsigned long param);
59static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
60static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +000061static struct usb_driver cdc_ncm_driver;
Alexey Orishko900d4952010-11-29 23:23:28 +000062
63static void
64cdc_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
Alexey Orishko900d4952010-11-29 23:23:28 +000075static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
76{
Alexey Orishko900d4952010-11-29 23:23:28 +000077 u32 val;
Alexey Orishko900d4952010-11-29 23:23:28 +000078 u8 flags;
79 u8 iface_no;
80 int err;
Greg Suarez2d1c4312012-10-22 10:56:30 +000081 int eth_hlen;
Alexey Orishko84e77a82011-02-07 09:45:10 +000082 u16 ntb_fmt_supported;
Greg Suarez2d1c4312012-10-22 10:56:30 +000083 u32 min_dgram_size;
84 u32 min_hdr_size;
Alexey Orishko900d4952010-11-29 23:23:28 +000085
86 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
87
Giuseppe Scrivano36c35412011-08-03 22:10:29 +000088 err = usb_control_msg(ctx->udev,
89 usb_rcvctrlpipe(ctx->udev, 0),
90 USB_CDC_GET_NTB_PARAMETERS,
91 USB_TYPE_CLASS | USB_DIR_IN
92 | USB_RECIP_INTERFACE,
93 0, iface_no, &ctx->ncm_parm,
94 sizeof(ctx->ncm_parm), 10000);
95 if (err < 0) {
Alexey Orishko900d4952010-11-29 23:23:28 +000096 pr_debug("failed GET_NTB_PARAMETERS\n");
97 return 1;
98 }
99
100 /* read correct set of parameters according to device mode */
101 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
102 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
103 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
104 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
105 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000106 /* devices prior to NCM Errata shall set this field to zero */
107 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
108 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
Alexey Orishko900d4952010-11-29 23:23:28 +0000109
Greg Suarez2d1c4312012-10-22 10:56:30 +0000110 eth_hlen = ETH_HLEN;
111 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
112 min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
113 if (ctx->mbim_desc != NULL) {
114 flags = ctx->mbim_desc->bmNetworkCapabilities;
115 eth_hlen = 0;
116 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
117 min_hdr_size = 0;
118 } else if (ctx->func_desc != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000119 flags = ctx->func_desc->bmNetworkCapabilities;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000120 } else {
Alexey Orishko900d4952010-11-29 23:23:28 +0000121 flags = 0;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000122 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000123
124 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
125 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
Alexey Orishko84e77a82011-02-07 09:45:10 +0000126 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
Alexey Orishko900d4952010-11-29 23:23:28 +0000127 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
Alexey Orishko84e77a82011-02-07 09:45:10 +0000128 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
Alexey Orishko900d4952010-11-29 23:23:28 +0000129
Alexey Orishko84e77a82011-02-07 09:45:10 +0000130 /* max count of tx datagrams */
131 if ((ctx->tx_max_datagrams == 0) ||
132 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
133 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
Alexey Orishko900d4952010-11-29 23:23:28 +0000134
135 /* verify maximum size of received NTB in bytes */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000136 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
137 pr_debug("Using min receive length=%d\n",
138 USB_CDC_NCM_NTB_MIN_IN_SIZE);
139 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
140 }
141
142 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000143 pr_debug("Using default maximum receive length=%d\n",
144 CDC_NCM_NTB_MAX_SIZE_RX);
145 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
146 }
147
Alexey Orishko84e77a82011-02-07 09:45:10 +0000148 /* inform device about NTB input size changes */
149 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
Bjørn Morkdad957d2012-10-22 10:56:28 +0000150 __le32 *dwNtbInMaxSize;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000151
Bjørn Morkdad957d2012-10-22 10:56:28 +0000152 dwNtbInMaxSize = kzalloc(sizeof(*dwNtbInMaxSize), GFP_KERNEL);
153 if (!dwNtbInMaxSize) {
154 err = -ENOMEM;
155 goto size_err;
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000156 }
Bjørn Morkdad957d2012-10-22 10:56:28 +0000157 *dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
158 err = usb_control_msg(ctx->udev,
159 usb_sndctrlpipe(ctx->udev, 0),
160 USB_CDC_SET_NTB_INPUT_SIZE,
161 USB_TYPE_CLASS | USB_DIR_OUT
162 | USB_RECIP_INTERFACE,
163 0, iface_no, dwNtbInMaxSize, 4, 1000);
164 kfree(dwNtbInMaxSize);
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000165size_err:
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000166 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000167 pr_debug("Setting NTB Input Size failed\n");
168 }
169
Alexey Orishko900d4952010-11-29 23:23:28 +0000170 /* verify maximum size of transmitted NTB in bytes */
171 if ((ctx->tx_max <
Greg Suarez2d1c4312012-10-22 10:56:30 +0000172 (min_hdr_size + min_dgram_size)) ||
Alexey Orishko900d4952010-11-29 23:23:28 +0000173 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
174 pr_debug("Using default maximum transmit length=%d\n",
175 CDC_NCM_NTB_MAX_SIZE_TX);
176 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
177 }
178
179 /*
180 * verify that the structure alignment is:
181 * - power of two
182 * - not greater than the maximum transmit length
183 * - not less than four bytes
184 */
185 val = ctx->tx_ndp_modulus;
186
187 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
188 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
189 pr_debug("Using default alignment: 4 bytes\n");
190 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
191 }
192
193 /*
194 * verify that the payload alignment is:
195 * - power of two
196 * - not greater than the maximum transmit length
197 * - not less than four bytes
198 */
199 val = ctx->tx_modulus;
200
201 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
202 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
203 pr_debug("Using default transmit modulus: 4 bytes\n");
204 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
205 }
206
207 /* verify the payload remainder */
208 if (ctx->tx_remainder >= ctx->tx_modulus) {
209 pr_debug("Using default transmit remainder: 0 bytes\n");
210 ctx->tx_remainder = 0;
211 }
212
213 /* adjust TX-remainder according to NCM specification. */
Greg Suarez2d1c4312012-10-22 10:56:30 +0000214 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
215 (ctx->tx_modulus - 1));
Alexey Orishko900d4952010-11-29 23:23:28 +0000216
217 /* additional configuration */
218
219 /* set CRC Mode */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000220 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000221 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
222 USB_CDC_SET_CRC_MODE,
223 USB_TYPE_CLASS | USB_DIR_OUT
224 | USB_RECIP_INTERFACE,
225 USB_CDC_NCM_CRC_NOT_APPENDED,
226 iface_no, NULL, 0, 1000);
227 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000228 pr_debug("Setting CRC mode off failed\n");
229 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000230
Alexey Orishko84e77a82011-02-07 09:45:10 +0000231 /* set NTB format, if both formats are supported */
232 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000233 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
234 USB_CDC_SET_NTB_FORMAT, USB_TYPE_CLASS
235 | USB_DIR_OUT | USB_RECIP_INTERFACE,
236 USB_CDC_NCM_NTB16_FORMAT,
237 iface_no, NULL, 0, 1000);
238 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000239 pr_debug("Setting NTB format to 16-bit failed\n");
240 }
241
Greg Suarez2d1c4312012-10-22 10:56:30 +0000242 ctx->max_datagram_size = min_dgram_size;
Alexey Orishko900d4952010-11-29 23:23:28 +0000243
244 /* set Max Datagram Size (MTU) */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000245 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000246 __le16 *max_datagram_size;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000247 u16 eth_max_sz;
248 if (ctx->ether_desc != NULL)
249 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
250 else if (ctx->mbim_desc != NULL)
251 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
252 else
253 goto max_dgram_err;
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000254
255 max_datagram_size = kzalloc(sizeof(*max_datagram_size),
256 GFP_KERNEL);
257 if (!max_datagram_size) {
258 err = -ENOMEM;
259 goto max_dgram_err;
260 }
261
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000262 err = usb_control_msg(ctx->udev, usb_rcvctrlpipe(ctx->udev, 0),
263 USB_CDC_GET_MAX_DATAGRAM_SIZE,
264 USB_TYPE_CLASS | USB_DIR_IN
265 | USB_RECIP_INTERFACE,
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000266 0, iface_no, max_datagram_size,
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000267 2, 1000);
268 if (err < 0) {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000269 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
Greg Suarez2d1c4312012-10-22 10:56:30 +0000270 min_dgram_size);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000271 } else {
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000272 ctx->max_datagram_size =
273 le16_to_cpu(*max_datagram_size);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000274 /* Check Eth descriptor value */
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000275 if (ctx->max_datagram_size > eth_max_sz)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000276 ctx->max_datagram_size = eth_max_sz;
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000277
278 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
Greg Suarez2d1c4312012-10-22 10:56:30 +0000279 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000280
Greg Suarez2d1c4312012-10-22 10:56:30 +0000281 if (ctx->max_datagram_size < min_dgram_size)
282 ctx->max_datagram_size = min_dgram_size;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000283
284 /* if value changed, update device */
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000285 if (ctx->max_datagram_size !=
286 le16_to_cpu(*max_datagram_size)) {
287 err = usb_control_msg(ctx->udev,
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000288 usb_sndctrlpipe(ctx->udev, 0),
289 USB_CDC_SET_MAX_DATAGRAM_SIZE,
290 USB_TYPE_CLASS | USB_DIR_OUT
291 | USB_RECIP_INTERFACE,
292 0,
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000293 iface_no, max_datagram_size,
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000294 2, 1000);
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000295 if (err < 0)
296 pr_debug("SET_MAX_DGRAM_SIZE failed\n");
297 }
Alexey Orishko84e77a82011-02-07 09:45:10 +0000298 }
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000299 kfree(max_datagram_size);
Alexey Orishko900d4952010-11-29 23:23:28 +0000300 }
301
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000302max_dgram_err:
Greg Suarez2d1c4312012-10-22 10:56:30 +0000303 if (ctx->netdev->mtu != (ctx->max_datagram_size - eth_hlen))
304 ctx->netdev->mtu = ctx->max_datagram_size - eth_hlen;
Alexey Orishko900d4952010-11-29 23:23:28 +0000305
306 return 0;
307}
308
309static void
310cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
311{
312 struct usb_host_endpoint *e;
313 u8 ep;
314
315 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
316
317 e = intf->cur_altsetting->endpoint + ep;
318 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
319 case USB_ENDPOINT_XFER_INT:
320 if (usb_endpoint_dir_in(&e->desc)) {
321 if (ctx->status_ep == NULL)
322 ctx->status_ep = e;
323 }
324 break;
325
326 case USB_ENDPOINT_XFER_BULK:
327 if (usb_endpoint_dir_in(&e->desc)) {
328 if (ctx->in_ep == NULL)
329 ctx->in_ep = e;
330 } else {
331 if (ctx->out_ep == NULL)
332 ctx->out_ep = e;
333 }
334 break;
335
336 default:
337 break;
338 }
339 }
340}
341
342static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
343{
344 if (ctx == NULL)
345 return;
346
Alexey Orishko900d4952010-11-29 23:23:28 +0000347 if (ctx->tx_rem_skb != NULL) {
348 dev_kfree_skb_any(ctx->tx_rem_skb);
349 ctx->tx_rem_skb = NULL;
350 }
351
352 if (ctx->tx_curr_skb != NULL) {
353 dev_kfree_skb_any(ctx->tx_curr_skb);
354 ctx->tx_curr_skb = NULL;
355 }
356
357 kfree(ctx);
358}
359
Dan Williamsf94898e2012-07-24 08:43:22 +0000360static const struct ethtool_ops cdc_ncm_ethtool_ops = {
361 .get_drvinfo = cdc_ncm_get_drvinfo,
362 .get_link = usbnet_get_link,
363 .get_msglevel = usbnet_get_msglevel,
364 .set_msglevel = usbnet_set_msglevel,
365 .get_settings = usbnet_get_settings,
366 .set_settings = usbnet_set_settings,
367 .nway_reset = usbnet_nway_reset,
368};
369
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000370int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
Alexey Orishko900d4952010-11-29 23:23:28 +0000371{
372 struct cdc_ncm_ctx *ctx;
373 struct usb_driver *driver;
374 u8 *buf;
375 int len;
376 int temp;
377 u8 iface_no;
378
Thomas Meyerc7969842011-11-17 12:43:40 +0000379 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000380 if (ctx == NULL)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000381 return -ENODEV;
Alexey Orishko900d4952010-11-29 23:23:28 +0000382
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000383 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
384 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
385 ctx->bh.data = (unsigned long)ctx;
386 ctx->bh.func = cdc_ncm_txpath_bh;
387 atomic_set(&ctx->stop, 0);
Alexey Orishko900d4952010-11-29 23:23:28 +0000388 spin_lock_init(&ctx->mtx);
389 ctx->netdev = dev->net;
390
391 /* store ctx pointer in device data field */
392 dev->data[0] = (unsigned long)ctx;
393
394 /* get some pointers */
395 driver = driver_of(intf);
396 buf = intf->cur_altsetting->extra;
397 len = intf->cur_altsetting->extralen;
398
399 ctx->udev = dev->udev;
400 ctx->intf = intf;
401
402 /* parse through descriptors associated with control interface */
403 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
404
405 if (buf[1] != USB_DT_CS_INTERFACE)
406 goto advance;
407
408 switch (buf[2]) {
409 case USB_CDC_UNION_TYPE:
410 if (buf[0] < sizeof(*(ctx->union_desc)))
411 break;
412
413 ctx->union_desc =
414 (const struct usb_cdc_union_desc *)buf;
415
416 ctx->control = usb_ifnum_to_if(dev->udev,
417 ctx->union_desc->bMasterInterface0);
418 ctx->data = usb_ifnum_to_if(dev->udev,
419 ctx->union_desc->bSlaveInterface0);
420 break;
421
422 case USB_CDC_ETHERNET_TYPE:
423 if (buf[0] < sizeof(*(ctx->ether_desc)))
424 break;
425
426 ctx->ether_desc =
427 (const struct usb_cdc_ether_desc *)buf;
Alexey Orishko900d4952010-11-29 23:23:28 +0000428 dev->hard_mtu =
429 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
430
Alexey Orishko84e77a82011-02-07 09:45:10 +0000431 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
432 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
433 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
434 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
Alexey Orishko900d4952010-11-29 23:23:28 +0000435 break;
436
437 case USB_CDC_NCM_TYPE:
438 if (buf[0] < sizeof(*(ctx->func_desc)))
439 break;
440
441 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
442 break;
443
Greg Suarez38396e42012-10-22 10:56:31 +0000444 case USB_CDC_MBIM_TYPE:
445 if (buf[0] < sizeof(*(ctx->mbim_desc)))
446 break;
447
448 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
449 break;
450
Alexey Orishko900d4952010-11-29 23:23:28 +0000451 default:
452 break;
453 }
454advance:
455 /* advance to next descriptor */
456 temp = buf[0];
457 buf += temp;
458 len -= temp;
459 }
460
461 /* check if we got everything */
462 if ((ctx->control == NULL) || (ctx->data == NULL) ||
Greg Suarez38396e42012-10-22 10:56:31 +0000463 ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
Alexey Orishko900d4952010-11-29 23:23:28 +0000464 goto error;
465
466 /* claim interfaces, if any */
Alexey Orishko19694ac2011-05-24 05:26:13 +0000467 temp = usb_driver_claim_interface(driver, ctx->data, dev);
468 if (temp)
469 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +0000470
471 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
472
473 /* reset data interface */
474 temp = usb_set_interface(dev->udev, iface_no, 0);
475 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000476 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000477
478 /* initialize data interface */
479 if (cdc_ncm_setup(ctx))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000480 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000481
482 /* configure data interface */
Greg Suarez38396e42012-10-22 10:56:31 +0000483 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
Alexey Orishko900d4952010-11-29 23:23:28 +0000484 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000485 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000486
487 cdc_ncm_find_endpoints(ctx, ctx->data);
488 cdc_ncm_find_endpoints(ctx, ctx->control);
489
490 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
491 (ctx->status_ep == NULL))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000492 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000493
494 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
495
496 usb_set_intfdata(ctx->data, dev);
497 usb_set_intfdata(ctx->control, dev);
498 usb_set_intfdata(ctx->intf, dev);
499
Greg Suarez38396e42012-10-22 10:56:31 +0000500 if (ctx->ether_desc) {
501 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
502 if (temp)
503 goto error2;
504 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
505 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000506
Alexey Orishko900d4952010-11-29 23:23:28 +0000507
508 dev->in = usb_rcvbulkpipe(dev->udev,
509 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
510 dev->out = usb_sndbulkpipe(dev->udev,
511 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
512 dev->status = ctx->status_ep;
513 dev->rx_urb_size = ctx->rx_max;
514
Alexey Orishko900d4952010-11-29 23:23:28 +0000515 ctx->tx_speed = ctx->rx_speed = 0;
516 return 0;
517
Alexey Orishko19694ac2011-05-24 05:26:13 +0000518error2:
519 usb_set_intfdata(ctx->control, NULL);
520 usb_set_intfdata(ctx->data, NULL);
521 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko900d4952010-11-29 23:23:28 +0000522error:
523 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
524 dev->data[0] = 0;
Alexey Orishko19694ac2011-05-24 05:26:13 +0000525 dev_info(&dev->udev->dev, "bind() failure\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000526 return -ENODEV;
527}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000528EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
Alexey Orishko900d4952010-11-29 23:23:28 +0000529
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000530void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
Alexey Orishko900d4952010-11-29 23:23:28 +0000531{
532 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko19694ac2011-05-24 05:26:13 +0000533 struct usb_driver *driver = driver_of(intf);
Alexey Orishko900d4952010-11-29 23:23:28 +0000534
535 if (ctx == NULL)
536 return; /* no setup */
537
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000538 atomic_set(&ctx->stop, 1);
539
540 if (hrtimer_active(&ctx->tx_timer))
541 hrtimer_cancel(&ctx->tx_timer);
542
543 tasklet_kill(&ctx->bh);
544
Alexey Orishko19694ac2011-05-24 05:26:13 +0000545 /* disconnect master --> disconnect slave */
546 if (intf == ctx->control && ctx->data) {
547 usb_set_intfdata(ctx->data, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000548 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000549 ctx->data = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000550
Alexey Orishko19694ac2011-05-24 05:26:13 +0000551 } else if (intf == ctx->data && ctx->control) {
552 usb_set_intfdata(ctx->control, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000553 usb_driver_release_interface(driver, ctx->control);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000554 ctx->control = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000555 }
556
Alexey Orishko19694ac2011-05-24 05:26:13 +0000557 usb_set_intfdata(ctx->intf, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000558 cdc_ncm_free(ctx);
559}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000560EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
Alexey Orishko900d4952010-11-29 23:23:28 +0000561
Greg Suarez38396e42012-10-22 10:56:31 +0000562static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
563{
564 int ret;
565
566 /* NCM data altsetting is always 1 */
567 ret = cdc_ncm_bind_common(dev, intf, 1);
568
569 /*
570 * We should get an event when network connection is "connected" or
571 * "disconnected". Set network connection in "disconnected" state
572 * (carrier is OFF) during attach, so the IP network stack does not
573 * start IPv6 negotiation and more.
574 */
575 netif_carrier_off(dev->net);
576 return ret;
577}
578
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000579static void cdc_ncm_align_tail(struct sk_buff *skb, size_t modulus, size_t remainder, size_t max)
Alexey Orishko900d4952010-11-29 23:23:28 +0000580{
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000581 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
582
583 if (skb->len + align > max)
584 align = max - skb->len;
585 if (align && skb_tailroom(skb) >= align)
586 memset(skb_put(skb, align), 0, align);
587}
588
589/* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
590 * allocating a new one within skb
591 */
592static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
593{
594 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
595 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
596 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
597
598 /* follow the chain of NDPs, looking for a match */
599 while (ndpoffset) {
600 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
601 if (ndp16->dwSignature == sign)
602 return ndp16;
603 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
604 }
605
606 /* align new NDP */
607 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
608
609 /* verify that there is room for the NDP and the datagram (reserve) */
610 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
611 return NULL;
612
613 /* link to it */
614 if (ndp16)
615 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
616 else
617 nth16->wNdpIndex = cpu_to_le16(skb->len);
618
619 /* push a new empty NDP */
620 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
621 ndp16->dwSignature = sign;
622 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
623 return ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000624}
625
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000626struct sk_buff *
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000627cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign)
Alexey Orishko900d4952010-11-29 23:23:28 +0000628{
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000629 struct usb_cdc_ncm_nth16 *nth16;
630 struct usb_cdc_ncm_ndp16 *ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000631 struct sk_buff *skb_out;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000632 u16 n = 0, index, ndplen;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000633 u8 ready2send = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000634
635 /* if there is a remaining skb, it gets priority */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000636 if (skb != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000637 swap(skb, ctx->tx_rem_skb);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000638 swap(sign, ctx->tx_rem_sign);
639 } else {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000640 ready2send = 1;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000641 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000642
643 /* check if we are resuming an OUT skb */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000644 skb_out = ctx->tx_curr_skb;
Alexey Orishko900d4952010-11-29 23:23:28 +0000645
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000646 /* allocate a new OUT skb */
647 if (!skb_out) {
Alexey Orishko6c604082011-05-06 03:01:30 +0000648 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
Alexey Orishko900d4952010-11-29 23:23:28 +0000649 if (skb_out == NULL) {
650 if (skb != NULL) {
651 dev_kfree_skb_any(skb);
652 ctx->netdev->stats.tx_dropped++;
653 }
654 goto exit_no_skb;
655 }
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000656 /* fill out the initial 16-bit NTB header */
657 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
658 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
659 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
660 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
Alexey Orishko900d4952010-11-29 23:23:28 +0000661
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000662 /* count total number of frames in this NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +0000663 ctx->tx_curr_frame_num = 0;
664 }
665
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000666 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
667 /* send any remaining skb first */
Alexey Orishko900d4952010-11-29 23:23:28 +0000668 if (skb == NULL) {
669 skb = ctx->tx_rem_skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000670 sign = ctx->tx_rem_sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000671 ctx->tx_rem_skb = NULL;
672
673 /* check for end of skb */
674 if (skb == NULL)
675 break;
676 }
677
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000678 /* get the appropriate NDP for this skb */
679 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
680
681 /* align beginning of next frame */
682 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
683
684 /* check if we had enough room left for both NDP and frame */
685 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000686 if (n == 0) {
687 /* won't fit, MTU problem? */
688 dev_kfree_skb_any(skb);
689 skb = NULL;
690 ctx->netdev->stats.tx_dropped++;
691 } else {
692 /* no room for skb - store for later */
693 if (ctx->tx_rem_skb != NULL) {
694 dev_kfree_skb_any(ctx->tx_rem_skb);
695 ctx->netdev->stats.tx_dropped++;
696 }
697 ctx->tx_rem_skb = skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000698 ctx->tx_rem_sign = sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000699 skb = NULL;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000700 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000701 }
702 break;
703 }
704
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000705 /* calculate frame number withing this NDP */
706 ndplen = le16_to_cpu(ndp16->wLength);
707 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000708
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000709 /* OK, add this skb */
710 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
711 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
712 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
713 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000714 dev_kfree_skb_any(skb);
715 skb = NULL;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000716
717 /* send now if this NDP is full */
718 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
719 ready2send = 1;
720 break;
721 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000722 }
723
724 /* free up any dangling skb */
725 if (skb != NULL) {
726 dev_kfree_skb_any(skb);
727 skb = NULL;
728 ctx->netdev->stats.tx_dropped++;
729 }
730
731 ctx->tx_curr_frame_num = n;
732
733 if (n == 0) {
734 /* wait for more frames */
735 /* push variables */
736 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +0000737 goto exit_no_skb;
738
Alexey Orishko84e77a82011-02-07 09:45:10 +0000739 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000740 /* wait for more frames */
741 /* push variables */
742 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +0000743 /* set the pending count */
744 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000745 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
Alexey Orishko900d4952010-11-29 23:23:28 +0000746 goto exit_no_skb;
747
748 } else {
749 /* frame goes out */
750 /* variables will be reset at next call */
751 }
752
Alexey Orishko900d4952010-11-29 23:23:28 +0000753 /*
754 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
755 * we send buffers as it is. If we get more data, it would be more
756 * efficient for USB HS mobile device with DMA engine to receive a full
757 * size NTB, than canceling DMA transfer and receiving a short packet.
758 */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000759 if (skb_out->len > CDC_NCM_MIN_TX_PKT)
760 /* final zero padding */
761 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0, ctx->tx_max - skb_out->len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000762
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000763 /* do we need to prevent a ZLP? */
764 if (((skb_out->len % le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0) &&
765 (skb_out->len < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)) && skb_tailroom(skb_out))
766 *skb_put(skb_out, 1) = 0; /* force short packet */
Alexey Orishko900d4952010-11-29 23:23:28 +0000767
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000768 /* set final frame length */
769 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
770 nth16->wBlockLength = cpu_to_le16(skb_out->len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000771
772 /* return skb */
773 ctx->tx_curr_skb = NULL;
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000774 ctx->netdev->stats.tx_packets += ctx->tx_curr_frame_num;
Alexey Orishko900d4952010-11-29 23:23:28 +0000775 return skb_out;
776
777exit_no_skb:
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000778 /* Start timer, if there is a remaining skb */
779 if (ctx->tx_curr_skb != NULL)
780 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000781 return NULL;
782}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000783EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
Alexey Orishko900d4952010-11-29 23:23:28 +0000784
785static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
786{
787 /* start timer, if not already started */
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000788 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
789 hrtimer_start(&ctx->tx_timer,
790 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
791 HRTIMER_MODE_REL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000792}
793
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000794static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
Alexey Orishko900d4952010-11-29 23:23:28 +0000795{
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000796 struct cdc_ncm_ctx *ctx =
797 container_of(timer, struct cdc_ncm_ctx, tx_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +0000798
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000799 if (!atomic_read(&ctx->stop))
800 tasklet_schedule(&ctx->bh);
801 return HRTIMER_NORESTART;
802}
803
804static void cdc_ncm_txpath_bh(unsigned long param)
805{
806 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)param;
807
808 spin_lock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000809 if (ctx->tx_timer_pending != 0) {
810 ctx->tx_timer_pending--;
Alexey Orishko900d4952010-11-29 23:23:28 +0000811 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000812 spin_unlock_bh(&ctx->mtx);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000813 } else if (ctx->netdev != NULL) {
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000814 spin_unlock_bh(&ctx->mtx);
815 netif_tx_lock_bh(ctx->netdev);
Alexey Orishko900d4952010-11-29 23:23:28 +0000816 usbnet_start_xmit(NULL, ctx->netdev);
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000817 netif_tx_unlock_bh(ctx->netdev);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000818 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000819}
820
821static struct sk_buff *
822cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
823{
824 struct sk_buff *skb_out;
825 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko900d4952010-11-29 23:23:28 +0000826
827 /*
828 * The Ethernet API we are using does not support transmitting
829 * multiple Ethernet frames in a single call. This driver will
830 * accumulate multiple Ethernet frames and send out a larger
831 * USB frame when the USB buffer is full or when a single jiffies
832 * timeout happens.
833 */
834 if (ctx == NULL)
835 goto error;
836
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000837 spin_lock_bh(&ctx->mtx);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000838 skb_out = cdc_ncm_fill_tx_frame(ctx, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000839 spin_unlock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000840 return skb_out;
841
842error:
843 if (skb != NULL)
844 dev_kfree_skb_any(skb);
845
846 return NULL;
847}
848
Bjørn Morkff06ab12012-10-22 10:56:33 +0000849/* verify NTB header and return offset of first NDP, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000850int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
Alexey Orishko900d4952010-11-29 23:23:28 +0000851{
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000852 struct usb_cdc_ncm_nth16 *nth16;
Bjørn Morkff06ab12012-10-22 10:56:33 +0000853 int len;
854 int ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000855
Alexey Orishko900d4952010-11-29 23:23:28 +0000856 if (ctx == NULL)
857 goto error;
858
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000859 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
860 sizeof(struct usb_cdc_ncm_ndp16))) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000861 pr_debug("frame too short\n");
862 goto error;
863 }
864
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000865 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
Alexey Orishko900d4952010-11-29 23:23:28 +0000866
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000867 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000868 pr_debug("invalid NTH16 signature <%u>\n",
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000869 le32_to_cpu(nth16->dwSignature));
Alexey Orishko900d4952010-11-29 23:23:28 +0000870 goto error;
871 }
872
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000873 len = le16_to_cpu(nth16->wBlockLength);
874 if (len > ctx->rx_max) {
875 pr_debug("unsupported NTB block length %u/%u\n", len,
876 ctx->rx_max);
Alexey Orishko900d4952010-11-29 23:23:28 +0000877 goto error;
878 }
879
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000880 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
881 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
882 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
883 pr_debug("sequence number glitch prev=%d curr=%d\n",
884 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
885 }
886 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
887
Bjørn Morkff06ab12012-10-22 10:56:33 +0000888 ret = le16_to_cpu(nth16->wNdpIndex);
889error:
890 return ret;
891}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000892EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000893
894/* verify NDP header and return number of datagrams, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000895int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
Bjørn Morkff06ab12012-10-22 10:56:33 +0000896{
897 struct usb_cdc_ncm_ndp16 *ndp16;
898 int ret = -EINVAL;
899
Bjørn Mork75d67d32012-10-22 10:56:32 +0000900 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
901 pr_debug("invalid NDP offset <%u>\n", ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +0000902 goto error;
903 }
Bjørn Mork75d67d32012-10-22 10:56:32 +0000904 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +0000905
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000906 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000907 pr_debug("invalid DPT16 length <%u>\n",
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000908 le32_to_cpu(ndp16->dwSignature));
Bjørn Morkff06ab12012-10-22 10:56:33 +0000909 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +0000910 }
911
Bjørn Morkff06ab12012-10-22 10:56:33 +0000912 ret = ((le16_to_cpu(ndp16->wLength) -
Alexey Orishko900d4952010-11-29 23:23:28 +0000913 sizeof(struct usb_cdc_ncm_ndp16)) /
914 sizeof(struct usb_cdc_ncm_dpe16));
Bjørn Morkff06ab12012-10-22 10:56:33 +0000915 ret--; /* we process NDP entries except for the last one */
Alexey Orishko900d4952010-11-29 23:23:28 +0000916
Bjørn Morkff06ab12012-10-22 10:56:33 +0000917 if ((sizeof(struct usb_cdc_ncm_ndp16) + ret * (sizeof(struct usb_cdc_ncm_dpe16))) >
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000918 skb_in->len) {
Bjørn Morkff06ab12012-10-22 10:56:33 +0000919 pr_debug("Invalid nframes = %d\n", ret);
920 ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000921 }
922
Bjørn Morkff06ab12012-10-22 10:56:33 +0000923error:
924 return ret;
925}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000926EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000927
928static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
929{
930 struct sk_buff *skb;
931 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
932 int len;
933 int nframes;
934 int x;
935 int offset;
936 struct usb_cdc_ncm_ndp16 *ndp16;
937 struct usb_cdc_ncm_dpe16 *dpe16;
938 int ndpoffset;
939 int loopcount = 50; /* arbitrary max preventing infinite loop */
940
941 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
942 if (ndpoffset < 0)
943 goto error;
944
945next_ndp:
946 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
947 if (nframes < 0)
948 goto error;
949
950 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
951
952 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
953 pr_debug("invalid DPT16 signature <%u>\n",
954 le32_to_cpu(ndp16->dwSignature));
955 goto err_ndp;
956 }
957 dpe16 = ndp16->dpe16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000958
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000959 for (x = 0; x < nframes; x++, dpe16++) {
960 offset = le16_to_cpu(dpe16->wDatagramIndex);
961 len = le16_to_cpu(dpe16->wDatagramLength);
Alexey Orishko900d4952010-11-29 23:23:28 +0000962
963 /*
964 * CDC NCM ch. 3.7
965 * All entries after first NULL entry are to be ignored
966 */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000967 if ((offset == 0) || (len == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000968 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +0000969 goto err_ndp; /* empty NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +0000970 break;
971 }
972
973 /* sanity checking */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000974 if (((offset + len) > skb_in->len) ||
975 (len > ctx->rx_max) || (len < ETH_HLEN)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000976 pr_debug("invalid frame detected (ignored)"
Alexey Orishkof742aa82011-01-17 07:07:25 +0000977 "offset[%u]=%u, length=%u, skb=%p\n",
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000978 x, offset, len, skb_in);
Alexey Orishko900d4952010-11-29 23:23:28 +0000979 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +0000980 goto err_ndp;
Alexey Orishko900d4952010-11-29 23:23:28 +0000981 break;
982
983 } else {
984 skb = skb_clone(skb_in, GFP_ATOMIC);
Jesper Juhl9e567902011-01-13 11:40:11 +0000985 if (!skb)
986 goto error;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000987 skb->len = len;
Alexey Orishko900d4952010-11-29 23:23:28 +0000988 skb->data = ((u8 *)skb_in->data) + offset;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000989 skb_set_tail_pointer(skb, len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000990 usbnet_skb_return(dev, skb);
991 }
992 }
Bjørn Mork75d67d32012-10-22 10:56:32 +0000993err_ndp:
994 /* are there more NDPs to process? */
995 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
996 if (ndpoffset && loopcount--)
997 goto next_ndp;
998
Alexey Orishko900d4952010-11-29 23:23:28 +0000999 return 1;
1000error:
1001 return 0;
1002}
1003
1004static void
1005cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001006 struct usb_cdc_speed_change *data)
Alexey Orishko900d4952010-11-29 23:23:28 +00001007{
Alexey Orishko84e77a82011-02-07 09:45:10 +00001008 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1009 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
Alexey Orishko900d4952010-11-29 23:23:28 +00001010
1011 /*
1012 * Currently the USB-NET API does not support reporting the actual
1013 * device speed. Do print it instead.
1014 */
1015 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1016 ctx->tx_speed = tx_speed;
1017 ctx->rx_speed = rx_speed;
1018
1019 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1020 printk(KERN_INFO KBUILD_MODNAME
1021 ": %s: %u mbit/s downlink "
1022 "%u mbit/s uplink\n",
1023 ctx->netdev->name,
1024 (unsigned int)(rx_speed / 1000000U),
1025 (unsigned int)(tx_speed / 1000000U));
1026 } else {
1027 printk(KERN_INFO KBUILD_MODNAME
1028 ": %s: %u kbit/s downlink "
1029 "%u kbit/s uplink\n",
1030 ctx->netdev->name,
1031 (unsigned int)(rx_speed / 1000U),
1032 (unsigned int)(tx_speed / 1000U));
1033 }
1034 }
1035}
1036
1037static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1038{
1039 struct cdc_ncm_ctx *ctx;
1040 struct usb_cdc_notification *event;
1041
1042 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1043
1044 if (urb->actual_length < sizeof(*event))
1045 return;
1046
1047 /* test for split data in 8-byte chunks */
1048 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1049 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001050 (struct usb_cdc_speed_change *)urb->transfer_buffer);
Alexey Orishko900d4952010-11-29 23:23:28 +00001051 return;
1052 }
1053
1054 event = urb->transfer_buffer;
1055
1056 switch (event->bNotificationType) {
1057 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1058 /*
1059 * According to the CDC NCM specification ch.7.1
1060 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1061 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1062 */
1063 ctx->connected = event->wValue;
1064
1065 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1066 " %sconnected\n",
1067 ctx->netdev->name, ctx->connected ? "" : "dis");
1068
1069 if (ctx->connected)
1070 netif_carrier_on(dev->net);
1071 else {
1072 netif_carrier_off(dev->net);
1073 ctx->tx_speed = ctx->rx_speed = 0;
1074 }
1075 break;
1076
1077 case USB_CDC_NOTIFY_SPEED_CHANGE:
Alexey Orishko84e77a82011-02-07 09:45:10 +00001078 if (urb->actual_length < (sizeof(*event) +
1079 sizeof(struct usb_cdc_speed_change)))
Alexey Orishko900d4952010-11-29 23:23:28 +00001080 set_bit(EVENT_STS_SPLIT, &dev->flags);
1081 else
1082 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001083 (struct usb_cdc_speed_change *) &event[1]);
Alexey Orishko900d4952010-11-29 23:23:28 +00001084 break;
1085
1086 default:
1087 dev_err(&dev->udev->dev, "NCM: unexpected "
1088 "notification 0x%02x!\n", event->bNotificationType);
1089 break;
1090 }
1091}
1092
1093static int cdc_ncm_check_connect(struct usbnet *dev)
1094{
1095 struct cdc_ncm_ctx *ctx;
1096
1097 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1098 if (ctx == NULL)
1099 return 1; /* disconnected */
1100
1101 return !ctx->connected;
1102}
1103
1104static int
1105cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1106{
1107 return usbnet_probe(udev, prod);
1108}
1109
1110static void cdc_ncm_disconnect(struct usb_interface *intf)
1111{
1112 struct usbnet *dev = usb_get_intfdata(intf);
1113
1114 if (dev == NULL)
1115 return; /* already disconnected */
1116
1117 usbnet_disconnect(intf);
1118}
1119
1120static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1121{
1122 dev->intf->needs_remote_wakeup = status;
1123 return 0;
1124}
1125
1126static const struct driver_info cdc_ncm_info = {
1127 .description = "CDC NCM",
Arnd Bergmannc2613442011-04-01 20:12:02 -07001128 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
Alexey Orishko900d4952010-11-29 23:23:28 +00001129 .bind = cdc_ncm_bind,
1130 .unbind = cdc_ncm_unbind,
1131 .check_connect = cdc_ncm_check_connect,
1132 .manage_power = cdc_ncm_manage_power,
1133 .status = cdc_ncm_status,
1134 .rx_fixup = cdc_ncm_rx_fixup,
1135 .tx_fixup = cdc_ncm_tx_fixup,
1136};
1137
Dan Williamsf94898e2012-07-24 08:43:22 +00001138/* Same as cdc_ncm_info, but with FLAG_WWAN */
1139static const struct driver_info wwan_info = {
1140 .description = "Mobile Broadband Network Device",
1141 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1142 | FLAG_WWAN,
1143 .bind = cdc_ncm_bind,
1144 .unbind = cdc_ncm_unbind,
1145 .check_connect = cdc_ncm_check_connect,
1146 .manage_power = cdc_ncm_manage_power,
1147 .status = cdc_ncm_status,
1148 .rx_fixup = cdc_ncm_rx_fixup,
1149 .tx_fixup = cdc_ncm_tx_fixup,
1150};
1151
1152static const struct usb_device_id cdc_devs[] = {
1153 /* Ericsson MBM devices like F5521gw */
1154 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1155 | USB_DEVICE_ID_MATCH_VENDOR,
1156 .idVendor = 0x0bdb,
1157 .bInterfaceClass = USB_CLASS_COMM,
1158 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1159 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1160 .driver_info = (unsigned long) &wwan_info,
1161 },
1162
Peter Meiserf3a1ef92012-08-02 02:30:20 +00001163 /* Dell branded MBM devices like DW5550 */
1164 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1165 | USB_DEVICE_ID_MATCH_VENDOR,
1166 .idVendor = 0x413c,
1167 .bInterfaceClass = USB_CLASS_COMM,
1168 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1169 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1170 .driver_info = (unsigned long) &wwan_info,
1171 },
1172
1173 /* Toshiba branded MBM devices */
1174 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1175 | USB_DEVICE_ID_MATCH_VENDOR,
1176 .idVendor = 0x0930,
1177 .bInterfaceClass = USB_CLASS_COMM,
1178 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1179 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1180 .driver_info = (unsigned long) &wwan_info,
1181 },
1182
Dan Williamsf94898e2012-07-24 08:43:22 +00001183 /* Generic CDC-NCM devices */
1184 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1185 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1186 .driver_info = (unsigned long)&cdc_ncm_info,
1187 },
1188 {
1189 },
1190};
1191MODULE_DEVICE_TABLE(usb, cdc_devs);
1192
Alexey Orishko900d4952010-11-29 23:23:28 +00001193static struct usb_driver cdc_ncm_driver = {
1194 .name = "cdc_ncm",
1195 .id_table = cdc_devs,
1196 .probe = cdc_ncm_probe,
1197 .disconnect = cdc_ncm_disconnect,
1198 .suspend = usbnet_suspend,
1199 .resume = usbnet_resume,
Stefan Metzmacher85e3c652011-06-01 02:01:41 +00001200 .reset_resume = usbnet_resume,
Alexey Orishko900d4952010-11-29 23:23:28 +00001201 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001202 .disable_hub_initiated_lpm = 1,
Alexey Orishko900d4952010-11-29 23:23:28 +00001203};
1204
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001205module_usb_driver(cdc_ncm_driver);
Alexey Orishko900d4952010-11-29 23:23:28 +00001206
1207MODULE_AUTHOR("Hans Petter Selasky");
1208MODULE_DESCRIPTION("USB CDC NCM host driver");
1209MODULE_LICENSE("Dual BSD/GPL");