xref: /openbmc/linux/drivers/net/usb/cdc_ncm.c (revision 900d495a)
1900d495aSAlexey Orishko /*
2900d495aSAlexey Orishko  * cdc_ncm.c
3900d495aSAlexey Orishko  *
4900d495aSAlexey Orishko  * Copyright (C) ST-Ericsson 2010
5900d495aSAlexey Orishko  * Contact: Alexey Orishko <alexey.orishko@stericsson.com>
6900d495aSAlexey Orishko  * Original author: Hans Petter Selasky <hans.petter.selasky@stericsson.com>
7900d495aSAlexey Orishko  *
8900d495aSAlexey Orishko  * USB Host Driver for Network Control Model (NCM)
9900d495aSAlexey Orishko  * http://www.usb.org/developers/devclass_docs/NCM10.zip
10900d495aSAlexey Orishko  *
11900d495aSAlexey Orishko  * The NCM encoding, decoding and initialization logic
12900d495aSAlexey Orishko  * derives from FreeBSD 8.x. if_cdce.c and if_cdcereg.h
13900d495aSAlexey Orishko  *
14900d495aSAlexey Orishko  * This software is available to you under a choice of one of two
15900d495aSAlexey Orishko  * licenses. You may choose this file to be licensed under the terms
16900d495aSAlexey Orishko  * of the GNU General Public License (GPL) Version 2 or the 2-clause
17900d495aSAlexey Orishko  * BSD license listed below:
18900d495aSAlexey Orishko  *
19900d495aSAlexey Orishko  * Redistribution and use in source and binary forms, with or without
20900d495aSAlexey Orishko  * modification, are permitted provided that the following conditions
21900d495aSAlexey Orishko  * are met:
22900d495aSAlexey Orishko  * 1. Redistributions of source code must retain the above copyright
23900d495aSAlexey Orishko  *    notice, this list of conditions and the following disclaimer.
24900d495aSAlexey Orishko  * 2. Redistributions in binary form must reproduce the above copyright
25900d495aSAlexey Orishko  *    notice, this list of conditions and the following disclaimer in the
26900d495aSAlexey Orishko  *    documentation and/or other materials provided with the distribution.
27900d495aSAlexey Orishko  *
28900d495aSAlexey Orishko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29900d495aSAlexey Orishko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30900d495aSAlexey Orishko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31900d495aSAlexey Orishko  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32900d495aSAlexey Orishko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33900d495aSAlexey Orishko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34900d495aSAlexey Orishko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35900d495aSAlexey Orishko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36900d495aSAlexey Orishko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37900d495aSAlexey Orishko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38900d495aSAlexey Orishko  * SUCH DAMAGE.
39900d495aSAlexey Orishko  */
40900d495aSAlexey Orishko 
41900d495aSAlexey Orishko #include <linux/module.h>
42900d495aSAlexey Orishko #include <linux/init.h>
43900d495aSAlexey Orishko #include <linux/netdevice.h>
44900d495aSAlexey Orishko #include <linux/ctype.h>
45900d495aSAlexey Orishko #include <linux/ethtool.h>
46900d495aSAlexey Orishko #include <linux/workqueue.h>
47900d495aSAlexey Orishko #include <linux/mii.h>
48900d495aSAlexey Orishko #include <linux/crc32.h>
49900d495aSAlexey Orishko #include <linux/usb.h>
50900d495aSAlexey Orishko #include <linux/version.h>
51900d495aSAlexey Orishko #include <linux/timer.h>
52900d495aSAlexey Orishko #include <linux/spinlock.h>
53900d495aSAlexey Orishko #include <linux/atomic.h>
54900d495aSAlexey Orishko #include <linux/usb/usbnet.h>
55900d495aSAlexey Orishko #include <linux/usb/cdc.h>
56900d495aSAlexey Orishko 
57900d495aSAlexey Orishko #define	DRIVER_VERSION				"30-Nov-2010"
58900d495aSAlexey Orishko 
59900d495aSAlexey Orishko /* CDC NCM subclass 3.2.1 */
60900d495aSAlexey Orishko #define USB_CDC_NCM_NDP16_LENGTH_MIN		0x10
61900d495aSAlexey Orishko 
62900d495aSAlexey Orishko /* Maximum NTB length */
63900d495aSAlexey Orishko #define	CDC_NCM_NTB_MAX_SIZE_TX			16384	/* bytes */
64900d495aSAlexey Orishko #define	CDC_NCM_NTB_MAX_SIZE_RX			16384	/* bytes */
65900d495aSAlexey Orishko 
66900d495aSAlexey Orishko /* Minimum value for MaxDatagramSize, ch. 6.2.9 */
67900d495aSAlexey Orishko #define	CDC_NCM_MIN_DATAGRAM_SIZE		1514	/* bytes */
68900d495aSAlexey Orishko 
69900d495aSAlexey Orishko #define	CDC_NCM_MIN_TX_PKT			512	/* bytes */
70900d495aSAlexey Orishko 
71900d495aSAlexey Orishko /* Default value for MaxDatagramSize */
72900d495aSAlexey Orishko #define	CDC_NCM_MAX_DATAGRAM_SIZE		2048	/* bytes */
73900d495aSAlexey Orishko 
74900d495aSAlexey Orishko /*
75900d495aSAlexey Orishko  * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
76900d495aSAlexey Orishko  * the last NULL entry. Any additional datagrams in NTB would be discarded.
77900d495aSAlexey Orishko  */
78900d495aSAlexey Orishko #define	CDC_NCM_DPT_DATAGRAMS_MAX		32
79900d495aSAlexey Orishko 
80900d495aSAlexey Orishko /* Restart the timer, if amount of datagrams is less than given value */
81900d495aSAlexey Orishko #define	CDC_NCM_RESTART_TIMER_DATAGRAM_CNT	3
82900d495aSAlexey Orishko 
83900d495aSAlexey Orishko /* The following macro defines the minimum header space */
84900d495aSAlexey Orishko #define	CDC_NCM_MIN_HDR_SIZE \
85900d495aSAlexey Orishko 	(sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \
86900d495aSAlexey Orishko 	(CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
87900d495aSAlexey Orishko 
88900d495aSAlexey Orishko struct connection_speed_change {
89900d495aSAlexey Orishko 	__le32	USBitRate; /* holds 3GPP downlink value, bits per second */
90900d495aSAlexey Orishko 	__le32	DSBitRate; /* holds 3GPP uplink value, bits per second */
91900d495aSAlexey Orishko } __attribute__ ((packed));
92900d495aSAlexey Orishko 
93900d495aSAlexey Orishko struct cdc_ncm_data {
94900d495aSAlexey Orishko 	struct usb_cdc_ncm_nth16 nth16;
95900d495aSAlexey Orishko 	struct usb_cdc_ncm_ndp16 ndp16;
96900d495aSAlexey Orishko 	struct usb_cdc_ncm_dpe16 dpe16[CDC_NCM_DPT_DATAGRAMS_MAX + 1];
97900d495aSAlexey Orishko };
98900d495aSAlexey Orishko 
99900d495aSAlexey Orishko struct cdc_ncm_ctx {
100900d495aSAlexey Orishko 	struct cdc_ncm_data rx_ncm;
101900d495aSAlexey Orishko 	struct cdc_ncm_data tx_ncm;
102900d495aSAlexey Orishko 	struct usb_cdc_ncm_ntb_parameters ncm_parm;
103900d495aSAlexey Orishko 	struct timer_list tx_timer;
104900d495aSAlexey Orishko 
105900d495aSAlexey Orishko 	const struct usb_cdc_ncm_desc *func_desc;
106900d495aSAlexey Orishko 	const struct usb_cdc_header_desc *header_desc;
107900d495aSAlexey Orishko 	const struct usb_cdc_union_desc *union_desc;
108900d495aSAlexey Orishko 	const struct usb_cdc_ether_desc *ether_desc;
109900d495aSAlexey Orishko 
110900d495aSAlexey Orishko 	struct net_device *netdev;
111900d495aSAlexey Orishko 	struct usb_device *udev;
112900d495aSAlexey Orishko 	struct usb_host_endpoint *in_ep;
113900d495aSAlexey Orishko 	struct usb_host_endpoint *out_ep;
114900d495aSAlexey Orishko 	struct usb_host_endpoint *status_ep;
115900d495aSAlexey Orishko 	struct usb_interface *intf;
116900d495aSAlexey Orishko 	struct usb_interface *control;
117900d495aSAlexey Orishko 	struct usb_interface *data;
118900d495aSAlexey Orishko 
119900d495aSAlexey Orishko 	struct sk_buff *tx_curr_skb;
120900d495aSAlexey Orishko 	struct sk_buff *tx_rem_skb;
121900d495aSAlexey Orishko 
122900d495aSAlexey Orishko 	spinlock_t mtx;
123900d495aSAlexey Orishko 
124900d495aSAlexey Orishko 	u32 tx_timer_pending;
125900d495aSAlexey Orishko 	u32 tx_curr_offset;
126900d495aSAlexey Orishko 	u32 tx_curr_last_offset;
127900d495aSAlexey Orishko 	u32 tx_curr_frame_num;
128900d495aSAlexey Orishko 	u32 rx_speed;
129900d495aSAlexey Orishko 	u32 tx_speed;
130900d495aSAlexey Orishko 	u32 rx_max;
131900d495aSAlexey Orishko 	u32 tx_max;
132900d495aSAlexey Orishko 	u32 max_datagram_size;
133900d495aSAlexey Orishko 	u16 tx_max_datagrams;
134900d495aSAlexey Orishko 	u16 tx_remainder;
135900d495aSAlexey Orishko 	u16 tx_modulus;
136900d495aSAlexey Orishko 	u16 tx_ndp_modulus;
137900d495aSAlexey Orishko 	u16 tx_seq;
138900d495aSAlexey Orishko 	u16 connected;
139900d495aSAlexey Orishko 	u8 data_claimed;
140900d495aSAlexey Orishko 	u8 control_claimed;
141900d495aSAlexey Orishko };
142900d495aSAlexey Orishko 
143900d495aSAlexey Orishko static void cdc_ncm_tx_timeout(unsigned long arg);
144900d495aSAlexey Orishko static const struct driver_info cdc_ncm_info;
145900d495aSAlexey Orishko static struct usb_driver cdc_ncm_driver;
146900d495aSAlexey Orishko static struct ethtool_ops cdc_ncm_ethtool_ops;
147900d495aSAlexey Orishko 
148900d495aSAlexey Orishko static const struct usb_device_id cdc_devs[] = {
149900d495aSAlexey Orishko 	{ USB_INTERFACE_INFO(USB_CLASS_COMM,
150900d495aSAlexey Orishko 		USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
151900d495aSAlexey Orishko 		.driver_info = (unsigned long)&cdc_ncm_info,
152900d495aSAlexey Orishko 	},
153900d495aSAlexey Orishko 	{
154900d495aSAlexey Orishko 	},
155900d495aSAlexey Orishko };
156900d495aSAlexey Orishko 
157900d495aSAlexey Orishko MODULE_DEVICE_TABLE(usb, cdc_devs);
158900d495aSAlexey Orishko 
159900d495aSAlexey Orishko static void
160900d495aSAlexey Orishko cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
161900d495aSAlexey Orishko {
162900d495aSAlexey Orishko 	struct usbnet *dev = netdev_priv(net);
163900d495aSAlexey Orishko 
164900d495aSAlexey Orishko 	strncpy(info->driver, dev->driver_name, sizeof(info->driver));
165900d495aSAlexey Orishko 	strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
166900d495aSAlexey Orishko 	strncpy(info->fw_version, dev->driver_info->description,
167900d495aSAlexey Orishko 		sizeof(info->fw_version));
168900d495aSAlexey Orishko 	usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
169900d495aSAlexey Orishko }
170900d495aSAlexey Orishko 
171900d495aSAlexey Orishko static int
172900d495aSAlexey Orishko cdc_ncm_do_request(struct cdc_ncm_ctx *ctx, struct usb_cdc_notification *req,
173900d495aSAlexey Orishko 		   void *data, u16 flags, u16 *actlen, u16 timeout)
174900d495aSAlexey Orishko {
175900d495aSAlexey Orishko 	int err;
176900d495aSAlexey Orishko 
177900d495aSAlexey Orishko 	err = usb_control_msg(ctx->udev, (req->bmRequestType & USB_DIR_IN) ?
178900d495aSAlexey Orishko 				usb_rcvctrlpipe(ctx->udev, 0) :
179900d495aSAlexey Orishko 				usb_sndctrlpipe(ctx->udev, 0),
180900d495aSAlexey Orishko 				req->bNotificationType, req->bmRequestType,
181900d495aSAlexey Orishko 				req->wValue,
182900d495aSAlexey Orishko 				req->wIndex, data,
183900d495aSAlexey Orishko 				req->wLength, timeout);
184900d495aSAlexey Orishko 
185900d495aSAlexey Orishko 	if (err < 0) {
186900d495aSAlexey Orishko 		if (actlen)
187900d495aSAlexey Orishko 			*actlen = 0;
188900d495aSAlexey Orishko 		return err;
189900d495aSAlexey Orishko 	}
190900d495aSAlexey Orishko 
191900d495aSAlexey Orishko 	if (actlen)
192900d495aSAlexey Orishko 		*actlen = err;
193900d495aSAlexey Orishko 
194900d495aSAlexey Orishko 	return 0;
195900d495aSAlexey Orishko }
196900d495aSAlexey Orishko 
197900d495aSAlexey Orishko static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
198900d495aSAlexey Orishko {
199900d495aSAlexey Orishko 	struct usb_cdc_notification req;
200900d495aSAlexey Orishko 	u32 val;
201900d495aSAlexey Orishko 	__le16 max_datagram_size;
202900d495aSAlexey Orishko 	u8 flags;
203900d495aSAlexey Orishko 	u8 iface_no;
204900d495aSAlexey Orishko 	int err;
205900d495aSAlexey Orishko 
206900d495aSAlexey Orishko 	iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
207900d495aSAlexey Orishko 
208900d495aSAlexey Orishko 	req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE;
209900d495aSAlexey Orishko 	req.bNotificationType = USB_CDC_GET_NTB_PARAMETERS;
210900d495aSAlexey Orishko 	req.wValue = 0;
211900d495aSAlexey Orishko 	req.wIndex = cpu_to_le16(iface_no);
212900d495aSAlexey Orishko 	req.wLength = cpu_to_le16(sizeof(ctx->ncm_parm));
213900d495aSAlexey Orishko 
214900d495aSAlexey Orishko 	err = cdc_ncm_do_request(ctx, &req, &ctx->ncm_parm, 0, NULL, 1000);
215900d495aSAlexey Orishko 	if (err) {
216900d495aSAlexey Orishko 		pr_debug("failed GET_NTB_PARAMETERS\n");
217900d495aSAlexey Orishko 		return 1;
218900d495aSAlexey Orishko 	}
219900d495aSAlexey Orishko 
220900d495aSAlexey Orishko 	/* read correct set of parameters according to device mode */
221900d495aSAlexey Orishko 	ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
222900d495aSAlexey Orishko 	ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
223900d495aSAlexey Orishko 	ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
224900d495aSAlexey Orishko 	ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
225900d495aSAlexey Orishko 	ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
226900d495aSAlexey Orishko 
227900d495aSAlexey Orishko 	if (ctx->func_desc != NULL)
228900d495aSAlexey Orishko 		flags = ctx->func_desc->bmNetworkCapabilities;
229900d495aSAlexey Orishko 	else
230900d495aSAlexey Orishko 		flags = 0;
231900d495aSAlexey Orishko 
232900d495aSAlexey Orishko 	pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
233900d495aSAlexey Orishko 		 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
234900d495aSAlexey Orishko 		 "wNdpOutAlignment=%u flags=0x%x\n",
235900d495aSAlexey Orishko 		 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
236900d495aSAlexey Orishko 		 ctx->tx_ndp_modulus, flags);
237900d495aSAlexey Orishko 
238900d495aSAlexey Orishko 	/* max count of tx datagrams without terminating NULL entry */
239900d495aSAlexey Orishko 	ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
240900d495aSAlexey Orishko 
241900d495aSAlexey Orishko 	/* verify maximum size of received NTB in bytes */
242900d495aSAlexey Orishko 	if ((ctx->rx_max <
243900d495aSAlexey Orishko 	    (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) ||
244900d495aSAlexey Orishko 	    (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX)) {
245900d495aSAlexey Orishko 		pr_debug("Using default maximum receive length=%d\n",
246900d495aSAlexey Orishko 						CDC_NCM_NTB_MAX_SIZE_RX);
247900d495aSAlexey Orishko 		ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
248900d495aSAlexey Orishko 	}
249900d495aSAlexey Orishko 
250900d495aSAlexey Orishko 	/* verify maximum size of transmitted NTB in bytes */
251900d495aSAlexey Orishko 	if ((ctx->tx_max <
252900d495aSAlexey Orishko 	    (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) ||
253900d495aSAlexey Orishko 	    (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
254900d495aSAlexey Orishko 		pr_debug("Using default maximum transmit length=%d\n",
255900d495aSAlexey Orishko 						CDC_NCM_NTB_MAX_SIZE_TX);
256900d495aSAlexey Orishko 		ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
257900d495aSAlexey Orishko 	}
258900d495aSAlexey Orishko 
259900d495aSAlexey Orishko 	/*
260900d495aSAlexey Orishko 	 * verify that the structure alignment is:
261900d495aSAlexey Orishko 	 * - power of two
262900d495aSAlexey Orishko 	 * - not greater than the maximum transmit length
263900d495aSAlexey Orishko 	 * - not less than four bytes
264900d495aSAlexey Orishko 	 */
265900d495aSAlexey Orishko 	val = ctx->tx_ndp_modulus;
266900d495aSAlexey Orishko 
267900d495aSAlexey Orishko 	if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
268900d495aSAlexey Orishko 	    (val != ((-val) & val)) || (val >= ctx->tx_max)) {
269900d495aSAlexey Orishko 		pr_debug("Using default alignment: 4 bytes\n");
270900d495aSAlexey Orishko 		ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
271900d495aSAlexey Orishko 	}
272900d495aSAlexey Orishko 
273900d495aSAlexey Orishko 	/*
274900d495aSAlexey Orishko 	 * verify that the payload alignment is:
275900d495aSAlexey Orishko 	 * - power of two
276900d495aSAlexey Orishko 	 * - not greater than the maximum transmit length
277900d495aSAlexey Orishko 	 * - not less than four bytes
278900d495aSAlexey Orishko 	 */
279900d495aSAlexey Orishko 	val = ctx->tx_modulus;
280900d495aSAlexey Orishko 
281900d495aSAlexey Orishko 	if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
282900d495aSAlexey Orishko 	    (val != ((-val) & val)) || (val >= ctx->tx_max)) {
283900d495aSAlexey Orishko 		pr_debug("Using default transmit modulus: 4 bytes\n");
284900d495aSAlexey Orishko 		ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
285900d495aSAlexey Orishko 	}
286900d495aSAlexey Orishko 
287900d495aSAlexey Orishko 	/* verify the payload remainder */
288900d495aSAlexey Orishko 	if (ctx->tx_remainder >= ctx->tx_modulus) {
289900d495aSAlexey Orishko 		pr_debug("Using default transmit remainder: 0 bytes\n");
290900d495aSAlexey Orishko 		ctx->tx_remainder = 0;
291900d495aSAlexey Orishko 	}
292900d495aSAlexey Orishko 
293900d495aSAlexey Orishko 	/* adjust TX-remainder according to NCM specification. */
294900d495aSAlexey Orishko 	ctx->tx_remainder = ((ctx->tx_remainder - ETH_HLEN) &
295900d495aSAlexey Orishko 						(ctx->tx_modulus - 1));
296900d495aSAlexey Orishko 
297900d495aSAlexey Orishko 	/* additional configuration */
298900d495aSAlexey Orishko 
299900d495aSAlexey Orishko 	/* set CRC Mode */
300900d495aSAlexey Orishko 	req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE;
301900d495aSAlexey Orishko 	req.bNotificationType = USB_CDC_SET_CRC_MODE;
302900d495aSAlexey Orishko 	req.wValue = cpu_to_le16(USB_CDC_NCM_CRC_NOT_APPENDED);
303900d495aSAlexey Orishko 	req.wIndex = cpu_to_le16(iface_no);
304900d495aSAlexey Orishko 	req.wLength = 0;
305900d495aSAlexey Orishko 
306900d495aSAlexey Orishko 	err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
307900d495aSAlexey Orishko 	if (err)
308900d495aSAlexey Orishko 		pr_debug("Setting CRC mode off failed\n");
309900d495aSAlexey Orishko 
310900d495aSAlexey Orishko 	/* set NTB format */
311900d495aSAlexey Orishko 	req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE;
312900d495aSAlexey Orishko 	req.bNotificationType = USB_CDC_SET_NTB_FORMAT;
313900d495aSAlexey Orishko 	req.wValue = cpu_to_le16(USB_CDC_NCM_NTB16_FORMAT);
314900d495aSAlexey Orishko 	req.wIndex = cpu_to_le16(iface_no);
315900d495aSAlexey Orishko 	req.wLength = 0;
316900d495aSAlexey Orishko 
317900d495aSAlexey Orishko 	err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
318900d495aSAlexey Orishko 	if (err)
319900d495aSAlexey Orishko 		pr_debug("Setting NTB format to 16-bit failed\n");
320900d495aSAlexey Orishko 
321900d495aSAlexey Orishko 	/* set Max Datagram Size (MTU) */
322900d495aSAlexey Orishko 	req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE;
323900d495aSAlexey Orishko 	req.bNotificationType = USB_CDC_GET_MAX_DATAGRAM_SIZE;
324900d495aSAlexey Orishko 	req.wValue = 0;
325900d495aSAlexey Orishko 	req.wIndex = cpu_to_le16(iface_no);
326900d495aSAlexey Orishko 	req.wLength = cpu_to_le16(2);
327900d495aSAlexey Orishko 
328900d495aSAlexey Orishko 	err = cdc_ncm_do_request(ctx, &req, &max_datagram_size, 0, NULL, 1000);
329900d495aSAlexey Orishko 	if (err) {
330900d495aSAlexey Orishko 		pr_debug(" GET_MAX_DATAGRAM_SIZE failed, using size=%u\n",
331900d495aSAlexey Orishko 			 CDC_NCM_MIN_DATAGRAM_SIZE);
332900d495aSAlexey Orishko 		/* use default */
333900d495aSAlexey Orishko 		ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
334900d495aSAlexey Orishko 	} else {
335900d495aSAlexey Orishko 		ctx->max_datagram_size = le16_to_cpu(max_datagram_size);
336900d495aSAlexey Orishko 
337900d495aSAlexey Orishko 		if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
338900d495aSAlexey Orishko 			ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
339900d495aSAlexey Orishko 		else if (ctx->max_datagram_size > CDC_NCM_MAX_DATAGRAM_SIZE)
340900d495aSAlexey Orishko 			ctx->max_datagram_size = CDC_NCM_MAX_DATAGRAM_SIZE;
341900d495aSAlexey Orishko 	}
342900d495aSAlexey Orishko 
343900d495aSAlexey Orishko 	if (ctx->netdev->mtu != (ctx->max_datagram_size - ETH_HLEN))
344900d495aSAlexey Orishko 		ctx->netdev->mtu = ctx->max_datagram_size - ETH_HLEN;
345900d495aSAlexey Orishko 
346900d495aSAlexey Orishko 	return 0;
347900d495aSAlexey Orishko }
348900d495aSAlexey Orishko 
349900d495aSAlexey Orishko static void
350900d495aSAlexey Orishko cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
351900d495aSAlexey Orishko {
352900d495aSAlexey Orishko 	struct usb_host_endpoint *e;
353900d495aSAlexey Orishko 	u8 ep;
354900d495aSAlexey Orishko 
355900d495aSAlexey Orishko 	for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
356900d495aSAlexey Orishko 
357900d495aSAlexey Orishko 		e = intf->cur_altsetting->endpoint + ep;
358900d495aSAlexey Orishko 		switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
359900d495aSAlexey Orishko 		case USB_ENDPOINT_XFER_INT:
360900d495aSAlexey Orishko 			if (usb_endpoint_dir_in(&e->desc)) {
361900d495aSAlexey Orishko 				if (ctx->status_ep == NULL)
362900d495aSAlexey Orishko 					ctx->status_ep = e;
363900d495aSAlexey Orishko 			}
364900d495aSAlexey Orishko 			break;
365900d495aSAlexey Orishko 
366900d495aSAlexey Orishko 		case USB_ENDPOINT_XFER_BULK:
367900d495aSAlexey Orishko 			if (usb_endpoint_dir_in(&e->desc)) {
368900d495aSAlexey Orishko 				if (ctx->in_ep == NULL)
369900d495aSAlexey Orishko 					ctx->in_ep = e;
370900d495aSAlexey Orishko 			} else {
371900d495aSAlexey Orishko 				if (ctx->out_ep == NULL)
372900d495aSAlexey Orishko 					ctx->out_ep = e;
373900d495aSAlexey Orishko 			}
374900d495aSAlexey Orishko 			break;
375900d495aSAlexey Orishko 
376900d495aSAlexey Orishko 		default:
377900d495aSAlexey Orishko 			break;
378900d495aSAlexey Orishko 		}
379900d495aSAlexey Orishko 	}
380900d495aSAlexey Orishko }
381900d495aSAlexey Orishko 
382900d495aSAlexey Orishko static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
383900d495aSAlexey Orishko {
384900d495aSAlexey Orishko 	if (ctx == NULL)
385900d495aSAlexey Orishko 		return;
386900d495aSAlexey Orishko 
387900d495aSAlexey Orishko 	del_timer_sync(&ctx->tx_timer);
388900d495aSAlexey Orishko 
389900d495aSAlexey Orishko 	if (ctx->data_claimed) {
390900d495aSAlexey Orishko 		usb_set_intfdata(ctx->data, NULL);
391900d495aSAlexey Orishko 		usb_driver_release_interface(driver_of(ctx->intf), ctx->data);
392900d495aSAlexey Orishko 	}
393900d495aSAlexey Orishko 
394900d495aSAlexey Orishko 	if (ctx->control_claimed) {
395900d495aSAlexey Orishko 		usb_set_intfdata(ctx->control, NULL);
396900d495aSAlexey Orishko 		usb_driver_release_interface(driver_of(ctx->intf),
397900d495aSAlexey Orishko 								ctx->control);
398900d495aSAlexey Orishko 	}
399900d495aSAlexey Orishko 
400900d495aSAlexey Orishko 	if (ctx->tx_rem_skb != NULL) {
401900d495aSAlexey Orishko 		dev_kfree_skb_any(ctx->tx_rem_skb);
402900d495aSAlexey Orishko 		ctx->tx_rem_skb = NULL;
403900d495aSAlexey Orishko 	}
404900d495aSAlexey Orishko 
405900d495aSAlexey Orishko 	if (ctx->tx_curr_skb != NULL) {
406900d495aSAlexey Orishko 		dev_kfree_skb_any(ctx->tx_curr_skb);
407900d495aSAlexey Orishko 		ctx->tx_curr_skb = NULL;
408900d495aSAlexey Orishko 	}
409900d495aSAlexey Orishko 
410900d495aSAlexey Orishko 	kfree(ctx);
411900d495aSAlexey Orishko }
412900d495aSAlexey Orishko 
413900d495aSAlexey Orishko static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
414900d495aSAlexey Orishko {
415900d495aSAlexey Orishko 	struct cdc_ncm_ctx *ctx;
416900d495aSAlexey Orishko 	struct usb_driver *driver;
417900d495aSAlexey Orishko 	u8 *buf;
418900d495aSAlexey Orishko 	int len;
419900d495aSAlexey Orishko 	int temp;
420900d495aSAlexey Orishko 	u8 iface_no;
421900d495aSAlexey Orishko 
422900d495aSAlexey Orishko 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
423900d495aSAlexey Orishko 	if (ctx == NULL)
424900d495aSAlexey Orishko 		goto error;
425900d495aSAlexey Orishko 
426900d495aSAlexey Orishko 	memset(ctx, 0, sizeof(*ctx));
427900d495aSAlexey Orishko 
428900d495aSAlexey Orishko 	init_timer(&ctx->tx_timer);
429900d495aSAlexey Orishko 	spin_lock_init(&ctx->mtx);
430900d495aSAlexey Orishko 	ctx->netdev = dev->net;
431900d495aSAlexey Orishko 
432900d495aSAlexey Orishko 	/* store ctx pointer in device data field */
433900d495aSAlexey Orishko 	dev->data[0] = (unsigned long)ctx;
434900d495aSAlexey Orishko 
435900d495aSAlexey Orishko 	/* get some pointers */
436900d495aSAlexey Orishko 	driver = driver_of(intf);
437900d495aSAlexey Orishko 	buf = intf->cur_altsetting->extra;
438900d495aSAlexey Orishko 	len = intf->cur_altsetting->extralen;
439900d495aSAlexey Orishko 
440900d495aSAlexey Orishko 	ctx->udev = dev->udev;
441900d495aSAlexey Orishko 	ctx->intf = intf;
442900d495aSAlexey Orishko 
443900d495aSAlexey Orishko 	/* parse through descriptors associated with control interface */
444900d495aSAlexey Orishko 	while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
445900d495aSAlexey Orishko 
446900d495aSAlexey Orishko 		if (buf[1] != USB_DT_CS_INTERFACE)
447900d495aSAlexey Orishko 			goto advance;
448900d495aSAlexey Orishko 
449900d495aSAlexey Orishko 		switch (buf[2]) {
450900d495aSAlexey Orishko 		case USB_CDC_UNION_TYPE:
451900d495aSAlexey Orishko 			if (buf[0] < sizeof(*(ctx->union_desc)))
452900d495aSAlexey Orishko 				break;
453900d495aSAlexey Orishko 
454900d495aSAlexey Orishko 			ctx->union_desc =
455900d495aSAlexey Orishko 					(const struct usb_cdc_union_desc *)buf;
456900d495aSAlexey Orishko 
457900d495aSAlexey Orishko 			ctx->control = usb_ifnum_to_if(dev->udev,
458900d495aSAlexey Orishko 					ctx->union_desc->bMasterInterface0);
459900d495aSAlexey Orishko 			ctx->data = usb_ifnum_to_if(dev->udev,
460900d495aSAlexey Orishko 					ctx->union_desc->bSlaveInterface0);
461900d495aSAlexey Orishko 			break;
462900d495aSAlexey Orishko 
463900d495aSAlexey Orishko 		case USB_CDC_ETHERNET_TYPE:
464900d495aSAlexey Orishko 			if (buf[0] < sizeof(*(ctx->ether_desc)))
465900d495aSAlexey Orishko 				break;
466900d495aSAlexey Orishko 
467900d495aSAlexey Orishko 			ctx->ether_desc =
468900d495aSAlexey Orishko 					(const struct usb_cdc_ether_desc *)buf;
469900d495aSAlexey Orishko 
470900d495aSAlexey Orishko 			dev->hard_mtu =
471900d495aSAlexey Orishko 				le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
472900d495aSAlexey Orishko 
473900d495aSAlexey Orishko 			if (dev->hard_mtu <
474900d495aSAlexey Orishko 			    (CDC_NCM_MIN_DATAGRAM_SIZE - ETH_HLEN))
475900d495aSAlexey Orishko 				dev->hard_mtu =
476900d495aSAlexey Orishko 					CDC_NCM_MIN_DATAGRAM_SIZE - ETH_HLEN;
477900d495aSAlexey Orishko 
478900d495aSAlexey Orishko 			else if (dev->hard_mtu >
479900d495aSAlexey Orishko 				 (CDC_NCM_MAX_DATAGRAM_SIZE - ETH_HLEN))
480900d495aSAlexey Orishko 				dev->hard_mtu =
481900d495aSAlexey Orishko 					CDC_NCM_MAX_DATAGRAM_SIZE - ETH_HLEN;
482900d495aSAlexey Orishko 			break;
483900d495aSAlexey Orishko 
484900d495aSAlexey Orishko 		case USB_CDC_NCM_TYPE:
485900d495aSAlexey Orishko 			if (buf[0] < sizeof(*(ctx->func_desc)))
486900d495aSAlexey Orishko 				break;
487900d495aSAlexey Orishko 
488900d495aSAlexey Orishko 			ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
489900d495aSAlexey Orishko 			break;
490900d495aSAlexey Orishko 
491900d495aSAlexey Orishko 		default:
492900d495aSAlexey Orishko 			break;
493900d495aSAlexey Orishko 		}
494900d495aSAlexey Orishko advance:
495900d495aSAlexey Orishko 		/* advance to next descriptor */
496900d495aSAlexey Orishko 		temp = buf[0];
497900d495aSAlexey Orishko 		buf += temp;
498900d495aSAlexey Orishko 		len -= temp;
499900d495aSAlexey Orishko 	}
500900d495aSAlexey Orishko 
501900d495aSAlexey Orishko 	/* check if we got everything */
502900d495aSAlexey Orishko 	if ((ctx->control == NULL) || (ctx->data == NULL) ||
503900d495aSAlexey Orishko 	    (ctx->ether_desc == NULL))
504900d495aSAlexey Orishko 		goto error;
505900d495aSAlexey Orishko 
506900d495aSAlexey Orishko 	/* claim interfaces, if any */
507900d495aSAlexey Orishko 	if (ctx->data != intf) {
508900d495aSAlexey Orishko 		temp = usb_driver_claim_interface(driver, ctx->data, dev);
509900d495aSAlexey Orishko 		if (temp)
510900d495aSAlexey Orishko 			goto error;
511900d495aSAlexey Orishko 		ctx->data_claimed = 1;
512900d495aSAlexey Orishko 	}
513900d495aSAlexey Orishko 
514900d495aSAlexey Orishko 	if (ctx->control != intf) {
515900d495aSAlexey Orishko 		temp = usb_driver_claim_interface(driver, ctx->control, dev);
516900d495aSAlexey Orishko 		if (temp)
517900d495aSAlexey Orishko 			goto error;
518900d495aSAlexey Orishko 		ctx->control_claimed = 1;
519900d495aSAlexey Orishko 	}
520900d495aSAlexey Orishko 
521900d495aSAlexey Orishko 	iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
522900d495aSAlexey Orishko 
523900d495aSAlexey Orishko 	/* reset data interface */
524900d495aSAlexey Orishko 	temp = usb_set_interface(dev->udev, iface_no, 0);
525900d495aSAlexey Orishko 	if (temp)
526900d495aSAlexey Orishko 		goto error;
527900d495aSAlexey Orishko 
528900d495aSAlexey Orishko 	/* initialize data interface */
529900d495aSAlexey Orishko 	if (cdc_ncm_setup(ctx))
530900d495aSAlexey Orishko 		goto error;
531900d495aSAlexey Orishko 
532900d495aSAlexey Orishko 	/* configure data interface */
533900d495aSAlexey Orishko 	temp = usb_set_interface(dev->udev, iface_no, 1);
534900d495aSAlexey Orishko 	if (temp)
535900d495aSAlexey Orishko 		goto error;
536900d495aSAlexey Orishko 
537900d495aSAlexey Orishko 	cdc_ncm_find_endpoints(ctx, ctx->data);
538900d495aSAlexey Orishko 	cdc_ncm_find_endpoints(ctx, ctx->control);
539900d495aSAlexey Orishko 
540900d495aSAlexey Orishko 	if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
541900d495aSAlexey Orishko 	    (ctx->status_ep == NULL))
542900d495aSAlexey Orishko 		goto error;
543900d495aSAlexey Orishko 
544900d495aSAlexey Orishko 	dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
545900d495aSAlexey Orishko 
546900d495aSAlexey Orishko 	usb_set_intfdata(ctx->data, dev);
547900d495aSAlexey Orishko 	usb_set_intfdata(ctx->control, dev);
548900d495aSAlexey Orishko 	usb_set_intfdata(ctx->intf, dev);
549900d495aSAlexey Orishko 
550900d495aSAlexey Orishko 	temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
551900d495aSAlexey Orishko 	if (temp)
552900d495aSAlexey Orishko 		goto error;
553900d495aSAlexey Orishko 
554900d495aSAlexey Orishko 	dev_info(&dev->udev->dev, "MAC-Address: "
555900d495aSAlexey Orishko 				"0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
556900d495aSAlexey Orishko 				dev->net->dev_addr[0], dev->net->dev_addr[1],
557900d495aSAlexey Orishko 				dev->net->dev_addr[2], dev->net->dev_addr[3],
558900d495aSAlexey Orishko 				dev->net->dev_addr[4], dev->net->dev_addr[5]);
559900d495aSAlexey Orishko 
560900d495aSAlexey Orishko 	dev->in = usb_rcvbulkpipe(dev->udev,
561900d495aSAlexey Orishko 		ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
562900d495aSAlexey Orishko 	dev->out = usb_sndbulkpipe(dev->udev,
563900d495aSAlexey Orishko 		ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
564900d495aSAlexey Orishko 	dev->status = ctx->status_ep;
565900d495aSAlexey Orishko 	dev->rx_urb_size = ctx->rx_max;
566900d495aSAlexey Orishko 
567900d495aSAlexey Orishko 	/*
568900d495aSAlexey Orishko 	 * We should get an event when network connection is "connected" or
569900d495aSAlexey Orishko 	 * "disconnected". Set network connection in "disconnected" state
570900d495aSAlexey Orishko 	 * (carrier is OFF) during attach, so the IP network stack does not
571900d495aSAlexey Orishko 	 * start IPv6 negotiation and more.
572900d495aSAlexey Orishko 	 */
573900d495aSAlexey Orishko 	netif_carrier_off(dev->net);
574900d495aSAlexey Orishko 	ctx->tx_speed = ctx->rx_speed = 0;
575900d495aSAlexey Orishko 	return 0;
576900d495aSAlexey Orishko 
577900d495aSAlexey Orishko error:
578900d495aSAlexey Orishko 	cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
579900d495aSAlexey Orishko 	dev->data[0] = 0;
580900d495aSAlexey Orishko 	dev_info(&dev->udev->dev, "Descriptor failure\n");
581900d495aSAlexey Orishko 	return -ENODEV;
582900d495aSAlexey Orishko }
583900d495aSAlexey Orishko 
584900d495aSAlexey Orishko static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
585900d495aSAlexey Orishko {
586900d495aSAlexey Orishko 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
587900d495aSAlexey Orishko 	struct usb_driver *driver;
588900d495aSAlexey Orishko 
589900d495aSAlexey Orishko 	if (ctx == NULL)
590900d495aSAlexey Orishko 		return;		/* no setup */
591900d495aSAlexey Orishko 
592900d495aSAlexey Orishko 	driver = driver_of(intf);
593900d495aSAlexey Orishko 
594900d495aSAlexey Orishko 	usb_set_intfdata(ctx->data, NULL);
595900d495aSAlexey Orishko 	usb_set_intfdata(ctx->control, NULL);
596900d495aSAlexey Orishko 	usb_set_intfdata(ctx->intf, NULL);
597900d495aSAlexey Orishko 
598900d495aSAlexey Orishko 	/* release interfaces, if any */
599900d495aSAlexey Orishko 	if (ctx->data_claimed) {
600900d495aSAlexey Orishko 		usb_driver_release_interface(driver, ctx->data);
601900d495aSAlexey Orishko 		ctx->data_claimed = 0;
602900d495aSAlexey Orishko 	}
603900d495aSAlexey Orishko 
604900d495aSAlexey Orishko 	if (ctx->control_claimed) {
605900d495aSAlexey Orishko 		usb_driver_release_interface(driver, ctx->control);
606900d495aSAlexey Orishko 		ctx->control_claimed = 0;
607900d495aSAlexey Orishko 	}
608900d495aSAlexey Orishko 
609900d495aSAlexey Orishko 	cdc_ncm_free(ctx);
610900d495aSAlexey Orishko }
611900d495aSAlexey Orishko 
612900d495aSAlexey Orishko static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
613900d495aSAlexey Orishko {
614900d495aSAlexey Orishko 	if (first >= max)
615900d495aSAlexey Orishko 		return;
616900d495aSAlexey Orishko 	if (first >= end)
617900d495aSAlexey Orishko 		return;
618900d495aSAlexey Orishko 	if (end > max)
619900d495aSAlexey Orishko 		end = max;
620900d495aSAlexey Orishko 	memset(ptr + first, 0, end - first);
621900d495aSAlexey Orishko }
622900d495aSAlexey Orishko 
623900d495aSAlexey Orishko static struct sk_buff *
624900d495aSAlexey Orishko cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
625900d495aSAlexey Orishko {
626900d495aSAlexey Orishko 	struct sk_buff *skb_out;
627900d495aSAlexey Orishko 	u32 rem;
628900d495aSAlexey Orishko 	u32 offset;
629900d495aSAlexey Orishko 	u32 last_offset;
630900d495aSAlexey Orishko 	u16 n = 0;
631900d495aSAlexey Orishko 	u8 timeout = 0;
632900d495aSAlexey Orishko 
633900d495aSAlexey Orishko 	/* if there is a remaining skb, it gets priority */
634900d495aSAlexey Orishko 	if (skb != NULL)
635900d495aSAlexey Orishko 		swap(skb, ctx->tx_rem_skb);
636900d495aSAlexey Orishko 	else
637900d495aSAlexey Orishko 		timeout = 1;
638900d495aSAlexey Orishko 
639900d495aSAlexey Orishko 	/*
640900d495aSAlexey Orishko 	 * +----------------+
641900d495aSAlexey Orishko 	 * | skb_out        |
642900d495aSAlexey Orishko 	 * +----------------+
643900d495aSAlexey Orishko 	 *           ^ offset
644900d495aSAlexey Orishko 	 *        ^ last_offset
645900d495aSAlexey Orishko 	 */
646900d495aSAlexey Orishko 
647900d495aSAlexey Orishko 	/* check if we are resuming an OUT skb */
648900d495aSAlexey Orishko 	if (ctx->tx_curr_skb != NULL) {
649900d495aSAlexey Orishko 		/* pop variables */
650900d495aSAlexey Orishko 		skb_out = ctx->tx_curr_skb;
651900d495aSAlexey Orishko 		offset = ctx->tx_curr_offset;
652900d495aSAlexey Orishko 		last_offset = ctx->tx_curr_last_offset;
653900d495aSAlexey Orishko 		n = ctx->tx_curr_frame_num;
654900d495aSAlexey Orishko 
655900d495aSAlexey Orishko 	} else {
656900d495aSAlexey Orishko 		/* reset variables */
657900d495aSAlexey Orishko 		skb_out = alloc_skb(ctx->tx_max, GFP_ATOMIC);
658900d495aSAlexey Orishko 		if (skb_out == NULL) {
659900d495aSAlexey Orishko 			if (skb != NULL) {
660900d495aSAlexey Orishko 				dev_kfree_skb_any(skb);
661900d495aSAlexey Orishko 				ctx->netdev->stats.tx_dropped++;
662900d495aSAlexey Orishko 			}
663900d495aSAlexey Orishko 			goto exit_no_skb;
664900d495aSAlexey Orishko 		}
665900d495aSAlexey Orishko 
666900d495aSAlexey Orishko 		/* make room for NTH and NDP */
667900d495aSAlexey Orishko 		offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
668900d495aSAlexey Orishko 					ctx->tx_ndp_modulus) +
669900d495aSAlexey Orishko 					sizeof(struct usb_cdc_ncm_ndp16) +
670900d495aSAlexey Orishko 					(ctx->tx_max_datagrams + 1) *
671900d495aSAlexey Orishko 					sizeof(struct usb_cdc_ncm_dpe16);
672900d495aSAlexey Orishko 
673900d495aSAlexey Orishko 		/* store last valid offset before alignment */
674900d495aSAlexey Orishko 		last_offset = offset;
675900d495aSAlexey Orishko 		/* align first Datagram offset correctly */
676900d495aSAlexey Orishko 		offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
677900d495aSAlexey Orishko 		/* zero buffer till the first IP datagram */
678900d495aSAlexey Orishko 		cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
679900d495aSAlexey Orishko 		n = 0;
680900d495aSAlexey Orishko 		ctx->tx_curr_frame_num = 0;
681900d495aSAlexey Orishko 	}
682900d495aSAlexey Orishko 
683900d495aSAlexey Orishko 	for (; n < ctx->tx_max_datagrams; n++) {
684900d495aSAlexey Orishko 		/* check if end of transmit buffer is reached */
685900d495aSAlexey Orishko 		if (offset >= ctx->tx_max)
686900d495aSAlexey Orishko 			break;
687900d495aSAlexey Orishko 
688900d495aSAlexey Orishko 		/* compute maximum buffer size */
689900d495aSAlexey Orishko 		rem = ctx->tx_max - offset;
690900d495aSAlexey Orishko 
691900d495aSAlexey Orishko 		if (skb == NULL) {
692900d495aSAlexey Orishko 			skb = ctx->tx_rem_skb;
693900d495aSAlexey Orishko 			ctx->tx_rem_skb = NULL;
694900d495aSAlexey Orishko 
695900d495aSAlexey Orishko 			/* check for end of skb */
696900d495aSAlexey Orishko 			if (skb == NULL)
697900d495aSAlexey Orishko 				break;
698900d495aSAlexey Orishko 		}
699900d495aSAlexey Orishko 
700900d495aSAlexey Orishko 		if (skb->len > rem) {
701900d495aSAlexey Orishko 			if (n == 0) {
702900d495aSAlexey Orishko 				/* won't fit, MTU problem? */
703900d495aSAlexey Orishko 				dev_kfree_skb_any(skb);
704900d495aSAlexey Orishko 				skb = NULL;
705900d495aSAlexey Orishko 				ctx->netdev->stats.tx_dropped++;
706900d495aSAlexey Orishko 			} else {
707900d495aSAlexey Orishko 				/* no room for skb - store for later */
708900d495aSAlexey Orishko 				if (ctx->tx_rem_skb != NULL) {
709900d495aSAlexey Orishko 					dev_kfree_skb_any(ctx->tx_rem_skb);
710900d495aSAlexey Orishko 					ctx->netdev->stats.tx_dropped++;
711900d495aSAlexey Orishko 				}
712900d495aSAlexey Orishko 				ctx->tx_rem_skb = skb;
713900d495aSAlexey Orishko 				skb = NULL;
714900d495aSAlexey Orishko 
715900d495aSAlexey Orishko 				/* loop one more time */
716900d495aSAlexey Orishko 				timeout = 1;
717900d495aSAlexey Orishko 			}
718900d495aSAlexey Orishko 			break;
719900d495aSAlexey Orishko 		}
720900d495aSAlexey Orishko 
721900d495aSAlexey Orishko 		memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
722900d495aSAlexey Orishko 
723900d495aSAlexey Orishko 		ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
724900d495aSAlexey Orishko 		ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
725900d495aSAlexey Orishko 
726900d495aSAlexey Orishko 		/* update offset */
727900d495aSAlexey Orishko 		offset += skb->len;
728900d495aSAlexey Orishko 
729900d495aSAlexey Orishko 		/* store last valid offset before alignment */
730900d495aSAlexey Orishko 		last_offset = offset;
731900d495aSAlexey Orishko 
732900d495aSAlexey Orishko 		/* align offset correctly */
733900d495aSAlexey Orishko 		offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
734900d495aSAlexey Orishko 
735900d495aSAlexey Orishko 		/* zero padding */
736900d495aSAlexey Orishko 		cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
737900d495aSAlexey Orishko 								ctx->tx_max);
738900d495aSAlexey Orishko 		dev_kfree_skb_any(skb);
739900d495aSAlexey Orishko 		skb = NULL;
740900d495aSAlexey Orishko 	}
741900d495aSAlexey Orishko 
742900d495aSAlexey Orishko 	/* free up any dangling skb */
743900d495aSAlexey Orishko 	if (skb != NULL) {
744900d495aSAlexey Orishko 		dev_kfree_skb_any(skb);
745900d495aSAlexey Orishko 		skb = NULL;
746900d495aSAlexey Orishko 		ctx->netdev->stats.tx_dropped++;
747900d495aSAlexey Orishko 	}
748900d495aSAlexey Orishko 
749900d495aSAlexey Orishko 	ctx->tx_curr_frame_num = n;
750900d495aSAlexey Orishko 
751900d495aSAlexey Orishko 	if (n == 0) {
752900d495aSAlexey Orishko 		/* wait for more frames */
753900d495aSAlexey Orishko 		/* push variables */
754900d495aSAlexey Orishko 		ctx->tx_curr_skb = skb_out;
755900d495aSAlexey Orishko 		ctx->tx_curr_offset = offset;
756900d495aSAlexey Orishko 		ctx->tx_curr_last_offset = last_offset;
757900d495aSAlexey Orishko 		goto exit_no_skb;
758900d495aSAlexey Orishko 
759900d495aSAlexey Orishko 	} else if ((n < ctx->tx_max_datagrams) && (timeout == 0)) {
760900d495aSAlexey Orishko 		/* wait for more frames */
761900d495aSAlexey Orishko 		/* push variables */
762900d495aSAlexey Orishko 		ctx->tx_curr_skb = skb_out;
763900d495aSAlexey Orishko 		ctx->tx_curr_offset = offset;
764900d495aSAlexey Orishko 		ctx->tx_curr_last_offset = last_offset;
765900d495aSAlexey Orishko 		/* set the pending count */
766900d495aSAlexey Orishko 		if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
767900d495aSAlexey Orishko 			ctx->tx_timer_pending = 2;
768900d495aSAlexey Orishko 		goto exit_no_skb;
769900d495aSAlexey Orishko 
770900d495aSAlexey Orishko 	} else {
771900d495aSAlexey Orishko 		/* frame goes out */
772900d495aSAlexey Orishko 		/* variables will be reset at next call */
773900d495aSAlexey Orishko 	}
774900d495aSAlexey Orishko 
775900d495aSAlexey Orishko 	/* check for overflow */
776900d495aSAlexey Orishko 	if (last_offset > ctx->tx_max)
777900d495aSAlexey Orishko 		last_offset = ctx->tx_max;
778900d495aSAlexey Orishko 
779900d495aSAlexey Orishko 	/* revert offset */
780900d495aSAlexey Orishko 	offset = last_offset;
781900d495aSAlexey Orishko 
782900d495aSAlexey Orishko 	/*
783900d495aSAlexey Orishko 	 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
784900d495aSAlexey Orishko 	 * we send buffers as it is. If we get more data, it would be more
785900d495aSAlexey Orishko 	 * efficient for USB HS mobile device with DMA engine to receive a full
786900d495aSAlexey Orishko 	 * size NTB, than canceling DMA transfer and receiving a short packet.
787900d495aSAlexey Orishko 	 */
788900d495aSAlexey Orishko 	if (offset > CDC_NCM_MIN_TX_PKT)
789900d495aSAlexey Orishko 		offset = ctx->tx_max;
790900d495aSAlexey Orishko 
791900d495aSAlexey Orishko 	/* final zero padding */
792900d495aSAlexey Orishko 	cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
793900d495aSAlexey Orishko 
794900d495aSAlexey Orishko 	/* store last offset */
795900d495aSAlexey Orishko 	last_offset = offset;
796900d495aSAlexey Orishko 
797900d495aSAlexey Orishko 	if ((last_offset < ctx->tx_max) && ((last_offset %
798900d495aSAlexey Orishko 			le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) {
799900d495aSAlexey Orishko 		/* force short packet */
800900d495aSAlexey Orishko 		*(((u8 *)skb_out->data) + last_offset) = 0;
801900d495aSAlexey Orishko 		last_offset++;
802900d495aSAlexey Orishko 	}
803900d495aSAlexey Orishko 
804900d495aSAlexey Orishko 	/* zero the rest of the DPEs plus the last NULL entry */
805900d495aSAlexey Orishko 	for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
806900d495aSAlexey Orishko 		ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
807900d495aSAlexey Orishko 		ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
808900d495aSAlexey Orishko 	}
809900d495aSAlexey Orishko 
810900d495aSAlexey Orishko 	/* fill out 16-bit NTB header */
811900d495aSAlexey Orishko 	ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
812900d495aSAlexey Orishko 	ctx->tx_ncm.nth16.wHeaderLength =
813900d495aSAlexey Orishko 					cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
814900d495aSAlexey Orishko 	ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
815900d495aSAlexey Orishko 	ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
816900d495aSAlexey Orishko 	ctx->tx_ncm.nth16.wFpIndex = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
817900d495aSAlexey Orishko 							ctx->tx_ndp_modulus);
818900d495aSAlexey Orishko 
819900d495aSAlexey Orishko 	memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
820900d495aSAlexey Orishko 	ctx->tx_seq++;
821900d495aSAlexey Orishko 
822900d495aSAlexey Orishko 	/* fill out 16-bit NDP table */
823900d495aSAlexey Orishko 	ctx->tx_ncm.ndp16.dwSignature =
824900d495aSAlexey Orishko 				cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
825900d495aSAlexey Orishko 	rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
826900d495aSAlexey Orishko 					sizeof(struct usb_cdc_ncm_dpe16));
827900d495aSAlexey Orishko 	ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
828900d495aSAlexey Orishko 	ctx->tx_ncm.ndp16.wNextFpIndex = 0; /* reserved */
829900d495aSAlexey Orishko 
830900d495aSAlexey Orishko 	memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wFpIndex,
831900d495aSAlexey Orishko 						&(ctx->tx_ncm.ndp16),
832900d495aSAlexey Orishko 						sizeof(ctx->tx_ncm.ndp16));
833900d495aSAlexey Orishko 
834900d495aSAlexey Orishko 	memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wFpIndex +
835900d495aSAlexey Orishko 					sizeof(ctx->tx_ncm.ndp16),
836900d495aSAlexey Orishko 					&(ctx->tx_ncm.dpe16),
837900d495aSAlexey Orishko 					(ctx->tx_curr_frame_num + 1) *
838900d495aSAlexey Orishko 					sizeof(struct usb_cdc_ncm_dpe16));
839900d495aSAlexey Orishko 
840900d495aSAlexey Orishko 	/* set frame length */
841900d495aSAlexey Orishko 	skb_put(skb_out, last_offset);
842900d495aSAlexey Orishko 
843900d495aSAlexey Orishko 	/* return skb */
844900d495aSAlexey Orishko 	ctx->tx_curr_skb = NULL;
845900d495aSAlexey Orishko 	return skb_out;
846900d495aSAlexey Orishko 
847900d495aSAlexey Orishko exit_no_skb:
848900d495aSAlexey Orishko 	return NULL;
849900d495aSAlexey Orishko }
850900d495aSAlexey Orishko 
851900d495aSAlexey Orishko static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
852900d495aSAlexey Orishko {
853900d495aSAlexey Orishko 	/* start timer, if not already started */
854900d495aSAlexey Orishko 	if (timer_pending(&ctx->tx_timer) == 0) {
855900d495aSAlexey Orishko 		ctx->tx_timer.function = &cdc_ncm_tx_timeout;
856900d495aSAlexey Orishko 		ctx->tx_timer.data = (unsigned long)ctx;
857900d495aSAlexey Orishko 		ctx->tx_timer.expires = jiffies + ((HZ + 999) / 1000);
858900d495aSAlexey Orishko 		add_timer(&ctx->tx_timer);
859900d495aSAlexey Orishko 	}
860900d495aSAlexey Orishko }
861900d495aSAlexey Orishko 
862900d495aSAlexey Orishko static void cdc_ncm_tx_timeout(unsigned long arg)
863900d495aSAlexey Orishko {
864900d495aSAlexey Orishko 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)arg;
865900d495aSAlexey Orishko 	u8 restart;
866900d495aSAlexey Orishko 
867900d495aSAlexey Orishko 	spin_lock(&ctx->mtx);
868900d495aSAlexey Orishko 	if (ctx->tx_timer_pending != 0) {
869900d495aSAlexey Orishko 		ctx->tx_timer_pending--;
870900d495aSAlexey Orishko 		restart = 1;
871900d495aSAlexey Orishko 	} else
872900d495aSAlexey Orishko 		restart = 0;
873900d495aSAlexey Orishko 
874900d495aSAlexey Orishko 	spin_unlock(&ctx->mtx);
875900d495aSAlexey Orishko 
876900d495aSAlexey Orishko 	if (restart)
877900d495aSAlexey Orishko 		cdc_ncm_tx_timeout_start(ctx);
878900d495aSAlexey Orishko 	else if (ctx->netdev != NULL)
879900d495aSAlexey Orishko 		usbnet_start_xmit(NULL, ctx->netdev);
880900d495aSAlexey Orishko }
881900d495aSAlexey Orishko 
882900d495aSAlexey Orishko static struct sk_buff *
883900d495aSAlexey Orishko cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
884900d495aSAlexey Orishko {
885900d495aSAlexey Orishko 	struct sk_buff *skb_out;
886900d495aSAlexey Orishko 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
887900d495aSAlexey Orishko 	u8 need_timer = 0;
888900d495aSAlexey Orishko 
889900d495aSAlexey Orishko 	/*
890900d495aSAlexey Orishko 	 * The Ethernet API we are using does not support transmitting
891900d495aSAlexey Orishko 	 * multiple Ethernet frames in a single call. This driver will
892900d495aSAlexey Orishko 	 * accumulate multiple Ethernet frames and send out a larger
893900d495aSAlexey Orishko 	 * USB frame when the USB buffer is full or when a single jiffies
894900d495aSAlexey Orishko 	 * timeout happens.
895900d495aSAlexey Orishko 	 */
896900d495aSAlexey Orishko 	if (ctx == NULL)
897900d495aSAlexey Orishko 		goto error;
898900d495aSAlexey Orishko 
899900d495aSAlexey Orishko 	spin_lock(&ctx->mtx);
900900d495aSAlexey Orishko 	skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
901900d495aSAlexey Orishko 	if (ctx->tx_curr_skb != NULL)
902900d495aSAlexey Orishko 		need_timer = 1;
903900d495aSAlexey Orishko 	spin_unlock(&ctx->mtx);
904900d495aSAlexey Orishko 
905900d495aSAlexey Orishko 	/* Start timer, if there is a remaining skb */
906900d495aSAlexey Orishko 	if (need_timer)
907900d495aSAlexey Orishko 		cdc_ncm_tx_timeout_start(ctx);
908900d495aSAlexey Orishko 
909900d495aSAlexey Orishko 	if (skb_out)
910900d495aSAlexey Orishko 		dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
911900d495aSAlexey Orishko 	return skb_out;
912900d495aSAlexey Orishko 
913900d495aSAlexey Orishko error:
914900d495aSAlexey Orishko 	if (skb != NULL)
915900d495aSAlexey Orishko 		dev_kfree_skb_any(skb);
916900d495aSAlexey Orishko 
917900d495aSAlexey Orishko 	return NULL;
918900d495aSAlexey Orishko }
919900d495aSAlexey Orishko 
920900d495aSAlexey Orishko static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
921900d495aSAlexey Orishko {
922900d495aSAlexey Orishko 	struct sk_buff *skb;
923900d495aSAlexey Orishko 	struct cdc_ncm_ctx *ctx;
924900d495aSAlexey Orishko 	int sumlen;
925900d495aSAlexey Orishko 	int actlen;
926900d495aSAlexey Orishko 	int temp;
927900d495aSAlexey Orishko 	int nframes;
928900d495aSAlexey Orishko 	int x;
929900d495aSAlexey Orishko 	int offset;
930900d495aSAlexey Orishko 
931900d495aSAlexey Orishko 	ctx = (struct cdc_ncm_ctx *)dev->data[0];
932900d495aSAlexey Orishko 	if (ctx == NULL)
933900d495aSAlexey Orishko 		goto error;
934900d495aSAlexey Orishko 
935900d495aSAlexey Orishko 	actlen = skb_in->len;
936900d495aSAlexey Orishko 	sumlen = CDC_NCM_NTB_MAX_SIZE_RX;
937900d495aSAlexey Orishko 
938900d495aSAlexey Orishko 	if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) {
939900d495aSAlexey Orishko 		pr_debug("frame too short\n");
940900d495aSAlexey Orishko 		goto error;
941900d495aSAlexey Orishko 	}
942900d495aSAlexey Orishko 
943900d495aSAlexey Orishko 	memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data),
944900d495aSAlexey Orishko 						sizeof(ctx->rx_ncm.nth16));
945900d495aSAlexey Orishko 
946900d495aSAlexey Orishko 	if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) !=
947900d495aSAlexey Orishko 	    USB_CDC_NCM_NTH16_SIGN) {
948900d495aSAlexey Orishko 		pr_debug("invalid NTH16 signature <%u>\n",
949900d495aSAlexey Orishko 			 le32_to_cpu(ctx->rx_ncm.nth16.dwSignature));
950900d495aSAlexey Orishko 		goto error;
951900d495aSAlexey Orishko 	}
952900d495aSAlexey Orishko 
953900d495aSAlexey Orishko 	temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength);
954900d495aSAlexey Orishko 	if (temp > sumlen) {
955900d495aSAlexey Orishko 		pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen);
956900d495aSAlexey Orishko 		goto error;
957900d495aSAlexey Orishko 	}
958900d495aSAlexey Orishko 
959900d495aSAlexey Orishko 	temp = le16_to_cpu(ctx->rx_ncm.nth16.wFpIndex);
960900d495aSAlexey Orishko 	if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) {
961900d495aSAlexey Orishko 		pr_debug("invalid DPT16 index\n");
962900d495aSAlexey Orishko 		goto error;
963900d495aSAlexey Orishko 	}
964900d495aSAlexey Orishko 
965900d495aSAlexey Orishko 	memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp,
966900d495aSAlexey Orishko 						sizeof(ctx->rx_ncm.ndp16));
967900d495aSAlexey Orishko 
968900d495aSAlexey Orishko 	if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) !=
969900d495aSAlexey Orishko 	    USB_CDC_NCM_NDP16_NOCRC_SIGN) {
970900d495aSAlexey Orishko 		pr_debug("invalid DPT16 signature <%u>\n",
971900d495aSAlexey Orishko 			 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
972900d495aSAlexey Orishko 		goto error;
973900d495aSAlexey Orishko 	}
974900d495aSAlexey Orishko 
975900d495aSAlexey Orishko 	if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) <
976900d495aSAlexey Orishko 	    USB_CDC_NCM_NDP16_LENGTH_MIN) {
977900d495aSAlexey Orishko 		pr_debug("invalid DPT16 length <%u>\n",
978900d495aSAlexey Orishko 			 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
979900d495aSAlexey Orishko 		goto error;
980900d495aSAlexey Orishko 	}
981900d495aSAlexey Orishko 
982900d495aSAlexey Orishko 	nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) -
983900d495aSAlexey Orishko 					sizeof(struct usb_cdc_ncm_ndp16)) /
984900d495aSAlexey Orishko 					sizeof(struct usb_cdc_ncm_dpe16));
985900d495aSAlexey Orishko 	nframes--; /* we process NDP entries except for the last one */
986900d495aSAlexey Orishko 
987900d495aSAlexey Orishko 	pr_debug("nframes = %u\n", nframes);
988900d495aSAlexey Orishko 
989900d495aSAlexey Orishko 	temp += sizeof(ctx->rx_ncm.ndp16);
990900d495aSAlexey Orishko 
991900d495aSAlexey Orishko 	if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) {
992900d495aSAlexey Orishko 		pr_debug("Invalid nframes = %d\n", nframes);
993900d495aSAlexey Orishko 		goto error;
994900d495aSAlexey Orishko 	}
995900d495aSAlexey Orishko 
996900d495aSAlexey Orishko 	if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) {
997900d495aSAlexey Orishko 		pr_debug("Truncating number of frames from %u to %u\n",
998900d495aSAlexey Orishko 					nframes, CDC_NCM_DPT_DATAGRAMS_MAX);
999900d495aSAlexey Orishko 		nframes = CDC_NCM_DPT_DATAGRAMS_MAX;
1000900d495aSAlexey Orishko 	}
1001900d495aSAlexey Orishko 
1002900d495aSAlexey Orishko 	memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp,
1003900d495aSAlexey Orishko 				nframes * (sizeof(struct usb_cdc_ncm_dpe16)));
1004900d495aSAlexey Orishko 
1005900d495aSAlexey Orishko 	for (x = 0; x < nframes; x++) {
1006900d495aSAlexey Orishko 		offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex);
1007900d495aSAlexey Orishko 		temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength);
1008900d495aSAlexey Orishko 
1009900d495aSAlexey Orishko 		/*
1010900d495aSAlexey Orishko 		 * CDC NCM ch. 3.7
1011900d495aSAlexey Orishko 		 * All entries after first NULL entry are to be ignored
1012900d495aSAlexey Orishko 		 */
1013900d495aSAlexey Orishko 		if ((offset == 0) || (temp == 0)) {
1014900d495aSAlexey Orishko 			if (!x)
1015900d495aSAlexey Orishko 				goto error; /* empty NTB */
1016900d495aSAlexey Orishko 			break;
1017900d495aSAlexey Orishko 		}
1018900d495aSAlexey Orishko 
1019900d495aSAlexey Orishko 		/* sanity checking */
1020900d495aSAlexey Orishko 		if (((offset + temp) > actlen) ||
1021900d495aSAlexey Orishko 		    (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) {
1022900d495aSAlexey Orishko 			pr_debug("invalid frame detected (ignored)"
1023900d495aSAlexey Orishko 				"offset[%u]=%u, length=%u, skb=%p\n",
1024900d495aSAlexey Orishko 							x, offset, temp, skb);
1025900d495aSAlexey Orishko 			if (!x)
1026900d495aSAlexey Orishko 				goto error;
1027900d495aSAlexey Orishko 			break;
1028900d495aSAlexey Orishko 
1029900d495aSAlexey Orishko 		} else {
1030900d495aSAlexey Orishko 			skb = skb_clone(skb_in, GFP_ATOMIC);
1031900d495aSAlexey Orishko 			skb->len = temp;
1032900d495aSAlexey Orishko 			skb->data = ((u8 *)skb_in->data) + offset;
1033900d495aSAlexey Orishko 			skb_set_tail_pointer(skb, temp);
1034900d495aSAlexey Orishko 			usbnet_skb_return(dev, skb);
1035900d495aSAlexey Orishko 		}
1036900d495aSAlexey Orishko 	}
1037900d495aSAlexey Orishko 	return 1;
1038900d495aSAlexey Orishko error:
1039900d495aSAlexey Orishko 	return 0;
1040900d495aSAlexey Orishko }
1041900d495aSAlexey Orishko 
1042900d495aSAlexey Orishko static void
1043900d495aSAlexey Orishko cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
1044900d495aSAlexey Orishko 		     struct connection_speed_change *data)
1045900d495aSAlexey Orishko {
1046900d495aSAlexey Orishko 	uint32_t rx_speed = le32_to_cpu(data->USBitRate);
1047900d495aSAlexey Orishko 	uint32_t tx_speed = le32_to_cpu(data->DSBitRate);
1048900d495aSAlexey Orishko 
1049900d495aSAlexey Orishko 	/*
1050900d495aSAlexey Orishko 	 * Currently the USB-NET API does not support reporting the actual
1051900d495aSAlexey Orishko 	 * device speed. Do print it instead.
1052900d495aSAlexey Orishko 	 */
1053900d495aSAlexey Orishko 	if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1054900d495aSAlexey Orishko 		ctx->tx_speed = tx_speed;
1055900d495aSAlexey Orishko 		ctx->rx_speed = rx_speed;
1056900d495aSAlexey Orishko 
1057900d495aSAlexey Orishko 		if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1058900d495aSAlexey Orishko 			printk(KERN_INFO KBUILD_MODNAME
1059900d495aSAlexey Orishko 				": %s: %u mbit/s downlink "
1060900d495aSAlexey Orishko 				"%u mbit/s uplink\n",
1061900d495aSAlexey Orishko 				ctx->netdev->name,
1062900d495aSAlexey Orishko 				(unsigned int)(rx_speed / 1000000U),
1063900d495aSAlexey Orishko 				(unsigned int)(tx_speed / 1000000U));
1064900d495aSAlexey Orishko 		} else {
1065900d495aSAlexey Orishko 			printk(KERN_INFO KBUILD_MODNAME
1066900d495aSAlexey Orishko 				": %s: %u kbit/s downlink "
1067900d495aSAlexey Orishko 				"%u kbit/s uplink\n",
1068900d495aSAlexey Orishko 				ctx->netdev->name,
1069900d495aSAlexey Orishko 				(unsigned int)(rx_speed / 1000U),
1070900d495aSAlexey Orishko 				(unsigned int)(tx_speed / 1000U));
1071900d495aSAlexey Orishko 		}
1072900d495aSAlexey Orishko 	}
1073900d495aSAlexey Orishko }
1074900d495aSAlexey Orishko 
1075900d495aSAlexey Orishko static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1076900d495aSAlexey Orishko {
1077900d495aSAlexey Orishko 	struct cdc_ncm_ctx *ctx;
1078900d495aSAlexey Orishko 	struct usb_cdc_notification *event;
1079900d495aSAlexey Orishko 
1080900d495aSAlexey Orishko 	ctx = (struct cdc_ncm_ctx *)dev->data[0];
1081900d495aSAlexey Orishko 
1082900d495aSAlexey Orishko 	if (urb->actual_length < sizeof(*event))
1083900d495aSAlexey Orishko 		return;
1084900d495aSAlexey Orishko 
1085900d495aSAlexey Orishko 	/* test for split data in 8-byte chunks */
1086900d495aSAlexey Orishko 	if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1087900d495aSAlexey Orishko 		cdc_ncm_speed_change(ctx,
1088900d495aSAlexey Orishko 		      (struct connection_speed_change *)urb->transfer_buffer);
1089900d495aSAlexey Orishko 		return;
1090900d495aSAlexey Orishko 	}
1091900d495aSAlexey Orishko 
1092900d495aSAlexey Orishko 	event = urb->transfer_buffer;
1093900d495aSAlexey Orishko 
1094900d495aSAlexey Orishko 	switch (event->bNotificationType) {
1095900d495aSAlexey Orishko 	case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1096900d495aSAlexey Orishko 		/*
1097900d495aSAlexey Orishko 		 * According to the CDC NCM specification ch.7.1
1098900d495aSAlexey Orishko 		 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1099900d495aSAlexey Orishko 		 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1100900d495aSAlexey Orishko 		 */
1101900d495aSAlexey Orishko 		ctx->connected = event->wValue;
1102900d495aSAlexey Orishko 
1103900d495aSAlexey Orishko 		printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1104900d495aSAlexey Orishko 			" %sconnected\n",
1105900d495aSAlexey Orishko 			ctx->netdev->name, ctx->connected ? "" : "dis");
1106900d495aSAlexey Orishko 
1107900d495aSAlexey Orishko 		if (ctx->connected)
1108900d495aSAlexey Orishko 			netif_carrier_on(dev->net);
1109900d495aSAlexey Orishko 		else {
1110900d495aSAlexey Orishko 			netif_carrier_off(dev->net);
1111900d495aSAlexey Orishko 			ctx->tx_speed = ctx->rx_speed = 0;
1112900d495aSAlexey Orishko 		}
1113900d495aSAlexey Orishko 		break;
1114900d495aSAlexey Orishko 
1115900d495aSAlexey Orishko 	case USB_CDC_NOTIFY_SPEED_CHANGE:
1116900d495aSAlexey Orishko 		if (urb->actual_length <
1117900d495aSAlexey Orishko 		    (sizeof(*event) + sizeof(struct connection_speed_change)))
1118900d495aSAlexey Orishko 			set_bit(EVENT_STS_SPLIT, &dev->flags);
1119900d495aSAlexey Orishko 		else
1120900d495aSAlexey Orishko 			cdc_ncm_speed_change(ctx,
1121900d495aSAlexey Orishko 				(struct connection_speed_change *) &event[1]);
1122900d495aSAlexey Orishko 		break;
1123900d495aSAlexey Orishko 
1124900d495aSAlexey Orishko 	default:
1125900d495aSAlexey Orishko 		dev_err(&dev->udev->dev, "NCM: unexpected "
1126900d495aSAlexey Orishko 			"notification 0x%02x!\n", event->bNotificationType);
1127900d495aSAlexey Orishko 		break;
1128900d495aSAlexey Orishko 	}
1129900d495aSAlexey Orishko }
1130900d495aSAlexey Orishko 
1131900d495aSAlexey Orishko static int cdc_ncm_check_connect(struct usbnet *dev)
1132900d495aSAlexey Orishko {
1133900d495aSAlexey Orishko 	struct cdc_ncm_ctx *ctx;
1134900d495aSAlexey Orishko 
1135900d495aSAlexey Orishko 	ctx = (struct cdc_ncm_ctx *)dev->data[0];
1136900d495aSAlexey Orishko 	if (ctx == NULL)
1137900d495aSAlexey Orishko 		return 1;	/* disconnected */
1138900d495aSAlexey Orishko 
1139900d495aSAlexey Orishko 	return !ctx->connected;
1140900d495aSAlexey Orishko }
1141900d495aSAlexey Orishko 
1142900d495aSAlexey Orishko static int
1143900d495aSAlexey Orishko cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1144900d495aSAlexey Orishko {
1145900d495aSAlexey Orishko 	return usbnet_probe(udev, prod);
1146900d495aSAlexey Orishko }
1147900d495aSAlexey Orishko 
1148900d495aSAlexey Orishko static void cdc_ncm_disconnect(struct usb_interface *intf)
1149900d495aSAlexey Orishko {
1150900d495aSAlexey Orishko 	struct usbnet *dev = usb_get_intfdata(intf);
1151900d495aSAlexey Orishko 
1152900d495aSAlexey Orishko 	if (dev == NULL)
1153900d495aSAlexey Orishko 		return;		/* already disconnected */
1154900d495aSAlexey Orishko 
1155900d495aSAlexey Orishko 	usbnet_disconnect(intf);
1156900d495aSAlexey Orishko }
1157900d495aSAlexey Orishko 
1158900d495aSAlexey Orishko static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1159900d495aSAlexey Orishko {
1160900d495aSAlexey Orishko 	dev->intf->needs_remote_wakeup = status;
1161900d495aSAlexey Orishko 	return 0;
1162900d495aSAlexey Orishko }
1163900d495aSAlexey Orishko 
1164900d495aSAlexey Orishko static const struct driver_info cdc_ncm_info = {
1165900d495aSAlexey Orishko 	.description = "CDC NCM",
1166900d495aSAlexey Orishko 	.flags = FLAG_NO_SETINT | FLAG_MULTI_PACKET,
1167900d495aSAlexey Orishko 	.bind = cdc_ncm_bind,
1168900d495aSAlexey Orishko 	.unbind = cdc_ncm_unbind,
1169900d495aSAlexey Orishko 	.check_connect = cdc_ncm_check_connect,
1170900d495aSAlexey Orishko 	.manage_power = cdc_ncm_manage_power,
1171900d495aSAlexey Orishko 	.status = cdc_ncm_status,
1172900d495aSAlexey Orishko 	.rx_fixup = cdc_ncm_rx_fixup,
1173900d495aSAlexey Orishko 	.tx_fixup = cdc_ncm_tx_fixup,
1174900d495aSAlexey Orishko };
1175900d495aSAlexey Orishko 
1176900d495aSAlexey Orishko static struct usb_driver cdc_ncm_driver = {
1177900d495aSAlexey Orishko 	.name = "cdc_ncm",
1178900d495aSAlexey Orishko 	.id_table = cdc_devs,
1179900d495aSAlexey Orishko 	.probe = cdc_ncm_probe,
1180900d495aSAlexey Orishko 	.disconnect = cdc_ncm_disconnect,
1181900d495aSAlexey Orishko 	.suspend = usbnet_suspend,
1182900d495aSAlexey Orishko 	.resume = usbnet_resume,
1183900d495aSAlexey Orishko 	.supports_autosuspend = 1,
1184900d495aSAlexey Orishko };
1185900d495aSAlexey Orishko 
1186900d495aSAlexey Orishko static struct ethtool_ops cdc_ncm_ethtool_ops = {
1187900d495aSAlexey Orishko 	.get_drvinfo = cdc_ncm_get_drvinfo,
1188900d495aSAlexey Orishko 	.get_link = usbnet_get_link,
1189900d495aSAlexey Orishko 	.get_msglevel = usbnet_get_msglevel,
1190900d495aSAlexey Orishko 	.set_msglevel = usbnet_set_msglevel,
1191900d495aSAlexey Orishko 	.get_settings = usbnet_get_settings,
1192900d495aSAlexey Orishko 	.set_settings = usbnet_set_settings,
1193900d495aSAlexey Orishko 	.nway_reset = usbnet_nway_reset,
1194900d495aSAlexey Orishko };
1195900d495aSAlexey Orishko 
1196900d495aSAlexey Orishko static int __init cdc_ncm_init(void)
1197900d495aSAlexey Orishko {
1198900d495aSAlexey Orishko 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
1199900d495aSAlexey Orishko 	return usb_register(&cdc_ncm_driver);
1200900d495aSAlexey Orishko }
1201900d495aSAlexey Orishko 
1202900d495aSAlexey Orishko module_init(cdc_ncm_init);
1203900d495aSAlexey Orishko 
1204900d495aSAlexey Orishko static void __exit cdc_ncm_exit(void)
1205900d495aSAlexey Orishko {
1206900d495aSAlexey Orishko 	usb_deregister(&cdc_ncm_driver);
1207900d495aSAlexey Orishko }
1208900d495aSAlexey Orishko 
1209900d495aSAlexey Orishko module_exit(cdc_ncm_exit);
1210900d495aSAlexey Orishko 
1211900d495aSAlexey Orishko MODULE_AUTHOR("Hans Petter Selasky");
1212900d495aSAlexey Orishko MODULE_DESCRIPTION("USB CDC NCM host driver");
1213900d495aSAlexey Orishko MODULE_LICENSE("Dual BSD/GPL");
1214