blob: 73faf05209b3c1f8aaeb44bff88eb74d379cc172 [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
Bjørn Mork1e8bbe62013-03-14 01:05:13 +000056#if IS_ENABLED(CONFIG_USB_NET_CDC_MBIM)
57static bool prefer_mbim = true;
58#else
59static bool prefer_mbim;
60#endif
61module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
62MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
63
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +000064static void cdc_ncm_txpath_bh(unsigned long param);
65static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
66static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +000067static struct usb_driver cdc_ncm_driver;
Alexey Orishko900d4952010-11-29 23:23:28 +000068
Bjørn Morkbed6f762013-11-01 11:16:42 +010069static u8 cdc_ncm_setup(struct usbnet *dev)
Alexey Orishko900d4952010-11-29 23:23:28 +000070{
Bjørn Morkbed6f762013-11-01 11:16:42 +010071 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Bjørn Mork6a9612e2013-11-01 11:16:45 +010072 struct usb_cdc_ncm_ntb_parameters ncm_parm;
Alexey Orishko900d4952010-11-29 23:23:28 +000073 u32 val;
Alexey Orishko900d4952010-11-29 23:23:28 +000074 u8 flags;
75 u8 iface_no;
76 int err;
Greg Suarez2d1c4312012-10-22 10:56:30 +000077 int eth_hlen;
Alexey Orishko84e77a82011-02-07 09:45:10 +000078 u16 ntb_fmt_supported;
Greg Suarez2d1c4312012-10-22 10:56:30 +000079 u32 min_dgram_size;
80 u32 min_hdr_size;
Alexey Orishko900d4952010-11-29 23:23:28 +000081
82 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
83
Ming Lei90b8b032012-10-24 19:46:56 +000084 err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
85 USB_TYPE_CLASS | USB_DIR_IN
86 |USB_RECIP_INTERFACE,
Bjørn Mork6a9612e2013-11-01 11:16:45 +010087 0, iface_no, &ncm_parm,
88 sizeof(ncm_parm));
Giuseppe Scrivano36c35412011-08-03 22:10:29 +000089 if (err < 0) {
Bjørn Morkae223cd2013-11-01 11:16:54 +010090 dev_dbg(&dev->intf->dev, "failed GET_NTB_PARAMETERS\n");
Alexey Orishko900d4952010-11-29 23:23:28 +000091 return 1;
92 }
93
94 /* read correct set of parameters according to device mode */
Bjørn Mork6a9612e2013-11-01 11:16:45 +010095 ctx->rx_max = le32_to_cpu(ncm_parm.dwNtbInMaxSize);
96 ctx->tx_max = le32_to_cpu(ncm_parm.dwNtbOutMaxSize);
97 ctx->tx_remainder = le16_to_cpu(ncm_parm.wNdpOutPayloadRemainder);
98 ctx->tx_modulus = le16_to_cpu(ncm_parm.wNdpOutDivisor);
99 ctx->tx_ndp_modulus = le16_to_cpu(ncm_parm.wNdpOutAlignment);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000100 /* devices prior to NCM Errata shall set this field to zero */
Bjørn Mork6a9612e2013-11-01 11:16:45 +0100101 ctx->tx_max_datagrams = le16_to_cpu(ncm_parm.wNtbOutMaxDatagrams);
102 ntb_fmt_supported = le16_to_cpu(ncm_parm.bmNtbFormatsSupported);
Alexey Orishko900d4952010-11-29 23:23:28 +0000103
Greg Suarez2d1c4312012-10-22 10:56:30 +0000104 eth_hlen = ETH_HLEN;
105 min_dgram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
106 min_hdr_size = CDC_NCM_MIN_HDR_SIZE;
107 if (ctx->mbim_desc != NULL) {
108 flags = ctx->mbim_desc->bmNetworkCapabilities;
109 eth_hlen = 0;
110 min_dgram_size = CDC_MBIM_MIN_DATAGRAM_SIZE;
111 min_hdr_size = 0;
112 } else if (ctx->func_desc != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000113 flags = ctx->func_desc->bmNetworkCapabilities;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000114 } else {
Alexey Orishko900d4952010-11-29 23:23:28 +0000115 flags = 0;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000116 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000117
Bjørn Morkae223cd2013-11-01 11:16:54 +0100118 dev_dbg(&dev->intf->dev,
119 "dwNtbInMaxSize=%u dwNtbOutMaxSize=%u wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
120 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
121 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
Alexey Orishko900d4952010-11-29 23:23:28 +0000122
Alexey Orishko84e77a82011-02-07 09:45:10 +0000123 /* max count of tx datagrams */
124 if ((ctx->tx_max_datagrams == 0) ||
125 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
126 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
Alexey Orishko900d4952010-11-29 23:23:28 +0000127
128 /* verify maximum size of received NTB in bytes */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000129 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100130 dev_dbg(&dev->intf->dev, "Using min receive length=%d\n",
131 USB_CDC_NCM_NTB_MIN_IN_SIZE);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000132 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
133 }
134
135 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100136 dev_dbg(&dev->intf->dev, "Using default maximum receive length=%d\n",
137 CDC_NCM_NTB_MAX_SIZE_RX);
Alexey Orishko900d4952010-11-29 23:23:28 +0000138 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
139 }
140
Alexey Orishko84e77a82011-02-07 09:45:10 +0000141 /* inform device about NTB input size changes */
Bjørn Mork6a9612e2013-11-01 11:16:45 +0100142 if (ctx->rx_max != le32_to_cpu(ncm_parm.dwNtbInMaxSize)) {
Ming Lei90b8b032012-10-24 19:46:56 +0000143 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000144
Ming Lei90b8b032012-10-24 19:46:56 +0000145 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
146 USB_TYPE_CLASS | USB_DIR_OUT
147 | USB_RECIP_INTERFACE,
148 0, iface_no, &dwNtbInMaxSize, 4);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000149 if (err < 0)
Bjørn Morkae223cd2013-11-01 11:16:54 +0100150 dev_dbg(&dev->intf->dev, "Setting NTB Input Size failed\n");
Alexey Orishko84e77a82011-02-07 09:45:10 +0000151 }
152
Alexey Orishko900d4952010-11-29 23:23:28 +0000153 /* verify maximum size of transmitted NTB in bytes */
154 if ((ctx->tx_max <
Greg Suarez2d1c4312012-10-22 10:56:30 +0000155 (min_hdr_size + min_dgram_size)) ||
Alexey Orishko900d4952010-11-29 23:23:28 +0000156 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100157 dev_dbg(&dev->intf->dev, "Using default maximum transmit length=%d\n",
158 CDC_NCM_NTB_MAX_SIZE_TX);
Alexey Orishko900d4952010-11-29 23:23:28 +0000159 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
Bjørn Mork6a9612e2013-11-01 11:16:45 +0100160
161 /* Adding a pad byte here simplifies the handling in
162 * cdc_ncm_fill_tx_frame, by making tx_max always
163 * represent the real skb max size.
164 */
165 if (ctx->tx_max % usb_maxpacket(dev->udev, dev->out, 1) == 0)
166 ctx->tx_max++;
167
Alexey Orishko900d4952010-11-29 23:23:28 +0000168 }
169
170 /*
171 * verify that the structure alignment is:
172 * - power of two
173 * - not greater than the maximum transmit length
174 * - not less than four bytes
175 */
176 val = ctx->tx_ndp_modulus;
177
178 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
179 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100180 dev_dbg(&dev->intf->dev, "Using default alignment: 4 bytes\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000181 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
182 }
183
184 /*
185 * verify that the payload alignment is:
186 * - power of two
187 * - not greater than the maximum transmit length
188 * - not less than four bytes
189 */
190 val = ctx->tx_modulus;
191
192 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
193 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100194 dev_dbg(&dev->intf->dev, "Using default transmit modulus: 4 bytes\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000195 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
196 }
197
198 /* verify the payload remainder */
199 if (ctx->tx_remainder >= ctx->tx_modulus) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100200 dev_dbg(&dev->intf->dev, "Using default transmit remainder: 0 bytes\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000201 ctx->tx_remainder = 0;
202 }
203
204 /* adjust TX-remainder according to NCM specification. */
Greg Suarez2d1c4312012-10-22 10:56:30 +0000205 ctx->tx_remainder = ((ctx->tx_remainder - eth_hlen) &
206 (ctx->tx_modulus - 1));
Alexey Orishko900d4952010-11-29 23:23:28 +0000207
208 /* additional configuration */
209
210 /* set CRC Mode */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000211 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
Ming Lei90b8b032012-10-24 19:46:56 +0000212 err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
213 USB_TYPE_CLASS | USB_DIR_OUT
214 | USB_RECIP_INTERFACE,
215 USB_CDC_NCM_CRC_NOT_APPENDED,
216 iface_no, NULL, 0);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000217 if (err < 0)
Bjørn Morkae223cd2013-11-01 11:16:54 +0100218 dev_dbg(&dev->intf->dev, "Setting CRC mode off failed\n");
Alexey Orishko84e77a82011-02-07 09:45:10 +0000219 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000220
Alexey Orishko84e77a82011-02-07 09:45:10 +0000221 /* set NTB format, if both formats are supported */
222 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
Ming Lei90b8b032012-10-24 19:46:56 +0000223 err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
224 USB_TYPE_CLASS | USB_DIR_OUT
225 | USB_RECIP_INTERFACE,
226 USB_CDC_NCM_NTB16_FORMAT,
227 iface_no, NULL, 0);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000228 if (err < 0)
Bjørn Morkae223cd2013-11-01 11:16:54 +0100229 dev_dbg(&dev->intf->dev, "Setting NTB format to 16-bit failed\n");
Alexey Orishko84e77a82011-02-07 09:45:10 +0000230 }
231
Greg Suarez2d1c4312012-10-22 10:56:30 +0000232 ctx->max_datagram_size = min_dgram_size;
Alexey Orishko900d4952010-11-29 23:23:28 +0000233
234 /* set Max Datagram Size (MTU) */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000235 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
Ming Lei90b8b032012-10-24 19:46:56 +0000236 __le16 max_datagram_size;
Greg Suarez2d1c4312012-10-22 10:56:30 +0000237 u16 eth_max_sz;
238 if (ctx->ether_desc != NULL)
239 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
240 else if (ctx->mbim_desc != NULL)
241 eth_max_sz = le16_to_cpu(ctx->mbim_desc->wMaxSegmentSize);
242 else
243 goto max_dgram_err;
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000244
Ming Lei90b8b032012-10-24 19:46:56 +0000245 err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
246 USB_TYPE_CLASS | USB_DIR_IN
247 | USB_RECIP_INTERFACE,
248 0, iface_no, &max_datagram_size, 2);
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000249 if (err < 0) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100250 dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
251 min_dgram_size);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000252 } else {
Josh Boyer75bc8ef2011-08-08 02:34:07 +0000253 ctx->max_datagram_size =
Ming Lei90b8b032012-10-24 19:46:56 +0000254 le16_to_cpu(max_datagram_size);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000255 /* Check Eth descriptor value */
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000256 if (ctx->max_datagram_size > eth_max_sz)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000257 ctx->max_datagram_size = eth_max_sz;
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000258
259 if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
Greg Suarez2d1c4312012-10-22 10:56:30 +0000260 ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000261
Greg Suarez2d1c4312012-10-22 10:56:30 +0000262 if (ctx->max_datagram_size < min_dgram_size)
263 ctx->max_datagram_size = min_dgram_size;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000264
265 /* if value changed, update device */
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000266 if (ctx->max_datagram_size !=
Ming Lei90b8b032012-10-24 19:46:56 +0000267 le16_to_cpu(max_datagram_size)) {
Bjørn Mork1b5287a2013-11-01 11:16:46 +0100268 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
Ming Lei90b8b032012-10-24 19:46:56 +0000269 err = usbnet_write_cmd(dev,
Giuseppe Scrivano36c35412011-08-03 22:10:29 +0000270 USB_CDC_SET_MAX_DATAGRAM_SIZE,
271 USB_TYPE_CLASS | USB_DIR_OUT
272 | USB_RECIP_INTERFACE,
273 0,
Ming Lei90b8b032012-10-24 19:46:56 +0000274 iface_no, &max_datagram_size,
275 2);
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000276 if (err < 0)
Bjørn Morkae223cd2013-11-01 11:16:54 +0100277 dev_dbg(&dev->intf->dev, "SET_MAX_DGRAM_SIZE failed\n");
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000278 }
Alexey Orishko84e77a82011-02-07 09:45:10 +0000279 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000280 }
281
Alexey Orishko3f658cd2012-03-14 11:26:11 +0000282max_dgram_err:
Bjørn Morkbed6f762013-11-01 11:16:42 +0100283 if (dev->net->mtu != (ctx->max_datagram_size - eth_hlen))
284 dev->net->mtu = ctx->max_datagram_size - eth_hlen;
Alexey Orishko900d4952010-11-29 23:23:28 +0000285
286 return 0;
287}
288
289static void
Bjørn Morkff1632a2013-11-01 11:16:41 +0100290cdc_ncm_find_endpoints(struct usbnet *dev, struct usb_interface *intf)
Alexey Orishko900d4952010-11-29 23:23:28 +0000291{
Bjørn Morkff1632a2013-11-01 11:16:41 +0100292 struct usb_host_endpoint *e, *in = NULL, *out = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000293 u8 ep;
294
295 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
296
297 e = intf->cur_altsetting->endpoint + ep;
298 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
299 case USB_ENDPOINT_XFER_INT:
300 if (usb_endpoint_dir_in(&e->desc)) {
Bjørn Morkff1632a2013-11-01 11:16:41 +0100301 if (!dev->status)
302 dev->status = e;
Alexey Orishko900d4952010-11-29 23:23:28 +0000303 }
304 break;
305
306 case USB_ENDPOINT_XFER_BULK:
307 if (usb_endpoint_dir_in(&e->desc)) {
Bjørn Morkff1632a2013-11-01 11:16:41 +0100308 if (!in)
309 in = e;
Alexey Orishko900d4952010-11-29 23:23:28 +0000310 } else {
Bjørn Morkff1632a2013-11-01 11:16:41 +0100311 if (!out)
312 out = e;
Alexey Orishko900d4952010-11-29 23:23:28 +0000313 }
314 break;
315
316 default:
317 break;
318 }
319 }
Bjørn Morkff1632a2013-11-01 11:16:41 +0100320 if (in && !dev->in)
321 dev->in = usb_rcvbulkpipe(dev->udev,
322 in->desc.bEndpointAddress &
323 USB_ENDPOINT_NUMBER_MASK);
324 if (out && !dev->out)
325 dev->out = usb_sndbulkpipe(dev->udev,
326 out->desc.bEndpointAddress &
327 USB_ENDPOINT_NUMBER_MASK);
Alexey Orishko900d4952010-11-29 23:23:28 +0000328}
329
330static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
331{
332 if (ctx == NULL)
333 return;
334
Alexey Orishko900d4952010-11-29 23:23:28 +0000335 if (ctx->tx_rem_skb != NULL) {
336 dev_kfree_skb_any(ctx->tx_rem_skb);
337 ctx->tx_rem_skb = NULL;
338 }
339
340 if (ctx->tx_curr_skb != NULL) {
341 dev_kfree_skb_any(ctx->tx_curr_skb);
342 ctx->tx_curr_skb = NULL;
343 }
344
345 kfree(ctx);
346}
347
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000348int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_altsetting)
Alexey Orishko900d4952010-11-29 23:23:28 +0000349{
Bjørn Mork83292232013-11-01 11:16:47 +0100350 const struct usb_cdc_union_desc *union_desc = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000351 struct cdc_ncm_ctx *ctx;
352 struct usb_driver *driver;
353 u8 *buf;
354 int len;
355 int temp;
356 u8 iface_no;
357
Thomas Meyerc7969842011-11-17 12:43:40 +0000358 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Devendra Naga8f0923c2013-03-29 23:03:22 +0000359 if (!ctx)
360 return -ENOMEM;
Alexey Orishko900d4952010-11-29 23:23:28 +0000361
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000362 hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
363 ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
Bjørn Morkbed6f762013-11-01 11:16:42 +0100364 ctx->bh.data = (unsigned long)dev;
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000365 ctx->bh.func = cdc_ncm_txpath_bh;
366 atomic_set(&ctx->stop, 0);
Alexey Orishko900d4952010-11-29 23:23:28 +0000367 spin_lock_init(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000368
369 /* store ctx pointer in device data field */
370 dev->data[0] = (unsigned long)ctx;
371
Bjørn Mork9fe02342013-11-01 11:16:48 +0100372 /* only the control interface can be successfully probed */
373 ctx->control = intf;
374
Alexey Orishko900d4952010-11-29 23:23:28 +0000375 /* get some pointers */
376 driver = driver_of(intf);
377 buf = intf->cur_altsetting->extra;
378 len = intf->cur_altsetting->extralen;
379
Alexey Orishko900d4952010-11-29 23:23:28 +0000380 /* parse through descriptors associated with control interface */
381 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
382
383 if (buf[1] != USB_DT_CS_INTERFACE)
384 goto advance;
385
386 switch (buf[2]) {
387 case USB_CDC_UNION_TYPE:
Bjørn Mork83292232013-11-01 11:16:47 +0100388 if (buf[0] < sizeof(*union_desc))
Alexey Orishko900d4952010-11-29 23:23:28 +0000389 break;
390
Bjørn Mork83292232013-11-01 11:16:47 +0100391 union_desc = (const struct usb_cdc_union_desc *)buf;
Bjørn Mork9fe02342013-11-01 11:16:48 +0100392 /* the master must be the interface we are probing */
393 if (intf->cur_altsetting->desc.bInterfaceNumber !=
394 union_desc->bMasterInterface0)
395 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +0000396 ctx->data = usb_ifnum_to_if(dev->udev,
Bjørn Mork83292232013-11-01 11:16:47 +0100397 union_desc->bSlaveInterface0);
Alexey Orishko900d4952010-11-29 23:23:28 +0000398 break;
399
400 case USB_CDC_ETHERNET_TYPE:
401 if (buf[0] < sizeof(*(ctx->ether_desc)))
402 break;
403
404 ctx->ether_desc =
405 (const struct usb_cdc_ether_desc *)buf;
Alexey Orishko900d4952010-11-29 23:23:28 +0000406 break;
407
408 case USB_CDC_NCM_TYPE:
409 if (buf[0] < sizeof(*(ctx->func_desc)))
410 break;
411
412 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
413 break;
414
Greg Suarez38396e42012-10-22 10:56:31 +0000415 case USB_CDC_MBIM_TYPE:
416 if (buf[0] < sizeof(*(ctx->mbim_desc)))
417 break;
418
419 ctx->mbim_desc = (const struct usb_cdc_mbim_desc *)buf;
420 break;
421
Alexey Orishko900d4952010-11-29 23:23:28 +0000422 default:
423 break;
424 }
425advance:
426 /* advance to next descriptor */
427 temp = buf[0];
428 buf += temp;
429 len -= temp;
430 }
431
Bjørn Mork9992c2e2013-01-21 05:50:38 +0000432 /* some buggy devices have an IAD but no CDC Union */
Bjørn Mork83292232013-11-01 11:16:47 +0100433 if (!union_desc && intf->intf_assoc && intf->intf_assoc->bInterfaceCount == 2) {
Bjørn Mork56a666d2013-01-25 23:36:59 +0000434 ctx->data = usb_ifnum_to_if(dev->udev, intf->cur_altsetting->desc.bInterfaceNumber + 1);
435 dev_dbg(&intf->dev, "CDC Union missing - got slave from IAD\n");
Bjørn Mork9992c2e2013-01-21 05:50:38 +0000436 }
437
Alexey Orishko900d4952010-11-29 23:23:28 +0000438 /* check if we got everything */
Bjørn Mork9fe02342013-11-01 11:16:48 +0100439 if (!ctx->data || (!ctx->mbim_desc && !ctx->ether_desc))
Alexey Orishko900d4952010-11-29 23:23:28 +0000440 goto error;
441
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000442 /* claim data interface, if different from control */
443 if (ctx->data != ctx->control) {
444 temp = usb_driver_claim_interface(driver, ctx->data, dev);
445 if (temp)
446 goto error;
447 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000448
449 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
450
451 /* reset data interface */
452 temp = usb_set_interface(dev->udev, iface_no, 0);
453 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000454 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000455
Alexey Orishko900d4952010-11-29 23:23:28 +0000456 /* configure data interface */
Greg Suarez38396e42012-10-22 10:56:31 +0000457 temp = usb_set_interface(dev->udev, iface_no, data_altsetting);
Alexey Orishko900d4952010-11-29 23:23:28 +0000458 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000459 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000460
Bjørn Morkff1632a2013-11-01 11:16:41 +0100461 cdc_ncm_find_endpoints(dev, ctx->data);
462 cdc_ncm_find_endpoints(dev, ctx->control);
463 if (!dev->in || !dev->out || !dev->status)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000464 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000465
Bjørn Mork6a9612e2013-11-01 11:16:45 +0100466 /* initialize data interface */
467 if (cdc_ncm_setup(dev))
468 goto error2;
469
Alexey Orishko900d4952010-11-29 23:23:28 +0000470 usb_set_intfdata(ctx->data, dev);
471 usb_set_intfdata(ctx->control, dev);
Alexey Orishko900d4952010-11-29 23:23:28 +0000472
Greg Suarez38396e42012-10-22 10:56:31 +0000473 if (ctx->ether_desc) {
474 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
475 if (temp)
476 goto error2;
477 dev_info(&dev->udev->dev, "MAC-Address: %pM\n", dev->net->dev_addr);
478 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000479
Bjørn Mork43c87f72013-11-01 11:16:52 +0100480 /* usbnet use these values for sizing tx/rx queues */
481 dev->hard_mtu = ctx->tx_max;
Alexey Orishko900d4952010-11-29 23:23:28 +0000482 dev->rx_urb_size = ctx->rx_max;
483
Alexey Orishko900d4952010-11-29 23:23:28 +0000484 return 0;
485
Alexey Orishko19694ac2011-05-24 05:26:13 +0000486error2:
487 usb_set_intfdata(ctx->control, NULL);
488 usb_set_intfdata(ctx->data, NULL);
Bjørn Mork6b4ef602013-01-21 05:50:40 +0000489 if (ctx->data != ctx->control)
490 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko900d4952010-11-29 23:23:28 +0000491error:
492 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
493 dev->data[0] = 0;
Alexey Orishko19694ac2011-05-24 05:26:13 +0000494 dev_info(&dev->udev->dev, "bind() failure\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000495 return -ENODEV;
496}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000497EXPORT_SYMBOL_GPL(cdc_ncm_bind_common);
Alexey Orishko900d4952010-11-29 23:23:28 +0000498
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000499void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
Alexey Orishko900d4952010-11-29 23:23:28 +0000500{
501 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko19694ac2011-05-24 05:26:13 +0000502 struct usb_driver *driver = driver_of(intf);
Alexey Orishko900d4952010-11-29 23:23:28 +0000503
504 if (ctx == NULL)
505 return; /* no setup */
506
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000507 atomic_set(&ctx->stop, 1);
508
509 if (hrtimer_active(&ctx->tx_timer))
510 hrtimer_cancel(&ctx->tx_timer);
511
512 tasklet_kill(&ctx->bh);
513
Bjørn Morkbbc8d922012-11-13 03:19:43 +0000514 /* handle devices with combined control and data interface */
515 if (ctx->control == ctx->data)
516 ctx->data = NULL;
517
Alexey Orishko19694ac2011-05-24 05:26:13 +0000518 /* disconnect master --> disconnect slave */
519 if (intf == ctx->control && ctx->data) {
520 usb_set_intfdata(ctx->data, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000521 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000522 ctx->data = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000523
Alexey Orishko19694ac2011-05-24 05:26:13 +0000524 } else if (intf == ctx->data && ctx->control) {
525 usb_set_intfdata(ctx->control, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000526 usb_driver_release_interface(driver, ctx->control);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000527 ctx->control = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000528 }
529
Bjørn Mork3e515662013-11-01 11:16:40 +0100530 usb_set_intfdata(intf, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000531 cdc_ncm_free(ctx);
532}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000533EXPORT_SYMBOL_GPL(cdc_ncm_unbind);
Alexey Orishko900d4952010-11-29 23:23:28 +0000534
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000535/* Select the MBIM altsetting iff it is preferred and available,
536 * returning the number of the corresponding data interface altsetting
537 */
538u8 cdc_ncm_select_altsetting(struct usbnet *dev, struct usb_interface *intf)
Greg Suarez38396e42012-10-22 10:56:31 +0000539{
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000540 struct usb_host_interface *alt;
Greg Suarez38396e42012-10-22 10:56:31 +0000541
Bjørn Morkbd329e12012-10-22 10:56:38 +0000542 /* The MBIM spec defines a NCM compatible default altsetting,
543 * which we may have matched:
544 *
545 * "Functions that implement both NCM 1.0 and MBIM (an
546 * “NCM/MBIM function”) according to this recommendation
547 * shall provide two alternate settings for the
548 * Communication Interface. Alternate setting 0, and the
549 * associated class and endpoint descriptors, shall be
550 * constructed according to the rules given for the
551 * Communication Interface in section 5 of [USBNCM10].
552 * Alternate setting 1, and the associated class and
553 * endpoint descriptors, shall be constructed according to
554 * the rules given in section 6 (USB Device Model) of this
555 * specification."
Bjørn Morkbd329e12012-10-22 10:56:38 +0000556 */
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000557 if (prefer_mbim && intf->num_altsetting == 2) {
558 alt = usb_altnum_to_altsetting(intf, CDC_NCM_COMM_ALTSETTING_MBIM);
559 if (alt && cdc_ncm_comm_intf_is_mbim(alt) &&
560 !usb_set_interface(dev->udev,
561 intf->cur_altsetting->desc.bInterfaceNumber,
562 CDC_NCM_COMM_ALTSETTING_MBIM))
563 return CDC_NCM_DATA_ALTSETTING_MBIM;
Bjørn Morkf350ca02013-02-13 12:09:52 +0000564 }
Bjørn Mork1e8bbe62013-03-14 01:05:13 +0000565 return CDC_NCM_DATA_ALTSETTING_NCM;
566}
567EXPORT_SYMBOL_GPL(cdc_ncm_select_altsetting);
568
569static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
570{
571 int ret;
572
573 /* MBIM backwards compatible function? */
574 cdc_ncm_select_altsetting(dev, intf);
575 if (cdc_ncm_comm_intf_is_mbim(intf->cur_altsetting))
576 return -ENODEV;
Bjørn Morkbd329e12012-10-22 10:56:38 +0000577
Greg Suarez38396e42012-10-22 10:56:31 +0000578 /* NCM data altsetting is always 1 */
579 ret = cdc_ncm_bind_common(dev, intf, 1);
580
581 /*
582 * We should get an event when network connection is "connected" or
583 * "disconnected". Set network connection in "disconnected" state
584 * (carrier is OFF) during attach, so the IP network stack does not
585 * start IPv6 negotiation and more.
586 */
Ming Lei8a34b0a2013-04-11 04:40:33 +0000587 usbnet_link_change(dev, 0, 0);
Greg Suarez38396e42012-10-22 10:56:31 +0000588 return ret;
589}
590
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000591static 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 +0000592{
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000593 size_t align = ALIGN(skb->len, modulus) - skb->len + remainder;
594
595 if (skb->len + align > max)
596 align = max - skb->len;
597 if (align && skb_tailroom(skb) >= align)
598 memset(skb_put(skb, align), 0, align);
599}
600
601/* return a pointer to a valid struct usb_cdc_ncm_ndp16 of type sign, possibly
602 * allocating a new one within skb
603 */
604static struct usb_cdc_ncm_ndp16 *cdc_ncm_ndp(struct cdc_ncm_ctx *ctx, struct sk_buff *skb, __le32 sign, size_t reserve)
605{
606 struct usb_cdc_ncm_ndp16 *ndp16 = NULL;
607 struct usb_cdc_ncm_nth16 *nth16 = (void *)skb->data;
608 size_t ndpoffset = le16_to_cpu(nth16->wNdpIndex);
609
610 /* follow the chain of NDPs, looking for a match */
611 while (ndpoffset) {
612 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb->data + ndpoffset);
613 if (ndp16->dwSignature == sign)
614 return ndp16;
615 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
616 }
617
618 /* align new NDP */
619 cdc_ncm_align_tail(skb, ctx->tx_ndp_modulus, 0, ctx->tx_max);
620
621 /* verify that there is room for the NDP and the datagram (reserve) */
622 if ((ctx->tx_max - skb->len - reserve) < CDC_NCM_NDP_SIZE)
623 return NULL;
624
625 /* link to it */
626 if (ndp16)
627 ndp16->wNextNdpIndex = cpu_to_le16(skb->len);
628 else
629 nth16->wNdpIndex = cpu_to_le16(skb->len);
630
631 /* push a new empty NDP */
632 ndp16 = (struct usb_cdc_ncm_ndp16 *)memset(skb_put(skb, CDC_NCM_NDP_SIZE), 0, CDC_NCM_NDP_SIZE);
633 ndp16->dwSignature = sign;
634 ndp16->wLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_ndp16) + sizeof(struct usb_cdc_ncm_dpe16));
635 return ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000636}
637
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000638struct sk_buff *
Bjørn Morkbed6f762013-11-01 11:16:42 +0100639cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
Alexey Orishko900d4952010-11-29 23:23:28 +0000640{
Bjørn Morkbed6f762013-11-01 11:16:42 +0100641 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000642 struct usb_cdc_ncm_nth16 *nth16;
643 struct usb_cdc_ncm_ndp16 *ndp16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000644 struct sk_buff *skb_out;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000645 u16 n = 0, index, ndplen;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000646 u8 ready2send = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000647
648 /* if there is a remaining skb, it gets priority */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000649 if (skb != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000650 swap(skb, ctx->tx_rem_skb);
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000651 swap(sign, ctx->tx_rem_sign);
652 } else {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000653 ready2send = 1;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000654 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000655
656 /* check if we are resuming an OUT skb */
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000657 skb_out = ctx->tx_curr_skb;
Alexey Orishko900d4952010-11-29 23:23:28 +0000658
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000659 /* allocate a new OUT skb */
660 if (!skb_out) {
Bjørn Mork205722262013-11-01 11:16:38 +0100661 skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
Alexey Orishko900d4952010-11-29 23:23:28 +0000662 if (skb_out == NULL) {
663 if (skb != NULL) {
664 dev_kfree_skb_any(skb);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100665 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +0000666 }
667 goto exit_no_skb;
668 }
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000669 /* fill out the initial 16-bit NTB header */
670 nth16 = (struct usb_cdc_ncm_nth16 *)memset(skb_put(skb_out, sizeof(struct usb_cdc_ncm_nth16)), 0, sizeof(struct usb_cdc_ncm_nth16));
671 nth16->dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
672 nth16->wHeaderLength = cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16));
673 nth16->wSequence = cpu_to_le16(ctx->tx_seq++);
Alexey Orishko900d4952010-11-29 23:23:28 +0000674
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000675 /* count total number of frames in this NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +0000676 ctx->tx_curr_frame_num = 0;
677 }
678
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000679 for (n = ctx->tx_curr_frame_num; n < ctx->tx_max_datagrams; n++) {
680 /* send any remaining skb first */
Alexey Orishko900d4952010-11-29 23:23:28 +0000681 if (skb == NULL) {
682 skb = ctx->tx_rem_skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000683 sign = ctx->tx_rem_sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000684 ctx->tx_rem_skb = NULL;
685
686 /* check for end of skb */
687 if (skb == NULL)
688 break;
689 }
690
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000691 /* get the appropriate NDP for this skb */
692 ndp16 = cdc_ncm_ndp(ctx, skb_out, sign, skb->len + ctx->tx_modulus + ctx->tx_remainder);
693
694 /* align beginning of next frame */
695 cdc_ncm_align_tail(skb_out, ctx->tx_modulus, ctx->tx_remainder, ctx->tx_max);
696
697 /* check if we had enough room left for both NDP and frame */
698 if (!ndp16 || skb_out->len + skb->len > ctx->tx_max) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000699 if (n == 0) {
700 /* won't fit, MTU problem? */
701 dev_kfree_skb_any(skb);
702 skb = NULL;
Bjørn Morkbed6f762013-11-01 11:16:42 +0100703 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +0000704 } else {
705 /* no room for skb - store for later */
706 if (ctx->tx_rem_skb != NULL) {
707 dev_kfree_skb_any(ctx->tx_rem_skb);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100708 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +0000709 }
710 ctx->tx_rem_skb = skb;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000711 ctx->tx_rem_sign = sign;
Alexey Orishko900d4952010-11-29 23:23:28 +0000712 skb = NULL;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000713 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000714 }
715 break;
716 }
717
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000718 /* calculate frame number withing this NDP */
719 ndplen = le16_to_cpu(ndp16->wLength);
720 index = (ndplen - sizeof(struct usb_cdc_ncm_ndp16)) / sizeof(struct usb_cdc_ncm_dpe16) - 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000721
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000722 /* OK, add this skb */
723 ndp16->dpe16[index].wDatagramLength = cpu_to_le16(skb->len);
724 ndp16->dpe16[index].wDatagramIndex = cpu_to_le16(skb_out->len);
725 ndp16->wLength = cpu_to_le16(ndplen + sizeof(struct usb_cdc_ncm_dpe16));
726 memcpy(skb_put(skb_out, skb->len), skb->data, skb->len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000727 dev_kfree_skb_any(skb);
728 skb = NULL;
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000729
730 /* send now if this NDP is full */
731 if (index >= CDC_NCM_DPT_DATAGRAMS_MAX) {
732 ready2send = 1;
733 break;
734 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000735 }
736
737 /* free up any dangling skb */
738 if (skb != NULL) {
739 dev_kfree_skb_any(skb);
740 skb = NULL;
Bjørn Morkbed6f762013-11-01 11:16:42 +0100741 dev->net->stats.tx_dropped++;
Alexey Orishko900d4952010-11-29 23:23:28 +0000742 }
743
744 ctx->tx_curr_frame_num = n;
745
746 if (n == 0) {
747 /* wait for more frames */
748 /* push variables */
749 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +0000750 goto exit_no_skb;
751
Alexey Orishko84e77a82011-02-07 09:45:10 +0000752 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000753 /* wait for more frames */
754 /* push variables */
755 ctx->tx_curr_skb = skb_out;
Alexey Orishko900d4952010-11-29 23:23:28 +0000756 /* set the pending count */
757 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000758 ctx->tx_timer_pending = CDC_NCM_TIMER_PENDING_CNT;
Alexey Orishko900d4952010-11-29 23:23:28 +0000759 goto exit_no_skb;
760
761 } else {
762 /* frame goes out */
763 /* variables will be reset at next call */
764 }
765
Bjørn Mork205722262013-11-01 11:16:38 +0100766 /* If collected data size is less or equal CDC_NCM_MIN_TX_PKT
767 * bytes, we send buffers as it is. If we get more data, it
768 * would be more efficient for USB HS mobile device with DMA
769 * engine to receive a full size NTB, than canceling DMA
770 * transfer and receiving a short packet.
Bjørn Mork4d619f62013-11-01 11:16:49 +0100771 *
772 * This optimization support is pointless if we end up sending
773 * a ZLP after full sized NTBs.
Alexey Orishko900d4952010-11-29 23:23:28 +0000774 */
Bjørn Mork4d619f62013-11-01 11:16:49 +0100775 if (!(dev->driver_info->flags & FLAG_SEND_ZLP) &&
776 skb_out->len > CDC_NCM_MIN_TX_PKT)
Bjørn Mork205722262013-11-01 11:16:38 +0100777 memset(skb_put(skb_out, ctx->tx_max - skb_out->len), 0,
778 ctx->tx_max - skb_out->len);
779 else if ((skb_out->len % dev->maxpacket) == 0)
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000780 *skb_put(skb_out, 1) = 0; /* force short packet */
Alexey Orishko900d4952010-11-29 23:23:28 +0000781
Bjørn Morkc78b7c52012-10-22 10:56:34 +0000782 /* set final frame length */
783 nth16 = (struct usb_cdc_ncm_nth16 *)skb_out->data;
784 nth16->wBlockLength = cpu_to_le16(skb_out->len);
Alexey Orishko900d4952010-11-29 23:23:28 +0000785
786 /* return skb */
787 ctx->tx_curr_skb = NULL;
Bjørn Morkbed6f762013-11-01 11:16:42 +0100788 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
Alexey Orishko900d4952010-11-29 23:23:28 +0000789 return skb_out;
790
791exit_no_skb:
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000792 /* Start timer, if there is a remaining skb */
793 if (ctx->tx_curr_skb != NULL)
794 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000795 return NULL;
796}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000797EXPORT_SYMBOL_GPL(cdc_ncm_fill_tx_frame);
Alexey Orishko900d4952010-11-29 23:23:28 +0000798
799static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
800{
801 /* start timer, if not already started */
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000802 if (!(hrtimer_active(&ctx->tx_timer) || atomic_read(&ctx->stop)))
803 hrtimer_start(&ctx->tx_timer,
804 ktime_set(0, CDC_NCM_TIMER_INTERVAL),
805 HRTIMER_MODE_REL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000806}
807
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000808static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
Alexey Orishko900d4952010-11-29 23:23:28 +0000809{
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000810 struct cdc_ncm_ctx *ctx =
811 container_of(timer, struct cdc_ncm_ctx, tx_timer);
Alexey Orishko900d4952010-11-29 23:23:28 +0000812
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000813 if (!atomic_read(&ctx->stop))
814 tasklet_schedule(&ctx->bh);
815 return HRTIMER_NORESTART;
816}
817
818static void cdc_ncm_txpath_bh(unsigned long param)
819{
Bjørn Morkbed6f762013-11-01 11:16:42 +0100820 struct usbnet *dev = (struct usbnet *)param;
821 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000822
823 spin_lock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000824 if (ctx->tx_timer_pending != 0) {
825 ctx->tx_timer_pending--;
Alexey Orishko900d4952010-11-29 23:23:28 +0000826 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000827 spin_unlock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100828 } else if (dev->net != NULL) {
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000829 spin_unlock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100830 netif_tx_lock_bh(dev->net);
831 usbnet_start_xmit(NULL, dev->net);
832 netif_tx_unlock_bh(dev->net);
Bjørn Mork7b1e0cb2012-10-25 21:44:09 +0000833 } else {
834 spin_unlock_bh(&ctx->mtx);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000835 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000836}
837
838static struct sk_buff *
839cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
840{
841 struct sk_buff *skb_out;
842 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko900d4952010-11-29 23:23:28 +0000843
844 /*
845 * The Ethernet API we are using does not support transmitting
846 * multiple Ethernet frames in a single call. This driver will
847 * accumulate multiple Ethernet frames and send out a larger
848 * USB frame when the USB buffer is full or when a single jiffies
849 * timeout happens.
850 */
851 if (ctx == NULL)
852 goto error;
853
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000854 spin_lock_bh(&ctx->mtx);
Bjørn Morkbed6f762013-11-01 11:16:42 +0100855 skb_out = cdc_ncm_fill_tx_frame(dev, skb, cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN));
Alexey Orishkoc84ff1d2012-03-14 11:26:10 +0000856 spin_unlock_bh(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000857 return skb_out;
858
859error:
860 if (skb != NULL)
861 dev_kfree_skb_any(skb);
862
863 return NULL;
864}
865
Bjørn Morkff06ab12012-10-22 10:56:33 +0000866/* verify NTB header and return offset of first NDP, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000867int cdc_ncm_rx_verify_nth16(struct cdc_ncm_ctx *ctx, struct sk_buff *skb_in)
Alexey Orishko900d4952010-11-29 23:23:28 +0000868{
Bjørn Morkae223cd2013-11-01 11:16:54 +0100869 struct usbnet *dev = netdev_priv(skb_in->dev);
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000870 struct usb_cdc_ncm_nth16 *nth16;
Bjørn Morkff06ab12012-10-22 10:56:33 +0000871 int len;
872 int ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000873
Alexey Orishko900d4952010-11-29 23:23:28 +0000874 if (ctx == NULL)
875 goto error;
876
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000877 if (skb_in->len < (sizeof(struct usb_cdc_ncm_nth16) +
878 sizeof(struct usb_cdc_ncm_ndp16))) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100879 netif_dbg(dev, rx_err, dev->net, "frame too short\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000880 goto error;
881 }
882
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000883 nth16 = (struct usb_cdc_ncm_nth16 *)skb_in->data;
Alexey Orishko900d4952010-11-29 23:23:28 +0000884
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000885 if (le32_to_cpu(nth16->dwSignature) != USB_CDC_NCM_NTH16_SIGN) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000886 pr_debug("invalid NTH16 signature <%u>\n",
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000887 le32_to_cpu(nth16->dwSignature));
Alexey Orishko900d4952010-11-29 23:23:28 +0000888 goto error;
889 }
890
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000891 len = le16_to_cpu(nth16->wBlockLength);
892 if (len > ctx->rx_max) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100893 netif_dbg(dev, rx_err, dev->net,
894 "unsupported NTB block length %u/%u\n", len,
895 ctx->rx_max);
Alexey Orishko900d4952010-11-29 23:23:28 +0000896 goto error;
897 }
898
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000899 if ((ctx->rx_seq + 1) != le16_to_cpu(nth16->wSequence) &&
Bjørn Morkae223cd2013-11-01 11:16:54 +0100900 (ctx->rx_seq || le16_to_cpu(nth16->wSequence)) &&
901 !((ctx->rx_seq == 0xffff) && !le16_to_cpu(nth16->wSequence))) {
902 netif_dbg(dev, rx_err, dev->net,
903 "sequence number glitch prev=%d curr=%d\n",
904 ctx->rx_seq, le16_to_cpu(nth16->wSequence));
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000905 }
906 ctx->rx_seq = le16_to_cpu(nth16->wSequence);
907
Bjørn Morkff06ab12012-10-22 10:56:33 +0000908 ret = le16_to_cpu(nth16->wNdpIndex);
909error:
910 return ret;
911}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000912EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_nth16);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000913
914/* verify NDP header and return number of datagrams, or negative error */
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000915int cdc_ncm_rx_verify_ndp16(struct sk_buff *skb_in, int ndpoffset)
Bjørn Morkff06ab12012-10-22 10:56:33 +0000916{
Bjørn Morkae223cd2013-11-01 11:16:54 +0100917 struct usbnet *dev = netdev_priv(skb_in->dev);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000918 struct usb_cdc_ncm_ndp16 *ndp16;
919 int ret = -EINVAL;
920
Bjørn Mork75d67d32012-10-22 10:56:32 +0000921 if ((ndpoffset + sizeof(struct usb_cdc_ncm_ndp16)) > skb_in->len) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100922 netif_dbg(dev, rx_err, dev->net, "invalid NDP offset <%u>\n",
923 ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +0000924 goto error;
925 }
Bjørn Mork75d67d32012-10-22 10:56:32 +0000926 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
Alexey Orishko900d4952010-11-29 23:23:28 +0000927
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000928 if (le16_to_cpu(ndp16->wLength) < USB_CDC_NCM_NDP16_LENGTH_MIN) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100929 netif_dbg(dev, rx_err, dev->net, "invalid DPT16 length <%u>\n",
930 le16_to_cpu(ndp16->wLength));
Bjørn Morkff06ab12012-10-22 10:56:33 +0000931 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +0000932 }
933
Bjørn Morkff06ab12012-10-22 10:56:33 +0000934 ret = ((le16_to_cpu(ndp16->wLength) -
Alexey Orishko900d4952010-11-29 23:23:28 +0000935 sizeof(struct usb_cdc_ncm_ndp16)) /
936 sizeof(struct usb_cdc_ncm_dpe16));
Bjørn Morkff06ab12012-10-22 10:56:33 +0000937 ret--; /* we process NDP entries except for the last one */
Alexey Orishko900d4952010-11-29 23:23:28 +0000938
Bjørn Morkae223cd2013-11-01 11:16:54 +0100939 if ((sizeof(struct usb_cdc_ncm_ndp16) +
940 ret * (sizeof(struct usb_cdc_ncm_dpe16))) > skb_in->len) {
941 netif_dbg(dev, rx_err, dev->net, "Invalid nframes = %d\n", ret);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000942 ret = -EINVAL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000943 }
944
Bjørn Morkff06ab12012-10-22 10:56:33 +0000945error:
946 return ret;
947}
Bjørn Morkc91ce3b2012-10-22 10:56:35 +0000948EXPORT_SYMBOL_GPL(cdc_ncm_rx_verify_ndp16);
Bjørn Morkff06ab12012-10-22 10:56:33 +0000949
950static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
951{
952 struct sk_buff *skb;
953 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
954 int len;
955 int nframes;
956 int x;
957 int offset;
958 struct usb_cdc_ncm_ndp16 *ndp16;
959 struct usb_cdc_ncm_dpe16 *dpe16;
960 int ndpoffset;
961 int loopcount = 50; /* arbitrary max preventing infinite loop */
962
963 ndpoffset = cdc_ncm_rx_verify_nth16(ctx, skb_in);
964 if (ndpoffset < 0)
965 goto error;
966
967next_ndp:
968 nframes = cdc_ncm_rx_verify_ndp16(skb_in, ndpoffset);
969 if (nframes < 0)
970 goto error;
971
972 ndp16 = (struct usb_cdc_ncm_ndp16 *)(skb_in->data + ndpoffset);
973
974 if (le32_to_cpu(ndp16->dwSignature) != USB_CDC_NCM_NDP16_NOCRC_SIGN) {
975 pr_debug("invalid DPT16 signature <%u>\n",
976 le32_to_cpu(ndp16->dwSignature));
977 goto err_ndp;
978 }
979 dpe16 = ndp16->dpe16;
Alexey Orishko900d4952010-11-29 23:23:28 +0000980
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000981 for (x = 0; x < nframes; x++, dpe16++) {
982 offset = le16_to_cpu(dpe16->wDatagramIndex);
983 len = le16_to_cpu(dpe16->wDatagramLength);
Alexey Orishko900d4952010-11-29 23:23:28 +0000984
985 /*
986 * CDC NCM ch. 3.7
987 * All entries after first NULL entry are to be ignored
988 */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000989 if ((offset == 0) || (len == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000990 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +0000991 goto err_ndp; /* empty NTB */
Alexey Orishko900d4952010-11-29 23:23:28 +0000992 break;
993 }
994
995 /* sanity checking */
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +0000996 if (((offset + len) > skb_in->len) ||
997 (len > ctx->rx_max) || (len < ETH_HLEN)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +0100998 netif_dbg(dev, rx_err, dev->net,
999 "invalid frame detected (ignored) offset[%u]=%u, length=%u, skb=%p\n",
1000 x, offset, len, skb_in);
Alexey Orishko900d4952010-11-29 23:23:28 +00001001 if (!x)
Bjørn Mork75d67d32012-10-22 10:56:32 +00001002 goto err_ndp;
Alexey Orishko900d4952010-11-29 23:23:28 +00001003 break;
1004
1005 } else {
1006 skb = skb_clone(skb_in, GFP_ATOMIC);
Jesper Juhl9e567902011-01-13 11:40:11 +00001007 if (!skb)
1008 goto error;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001009 skb->len = len;
Alexey Orishko900d4952010-11-29 23:23:28 +00001010 skb->data = ((u8 *)skb_in->data) + offset;
Alexey Orishkod5ddb4a2012-03-14 11:26:12 +00001011 skb_set_tail_pointer(skb, len);
Alexey Orishko900d4952010-11-29 23:23:28 +00001012 usbnet_skb_return(dev, skb);
1013 }
1014 }
Bjørn Mork75d67d32012-10-22 10:56:32 +00001015err_ndp:
1016 /* are there more NDPs to process? */
1017 ndpoffset = le16_to_cpu(ndp16->wNextNdpIndex);
1018 if (ndpoffset && loopcount--)
1019 goto next_ndp;
1020
Alexey Orishko900d4952010-11-29 23:23:28 +00001021 return 1;
1022error:
1023 return 0;
1024}
1025
1026static void
Bjørn Morkbed6f762013-11-01 11:16:42 +01001027cdc_ncm_speed_change(struct usbnet *dev,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001028 struct usb_cdc_speed_change *data)
Alexey Orishko900d4952010-11-29 23:23:28 +00001029{
Alexey Orishko84e77a82011-02-07 09:45:10 +00001030 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1031 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
Alexey Orishko900d4952010-11-29 23:23:28 +00001032
1033 /*
1034 * Currently the USB-NET API does not support reporting the actual
1035 * device speed. Do print it instead.
1036 */
Bjørn Morkf3028c52013-11-01 11:16:44 +01001037 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001038 netif_info(dev, link, dev->net,
1039 "%u mbit/s downlink %u mbit/s uplink\n",
Bjørn Morkf3028c52013-11-01 11:16:44 +01001040 (unsigned int)(rx_speed / 1000000U),
1041 (unsigned int)(tx_speed / 1000000U));
1042 } else {
Bjørn Morkae223cd2013-11-01 11:16:54 +01001043 netif_info(dev, link, dev->net,
1044 "%u kbit/s downlink %u kbit/s uplink\n",
Bjørn Morkf3028c52013-11-01 11:16:44 +01001045 (unsigned int)(rx_speed / 1000U),
1046 (unsigned int)(tx_speed / 1000U));
Alexey Orishko900d4952010-11-29 23:23:28 +00001047 }
1048}
1049
1050static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1051{
1052 struct cdc_ncm_ctx *ctx;
1053 struct usb_cdc_notification *event;
1054
1055 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1056
1057 if (urb->actual_length < sizeof(*event))
1058 return;
1059
1060 /* test for split data in 8-byte chunks */
1061 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
Bjørn Morkbed6f762013-11-01 11:16:42 +01001062 cdc_ncm_speed_change(dev,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001063 (struct usb_cdc_speed_change *)urb->transfer_buffer);
Alexey Orishko900d4952010-11-29 23:23:28 +00001064 return;
1065 }
1066
1067 event = urb->transfer_buffer;
1068
1069 switch (event->bNotificationType) {
1070 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1071 /*
1072 * According to the CDC NCM specification ch.7.1
1073 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1074 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1075 */
Bjørn Mork1a7c6cc2012-10-25 21:44:08 +00001076 ctx->connected = le16_to_cpu(event->wValue);
Bjørn Morkae223cd2013-11-01 11:16:54 +01001077 netif_info(dev, link, dev->net,
1078 "network connection: %sconnected\n",
1079 ctx->connected ? "" : "dis");
Ming Lei8a34b0a2013-04-11 04:40:33 +00001080 usbnet_link_change(dev, ctx->connected, 0);
Alexey Orishko900d4952010-11-29 23:23:28 +00001081 break;
1082
1083 case USB_CDC_NOTIFY_SPEED_CHANGE:
Alexey Orishko84e77a82011-02-07 09:45:10 +00001084 if (urb->actual_length < (sizeof(*event) +
1085 sizeof(struct usb_cdc_speed_change)))
Alexey Orishko900d4952010-11-29 23:23:28 +00001086 set_bit(EVENT_STS_SPLIT, &dev->flags);
1087 else
Bjørn Morkbed6f762013-11-01 11:16:42 +01001088 cdc_ncm_speed_change(dev,
1089 (struct usb_cdc_speed_change *)&event[1]);
Alexey Orishko900d4952010-11-29 23:23:28 +00001090 break;
1091
1092 default:
Bjørn Mork67a36602013-04-08 08:26:23 +00001093 dev_dbg(&dev->udev->dev,
1094 "NCM: unexpected notification 0x%02x!\n",
1095 event->bNotificationType);
Alexey Orishko900d4952010-11-29 23:23:28 +00001096 break;
1097 }
1098}
1099
1100static int cdc_ncm_check_connect(struct usbnet *dev)
1101{
1102 struct cdc_ncm_ctx *ctx;
1103
1104 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1105 if (ctx == NULL)
1106 return 1; /* disconnected */
1107
1108 return !ctx->connected;
1109}
1110
Alexey Orishko900d4952010-11-29 23:23:28 +00001111static const struct driver_info cdc_ncm_info = {
1112 .description = "CDC NCM",
Arnd Bergmannc2613442011-04-01 20:12:02 -07001113 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
Alexey Orishko900d4952010-11-29 23:23:28 +00001114 .bind = cdc_ncm_bind,
1115 .unbind = cdc_ncm_unbind,
1116 .check_connect = cdc_ncm_check_connect,
Oliver Neukuma5e40702012-12-18 04:46:12 +00001117 .manage_power = usbnet_manage_power,
Alexey Orishko900d4952010-11-29 23:23:28 +00001118 .status = cdc_ncm_status,
1119 .rx_fixup = cdc_ncm_rx_fixup,
1120 .tx_fixup = cdc_ncm_tx_fixup,
1121};
1122
Dan Williamsf94898e2012-07-24 08:43:22 +00001123/* Same as cdc_ncm_info, but with FLAG_WWAN */
1124static const struct driver_info wwan_info = {
1125 .description = "Mobile Broadband Network Device",
1126 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1127 | FLAG_WWAN,
1128 .bind = cdc_ncm_bind,
1129 .unbind = cdc_ncm_unbind,
1130 .check_connect = cdc_ncm_check_connect,
Oliver Neukuma5e40702012-12-18 04:46:12 +00001131 .manage_power = usbnet_manage_power,
Dan Williamsf94898e2012-07-24 08:43:22 +00001132 .status = cdc_ncm_status,
1133 .rx_fixup = cdc_ncm_rx_fixup,
1134 .tx_fixup = cdc_ncm_tx_fixup,
1135};
1136
Wei Shuai2f62d5a2013-01-21 06:00:32 +00001137/* Same as wwan_info, but with FLAG_NOARP */
1138static const struct driver_info wwan_noarp_info = {
1139 .description = "Mobile Broadband Network Device (NO ARP)",
1140 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET
1141 | FLAG_WWAN | FLAG_NOARP,
1142 .bind = cdc_ncm_bind,
1143 .unbind = cdc_ncm_unbind,
1144 .check_connect = cdc_ncm_check_connect,
1145 .manage_power = usbnet_manage_power,
1146 .status = cdc_ncm_status,
1147 .rx_fixup = cdc_ncm_rx_fixup,
1148 .tx_fixup = cdc_ncm_tx_fixup,
1149};
1150
Dan Williamsf94898e2012-07-24 08:43:22 +00001151static const struct usb_device_id cdc_devs[] = {
1152 /* Ericsson MBM devices like F5521gw */
1153 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1154 | USB_DEVICE_ID_MATCH_VENDOR,
1155 .idVendor = 0x0bdb,
1156 .bInterfaceClass = USB_CLASS_COMM,
1157 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1158 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1159 .driver_info = (unsigned long) &wwan_info,
1160 },
1161
Peter Meiserf3a1ef9c2012-08-02 02:30:20 +00001162 /* Dell branded MBM devices like DW5550 */
1163 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1164 | USB_DEVICE_ID_MATCH_VENDOR,
1165 .idVendor = 0x413c,
1166 .bInterfaceClass = USB_CLASS_COMM,
1167 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1168 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1169 .driver_info = (unsigned long) &wwan_info,
1170 },
1171
1172 /* Toshiba branded MBM devices */
1173 { .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
1174 | USB_DEVICE_ID_MATCH_VENDOR,
1175 .idVendor = 0x0930,
1176 .bInterfaceClass = USB_CLASS_COMM,
1177 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
1178 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1179 .driver_info = (unsigned long) &wwan_info,
1180 },
1181
Bjørn Mork1f84eab2013-02-27 03:08:58 +00001182 /* tag Huawei devices as wwan */
1183 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
1184 USB_CLASS_COMM,
1185 USB_CDC_SUBCLASS_NCM,
1186 USB_CDC_PROTO_NONE),
1187 .driver_info = (unsigned long)&wwan_info,
1188 },
1189
Bjørn Morkbbc8d922012-11-13 03:19:43 +00001190 /* Huawei NCM devices disguised as vendor specific */
1191 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
1192 .driver_info = (unsigned long)&wwan_info,
1193 },
1194 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
1195 .driver_info = (unsigned long)&wwan_info,
1196 },
Bjørn Mork96316c52013-02-06 05:21:53 +00001197 { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x76),
1198 .driver_info = (unsigned long)&wwan_info,
1199 },
Bjørn Morkbbc8d922012-11-13 03:19:43 +00001200
Wei Shuai2f62d5a2013-01-21 06:00:32 +00001201 /* Infineon(now Intel) HSPA Modem platform */
1202 { USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
1203 USB_CLASS_COMM,
1204 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1205 .driver_info = (unsigned long)&wwan_noarp_info,
1206 },
1207
Dan Williamsf94898e2012-07-24 08:43:22 +00001208 /* Generic CDC-NCM devices */
1209 { USB_INTERFACE_INFO(USB_CLASS_COMM,
1210 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
1211 .driver_info = (unsigned long)&cdc_ncm_info,
1212 },
1213 {
1214 },
1215};
1216MODULE_DEVICE_TABLE(usb, cdc_devs);
1217
Alexey Orishko900d4952010-11-29 23:23:28 +00001218static struct usb_driver cdc_ncm_driver = {
1219 .name = "cdc_ncm",
1220 .id_table = cdc_devs,
Bjørn Mork085e50e2013-11-01 11:16:50 +01001221 .probe = usbnet_probe,
1222 .disconnect = usbnet_disconnect,
Alexey Orishko900d4952010-11-29 23:23:28 +00001223 .suspend = usbnet_suspend,
1224 .resume = usbnet_resume,
Stefan Metzmacher85e3c652011-06-01 02:01:41 +00001225 .reset_resume = usbnet_resume,
Alexey Orishko900d4952010-11-29 23:23:28 +00001226 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001227 .disable_hub_initiated_lpm = 1,
Alexey Orishko900d4952010-11-29 23:23:28 +00001228};
1229
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001230module_usb_driver(cdc_ncm_driver);
Alexey Orishko900d4952010-11-29 23:23:28 +00001231
1232MODULE_AUTHOR("Hans Petter Selasky");
1233MODULE_DESCRIPTION("USB CDC NCM host driver");
1234MODULE_LICENSE("Dual BSD/GPL");