blob: d3b9e958db0bd38c585c4d8fcfa6a04ba5c9bc2e [file] [log] [blame]
Alexey Orishko900d4952010-11-29 23:23:28 +00001/*
2 * cdc_ncm.c
3 *
Alexey Orishko84e77a82011-02-07 09:45:10 +00004 * Copyright (C) ST-Ericsson 2010-2011
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>
50#include <linux/version.h>
51#include <linux/timer.h>
52#include <linux/spinlock.h>
53#include <linux/atomic.h>
54#include <linux/usb/usbnet.h>
55#include <linux/usb/cdc.h>
56
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +000057#define DRIVER_VERSION "04-Aug-2011"
Alexey Orishko900d4952010-11-29 23:23:28 +000058
59/* CDC NCM subclass 3.2.1 */
60#define USB_CDC_NCM_NDP16_LENGTH_MIN 0x10
61
62/* Maximum NTB length */
Alexey Orishko6c604082011-05-06 03:01:30 +000063#define CDC_NCM_NTB_MAX_SIZE_TX 16384 /* bytes */
Alexey Orishko900d4952010-11-29 23:23:28 +000064#define CDC_NCM_NTB_MAX_SIZE_RX 16384 /* bytes */
65
66/* Minimum value for MaxDatagramSize, ch. 6.2.9 */
67#define CDC_NCM_MIN_DATAGRAM_SIZE 1514 /* bytes */
68
69#define CDC_NCM_MIN_TX_PKT 512 /* bytes */
70
71/* Default value for MaxDatagramSize */
72#define CDC_NCM_MAX_DATAGRAM_SIZE 2048 /* bytes */
73
74/*
75 * Maximum amount of datagrams in NCM Datagram Pointer Table, not counting
76 * the last NULL entry. Any additional datagrams in NTB would be discarded.
77 */
78#define CDC_NCM_DPT_DATAGRAMS_MAX 32
79
Alexey Orishko84e77a82011-02-07 09:45:10 +000080/* Maximum amount of IN datagrams in NTB */
81#define CDC_NCM_DPT_DATAGRAMS_IN_MAX 0 /* unlimited */
82
Alexey Orishko900d4952010-11-29 23:23:28 +000083/* Restart the timer, if amount of datagrams is less than given value */
84#define CDC_NCM_RESTART_TIMER_DATAGRAM_CNT 3
85
86/* The following macro defines the minimum header space */
87#define CDC_NCM_MIN_HDR_SIZE \
88 (sizeof(struct usb_cdc_ncm_nth16) + sizeof(struct usb_cdc_ncm_ndp16) + \
89 (CDC_NCM_DPT_DATAGRAMS_MAX + 1) * sizeof(struct usb_cdc_ncm_dpe16))
90
Alexey Orishko900d4952010-11-29 23:23:28 +000091struct cdc_ncm_data {
92 struct usb_cdc_ncm_nth16 nth16;
93 struct usb_cdc_ncm_ndp16 ndp16;
94 struct usb_cdc_ncm_dpe16 dpe16[CDC_NCM_DPT_DATAGRAMS_MAX + 1];
95};
96
97struct cdc_ncm_ctx {
98 struct cdc_ncm_data rx_ncm;
99 struct cdc_ncm_data tx_ncm;
100 struct usb_cdc_ncm_ntb_parameters ncm_parm;
101 struct timer_list tx_timer;
102
103 const struct usb_cdc_ncm_desc *func_desc;
104 const struct usb_cdc_header_desc *header_desc;
105 const struct usb_cdc_union_desc *union_desc;
106 const struct usb_cdc_ether_desc *ether_desc;
107
108 struct net_device *netdev;
109 struct usb_device *udev;
110 struct usb_host_endpoint *in_ep;
111 struct usb_host_endpoint *out_ep;
112 struct usb_host_endpoint *status_ep;
113 struct usb_interface *intf;
114 struct usb_interface *control;
115 struct usb_interface *data;
116
117 struct sk_buff *tx_curr_skb;
118 struct sk_buff *tx_rem_skb;
119
120 spinlock_t mtx;
121
122 u32 tx_timer_pending;
123 u32 tx_curr_offset;
124 u32 tx_curr_last_offset;
125 u32 tx_curr_frame_num;
126 u32 rx_speed;
127 u32 tx_speed;
128 u32 rx_max;
129 u32 tx_max;
130 u32 max_datagram_size;
131 u16 tx_max_datagrams;
132 u16 tx_remainder;
133 u16 tx_modulus;
134 u16 tx_ndp_modulus;
135 u16 tx_seq;
136 u16 connected;
Alexey Orishko900d4952010-11-29 23:23:28 +0000137};
138
139static void cdc_ncm_tx_timeout(unsigned long arg);
140static const struct driver_info cdc_ncm_info;
141static struct usb_driver cdc_ncm_driver;
142static struct ethtool_ops cdc_ncm_ethtool_ops;
143
144static const struct usb_device_id cdc_devs[] = {
145 { USB_INTERFACE_INFO(USB_CLASS_COMM,
146 USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
147 .driver_info = (unsigned long)&cdc_ncm_info,
148 },
149 {
150 },
151};
152
153MODULE_DEVICE_TABLE(usb, cdc_devs);
154
155static void
156cdc_ncm_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
157{
158 struct usbnet *dev = netdev_priv(net);
159
160 strncpy(info->driver, dev->driver_name, sizeof(info->driver));
161 strncpy(info->version, DRIVER_VERSION, sizeof(info->version));
162 strncpy(info->fw_version, dev->driver_info->description,
163 sizeof(info->fw_version));
164 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
165}
166
Alexey Orishko900d4952010-11-29 23:23:28 +0000167static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
168{
Alexey Orishko900d4952010-11-29 23:23:28 +0000169 u32 val;
Alexey Orishko900d4952010-11-29 23:23:28 +0000170 u8 flags;
171 u8 iface_no;
172 int err;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000173 u16 ntb_fmt_supported;
Alexey Orishko900d4952010-11-29 23:23:28 +0000174
175 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
176
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000177 err = usb_control_msg(ctx->udev,
178 usb_rcvctrlpipe(ctx->udev, 0),
179 USB_CDC_GET_NTB_PARAMETERS,
180 USB_TYPE_CLASS | USB_DIR_IN
181 | USB_RECIP_INTERFACE,
182 0, iface_no, &ctx->ncm_parm,
183 sizeof(ctx->ncm_parm), 10000);
184 if (err < 0) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000185 pr_debug("failed GET_NTB_PARAMETERS\n");
186 return 1;
187 }
188
189 /* read correct set of parameters according to device mode */
190 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
191 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
192 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
193 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
194 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000195 /* devices prior to NCM Errata shall set this field to zero */
196 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
197 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
Alexey Orishko900d4952010-11-29 23:23:28 +0000198
199 if (ctx->func_desc != NULL)
200 flags = ctx->func_desc->bmNetworkCapabilities;
201 else
202 flags = 0;
203
204 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
205 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
Alexey Orishko84e77a82011-02-07 09:45:10 +0000206 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
Alexey Orishko900d4952010-11-29 23:23:28 +0000207 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
Alexey Orishko84e77a82011-02-07 09:45:10 +0000208 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
Alexey Orishko900d4952010-11-29 23:23:28 +0000209
Alexey Orishko84e77a82011-02-07 09:45:10 +0000210 /* max count of tx datagrams */
211 if ((ctx->tx_max_datagrams == 0) ||
212 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
213 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
Alexey Orishko900d4952010-11-29 23:23:28 +0000214
215 /* verify maximum size of received NTB in bytes */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000216 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
217 pr_debug("Using min receive length=%d\n",
218 USB_CDC_NCM_NTB_MIN_IN_SIZE);
219 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
220 }
221
222 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000223 pr_debug("Using default maximum receive length=%d\n",
224 CDC_NCM_NTB_MAX_SIZE_RX);
225 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
226 }
227
Alexey Orishko84e77a82011-02-07 09:45:10 +0000228 /* inform device about NTB input size changes */
229 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000230
231 if (flags & USB_CDC_NCM_NCAP_NTB_INPUT_SIZE) {
232 struct usb_cdc_ncm_ndp_input_size ndp_in_sz;
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000233 err = usb_control_msg(ctx->udev,
234 usb_sndctrlpipe(ctx->udev, 0),
235 USB_CDC_SET_NTB_INPUT_SIZE,
236 USB_TYPE_CLASS | USB_DIR_OUT
237 | USB_RECIP_INTERFACE,
238 0, iface_no, &ndp_in_sz, 8, 1000);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000239 } else {
240 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000241 err = usb_control_msg(ctx->udev,
242 usb_sndctrlpipe(ctx->udev, 0),
243 USB_CDC_SET_NTB_INPUT_SIZE,
244 USB_TYPE_CLASS | USB_DIR_OUT
245 | USB_RECIP_INTERFACE,
246 0, iface_no, &dwNtbInMaxSize, 4, 1000);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000247 }
248
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000249 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000250 pr_debug("Setting NTB Input Size failed\n");
251 }
252
Alexey Orishko900d4952010-11-29 23:23:28 +0000253 /* verify maximum size of transmitted NTB in bytes */
254 if ((ctx->tx_max <
255 (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) ||
256 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
257 pr_debug("Using default maximum transmit length=%d\n",
258 CDC_NCM_NTB_MAX_SIZE_TX);
259 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
260 }
261
262 /*
263 * verify that the structure alignment is:
264 * - power of two
265 * - not greater than the maximum transmit length
266 * - not less than four bytes
267 */
268 val = ctx->tx_ndp_modulus;
269
270 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
271 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
272 pr_debug("Using default alignment: 4 bytes\n");
273 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
274 }
275
276 /*
277 * verify that the payload alignment is:
278 * - power of two
279 * - not greater than the maximum transmit length
280 * - not less than four bytes
281 */
282 val = ctx->tx_modulus;
283
284 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
285 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
286 pr_debug("Using default transmit modulus: 4 bytes\n");
287 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
288 }
289
290 /* verify the payload remainder */
291 if (ctx->tx_remainder >= ctx->tx_modulus) {
292 pr_debug("Using default transmit remainder: 0 bytes\n");
293 ctx->tx_remainder = 0;
294 }
295
296 /* adjust TX-remainder according to NCM specification. */
297 ctx->tx_remainder = ((ctx->tx_remainder - ETH_HLEN) &
298 (ctx->tx_modulus - 1));
299
300 /* additional configuration */
301
302 /* set CRC Mode */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000303 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000304 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
305 USB_CDC_SET_CRC_MODE,
306 USB_TYPE_CLASS | USB_DIR_OUT
307 | USB_RECIP_INTERFACE,
308 USB_CDC_NCM_CRC_NOT_APPENDED,
309 iface_no, NULL, 0, 1000);
310 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000311 pr_debug("Setting CRC mode off failed\n");
312 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000313
Alexey Orishko84e77a82011-02-07 09:45:10 +0000314 /* set NTB format, if both formats are supported */
315 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000316 err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
317 USB_CDC_SET_NTB_FORMAT, USB_TYPE_CLASS
318 | USB_DIR_OUT | USB_RECIP_INTERFACE,
319 USB_CDC_NCM_NTB16_FORMAT,
320 iface_no, NULL, 0, 1000);
321 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000322 pr_debug("Setting NTB format to 16-bit failed\n");
323 }
324
325 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
Alexey Orishko900d4952010-11-29 23:23:28 +0000326
327 /* set Max Datagram Size (MTU) */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000328 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
329 __le16 max_datagram_size;
330 u16 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000331 err = usb_control_msg(ctx->udev, usb_rcvctrlpipe(ctx->udev, 0),
332 USB_CDC_GET_MAX_DATAGRAM_SIZE,
333 USB_TYPE_CLASS | USB_DIR_IN
334 | USB_RECIP_INTERFACE,
335 0, iface_no, &max_datagram_size,
336 2, 1000);
337 if (err < 0) {
Alexey Orishko84e77a82011-02-07 09:45:10 +0000338 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
339 CDC_NCM_MIN_DATAGRAM_SIZE);
340 } else {
341 ctx->max_datagram_size = le16_to_cpu(max_datagram_size);
342 /* Check Eth descriptor value */
343 if (eth_max_sz < CDC_NCM_MAX_DATAGRAM_SIZE) {
344 if (ctx->max_datagram_size > eth_max_sz)
345 ctx->max_datagram_size = eth_max_sz;
346 } else {
347 if (ctx->max_datagram_size >
348 CDC_NCM_MAX_DATAGRAM_SIZE)
349 ctx->max_datagram_size =
350 CDC_NCM_MAX_DATAGRAM_SIZE;
351 }
352
353 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
354 ctx->max_datagram_size =
355 CDC_NCM_MIN_DATAGRAM_SIZE;
356
357 /* if value changed, update device */
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000358 err = usb_control_msg(ctx->udev,
359 usb_sndctrlpipe(ctx->udev, 0),
360 USB_CDC_SET_MAX_DATAGRAM_SIZE,
361 USB_TYPE_CLASS | USB_DIR_OUT
362 | USB_RECIP_INTERFACE,
363 0,
364 iface_no, &max_datagram_size,
365 2, 1000);
366 if (err < 0)
Alexey Orishko84e77a82011-02-07 09:45:10 +0000367 pr_debug("SET_MAX_DATAGRAM_SIZE failed\n");
368 }
369
Alexey Orishko900d4952010-11-29 23:23:28 +0000370 }
371
372 if (ctx->netdev->mtu != (ctx->max_datagram_size - ETH_HLEN))
373 ctx->netdev->mtu = ctx->max_datagram_size - ETH_HLEN;
374
375 return 0;
376}
377
378static void
379cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
380{
381 struct usb_host_endpoint *e;
382 u8 ep;
383
384 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
385
386 e = intf->cur_altsetting->endpoint + ep;
387 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
388 case USB_ENDPOINT_XFER_INT:
389 if (usb_endpoint_dir_in(&e->desc)) {
390 if (ctx->status_ep == NULL)
391 ctx->status_ep = e;
392 }
393 break;
394
395 case USB_ENDPOINT_XFER_BULK:
396 if (usb_endpoint_dir_in(&e->desc)) {
397 if (ctx->in_ep == NULL)
398 ctx->in_ep = e;
399 } else {
400 if (ctx->out_ep == NULL)
401 ctx->out_ep = e;
402 }
403 break;
404
405 default:
406 break;
407 }
408 }
409}
410
411static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
412{
413 if (ctx == NULL)
414 return;
415
416 del_timer_sync(&ctx->tx_timer);
417
Alexey Orishko900d4952010-11-29 23:23:28 +0000418 if (ctx->tx_rem_skb != NULL) {
419 dev_kfree_skb_any(ctx->tx_rem_skb);
420 ctx->tx_rem_skb = NULL;
421 }
422
423 if (ctx->tx_curr_skb != NULL) {
424 dev_kfree_skb_any(ctx->tx_curr_skb);
425 ctx->tx_curr_skb = NULL;
426 }
427
428 kfree(ctx);
429}
430
431static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
432{
433 struct cdc_ncm_ctx *ctx;
434 struct usb_driver *driver;
435 u8 *buf;
436 int len;
437 int temp;
438 u8 iface_no;
439
440 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
441 if (ctx == NULL)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000442 return -ENODEV;
Alexey Orishko900d4952010-11-29 23:23:28 +0000443
444 memset(ctx, 0, sizeof(*ctx));
445
446 init_timer(&ctx->tx_timer);
447 spin_lock_init(&ctx->mtx);
448 ctx->netdev = dev->net;
449
450 /* store ctx pointer in device data field */
451 dev->data[0] = (unsigned long)ctx;
452
453 /* get some pointers */
454 driver = driver_of(intf);
455 buf = intf->cur_altsetting->extra;
456 len = intf->cur_altsetting->extralen;
457
458 ctx->udev = dev->udev;
459 ctx->intf = intf;
460
461 /* parse through descriptors associated with control interface */
462 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
463
464 if (buf[1] != USB_DT_CS_INTERFACE)
465 goto advance;
466
467 switch (buf[2]) {
468 case USB_CDC_UNION_TYPE:
469 if (buf[0] < sizeof(*(ctx->union_desc)))
470 break;
471
472 ctx->union_desc =
473 (const struct usb_cdc_union_desc *)buf;
474
475 ctx->control = usb_ifnum_to_if(dev->udev,
476 ctx->union_desc->bMasterInterface0);
477 ctx->data = usb_ifnum_to_if(dev->udev,
478 ctx->union_desc->bSlaveInterface0);
479 break;
480
481 case USB_CDC_ETHERNET_TYPE:
482 if (buf[0] < sizeof(*(ctx->ether_desc)))
483 break;
484
485 ctx->ether_desc =
486 (const struct usb_cdc_ether_desc *)buf;
Alexey Orishko900d4952010-11-29 23:23:28 +0000487 dev->hard_mtu =
488 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
489
Alexey Orishko84e77a82011-02-07 09:45:10 +0000490 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
491 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
492 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
493 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
Alexey Orishko900d4952010-11-29 23:23:28 +0000494 break;
495
496 case USB_CDC_NCM_TYPE:
497 if (buf[0] < sizeof(*(ctx->func_desc)))
498 break;
499
500 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
501 break;
502
503 default:
504 break;
505 }
506advance:
507 /* advance to next descriptor */
508 temp = buf[0];
509 buf += temp;
510 len -= temp;
511 }
512
513 /* check if we got everything */
514 if ((ctx->control == NULL) || (ctx->data == NULL) ||
Alexey Orishko19694ac2011-05-24 05:26:13 +0000515 (ctx->ether_desc == NULL) || (ctx->control != intf))
Alexey Orishko900d4952010-11-29 23:23:28 +0000516 goto error;
517
518 /* claim interfaces, if any */
Alexey Orishko19694ac2011-05-24 05:26:13 +0000519 temp = usb_driver_claim_interface(driver, ctx->data, dev);
520 if (temp)
521 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +0000522
523 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
524
525 /* reset data interface */
526 temp = usb_set_interface(dev->udev, iface_no, 0);
527 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000528 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000529
530 /* initialize data interface */
531 if (cdc_ncm_setup(ctx))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000532 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000533
534 /* configure data interface */
535 temp = usb_set_interface(dev->udev, iface_no, 1);
536 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000537 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000538
539 cdc_ncm_find_endpoints(ctx, ctx->data);
540 cdc_ncm_find_endpoints(ctx, ctx->control);
541
542 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
543 (ctx->status_ep == NULL))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000544 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000545
546 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
547
548 usb_set_intfdata(ctx->data, dev);
549 usb_set_intfdata(ctx->control, dev);
550 usb_set_intfdata(ctx->intf, dev);
551
552 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
553 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000554 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000555
556 dev_info(&dev->udev->dev, "MAC-Address: "
557 "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
558 dev->net->dev_addr[0], dev->net->dev_addr[1],
559 dev->net->dev_addr[2], dev->net->dev_addr[3],
560 dev->net->dev_addr[4], dev->net->dev_addr[5]);
561
562 dev->in = usb_rcvbulkpipe(dev->udev,
563 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
564 dev->out = usb_sndbulkpipe(dev->udev,
565 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
566 dev->status = ctx->status_ep;
567 dev->rx_urb_size = ctx->rx_max;
568
569 /*
570 * We should get an event when network connection is "connected" or
571 * "disconnected". Set network connection in "disconnected" state
572 * (carrier is OFF) during attach, so the IP network stack does not
573 * start IPv6 negotiation and more.
574 */
575 netif_carrier_off(dev->net);
576 ctx->tx_speed = ctx->rx_speed = 0;
577 return 0;
578
Alexey Orishko19694ac2011-05-24 05:26:13 +0000579error2:
580 usb_set_intfdata(ctx->control, NULL);
581 usb_set_intfdata(ctx->data, NULL);
582 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko900d4952010-11-29 23:23:28 +0000583error:
584 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
585 dev->data[0] = 0;
Alexey Orishko19694ac2011-05-24 05:26:13 +0000586 dev_info(&dev->udev->dev, "bind() failure\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000587 return -ENODEV;
588}
589
590static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
591{
592 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko19694ac2011-05-24 05:26:13 +0000593 struct usb_driver *driver = driver_of(intf);
Alexey Orishko900d4952010-11-29 23:23:28 +0000594
595 if (ctx == NULL)
596 return; /* no setup */
597
Alexey Orishko19694ac2011-05-24 05:26:13 +0000598 /* disconnect master --> disconnect slave */
599 if (intf == ctx->control && ctx->data) {
600 usb_set_intfdata(ctx->data, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000601 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000602 ctx->data = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000603
Alexey Orishko19694ac2011-05-24 05:26:13 +0000604 } else if (intf == ctx->data && ctx->control) {
605 usb_set_intfdata(ctx->control, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000606 usb_driver_release_interface(driver, ctx->control);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000607 ctx->control = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000608 }
609
Alexey Orishko19694ac2011-05-24 05:26:13 +0000610 usb_set_intfdata(ctx->intf, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000611 cdc_ncm_free(ctx);
612}
613
614static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
615{
616 if (first >= max)
617 return;
618 if (first >= end)
619 return;
620 if (end > max)
621 end = max;
622 memset(ptr + first, 0, end - first);
623}
624
625static struct sk_buff *
626cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
627{
628 struct sk_buff *skb_out;
629 u32 rem;
630 u32 offset;
631 u32 last_offset;
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000632 u16 n = 0, index;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000633 u8 ready2send = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000634
635 /* if there is a remaining skb, it gets priority */
636 if (skb != NULL)
637 swap(skb, ctx->tx_rem_skb);
638 else
Alexey Orishko84e77a82011-02-07 09:45:10 +0000639 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000640
641 /*
642 * +----------------+
643 * | skb_out |
644 * +----------------+
645 * ^ offset
646 * ^ last_offset
647 */
648
649 /* check if we are resuming an OUT skb */
650 if (ctx->tx_curr_skb != NULL) {
651 /* pop variables */
652 skb_out = ctx->tx_curr_skb;
653 offset = ctx->tx_curr_offset;
654 last_offset = ctx->tx_curr_last_offset;
655 n = ctx->tx_curr_frame_num;
656
657 } else {
658 /* reset variables */
Alexey Orishko6c604082011-05-06 03:01:30 +0000659 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
Alexey Orishko900d4952010-11-29 23:23:28 +0000660 if (skb_out == NULL) {
661 if (skb != NULL) {
662 dev_kfree_skb_any(skb);
663 ctx->netdev->stats.tx_dropped++;
664 }
665 goto exit_no_skb;
666 }
667
668 /* make room for NTH and NDP */
669 offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
670 ctx->tx_ndp_modulus) +
671 sizeof(struct usb_cdc_ncm_ndp16) +
672 (ctx->tx_max_datagrams + 1) *
673 sizeof(struct usb_cdc_ncm_dpe16);
674
675 /* store last valid offset before alignment */
676 last_offset = offset;
677 /* align first Datagram offset correctly */
678 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
679 /* zero buffer till the first IP datagram */
680 cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
681 n = 0;
682 ctx->tx_curr_frame_num = 0;
683 }
684
685 for (; n < ctx->tx_max_datagrams; n++) {
686 /* check if end of transmit buffer is reached */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000687 if (offset >= ctx->tx_max) {
688 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000689 break;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000690 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000691 /* compute maximum buffer size */
692 rem = ctx->tx_max - offset;
693
694 if (skb == NULL) {
695 skb = ctx->tx_rem_skb;
696 ctx->tx_rem_skb = NULL;
697
698 /* check for end of skb */
699 if (skb == NULL)
700 break;
701 }
702
703 if (skb->len > rem) {
704 if (n == 0) {
705 /* won't fit, MTU problem? */
706 dev_kfree_skb_any(skb);
707 skb = NULL;
708 ctx->netdev->stats.tx_dropped++;
709 } else {
710 /* no room for skb - store for later */
711 if (ctx->tx_rem_skb != NULL) {
712 dev_kfree_skb_any(ctx->tx_rem_skb);
713 ctx->netdev->stats.tx_dropped++;
714 }
715 ctx->tx_rem_skb = skb;
716 skb = NULL;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000717 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000718 }
719 break;
720 }
721
722 memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
723
724 ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
725 ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
726
727 /* update offset */
728 offset += skb->len;
729
730 /* store last valid offset before alignment */
731 last_offset = offset;
732
733 /* align offset correctly */
734 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
735
736 /* zero padding */
737 cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
738 ctx->tx_max);
739 dev_kfree_skb_any(skb);
740 skb = NULL;
741 }
742
743 /* free up any dangling skb */
744 if (skb != NULL) {
745 dev_kfree_skb_any(skb);
746 skb = NULL;
747 ctx->netdev->stats.tx_dropped++;
748 }
749
750 ctx->tx_curr_frame_num = n;
751
752 if (n == 0) {
753 /* wait for more frames */
754 /* push variables */
755 ctx->tx_curr_skb = skb_out;
756 ctx->tx_curr_offset = offset;
757 ctx->tx_curr_last_offset = last_offset;
758 goto exit_no_skb;
759
Alexey Orishko84e77a82011-02-07 09:45:10 +0000760 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000761 /* wait for more frames */
762 /* push variables */
763 ctx->tx_curr_skb = skb_out;
764 ctx->tx_curr_offset = offset;
765 ctx->tx_curr_last_offset = last_offset;
766 /* set the pending count */
767 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
768 ctx->tx_timer_pending = 2;
769 goto exit_no_skb;
770
771 } else {
772 /* frame goes out */
773 /* variables will be reset at next call */
774 }
775
776 /* check for overflow */
777 if (last_offset > ctx->tx_max)
778 last_offset = ctx->tx_max;
779
780 /* revert offset */
781 offset = last_offset;
782
783 /*
784 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
785 * we send buffers as it is. If we get more data, it would be more
786 * efficient for USB HS mobile device with DMA engine to receive a full
787 * size NTB, than canceling DMA transfer and receiving a short packet.
788 */
789 if (offset > CDC_NCM_MIN_TX_PKT)
790 offset = ctx->tx_max;
791
792 /* final zero padding */
793 cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
794
795 /* store last offset */
796 last_offset = offset;
797
Alexey Orishko6c604082011-05-06 03:01:30 +0000798 if (((last_offset < ctx->tx_max) && ((last_offset %
799 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) ||
800 (((last_offset == ctx->tx_max) && ((ctx->tx_max %
801 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) &&
802 (ctx->tx_max < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)))) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000803 /* force short packet */
804 *(((u8 *)skb_out->data) + last_offset) = 0;
805 last_offset++;
806 }
807
808 /* zero the rest of the DPEs plus the last NULL entry */
809 for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
810 ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
811 ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
812 }
813
814 /* fill out 16-bit NTB header */
815 ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
816 ctx->tx_ncm.nth16.wHeaderLength =
817 cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
818 ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
819 ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000820 index = ALIGN(sizeof(struct usb_cdc_ncm_nth16), ctx->tx_ndp_modulus);
821 ctx->tx_ncm.nth16.wNdpIndex = cpu_to_le16(index);
Alexey Orishko900d4952010-11-29 23:23:28 +0000822
823 memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
824 ctx->tx_seq++;
825
826 /* fill out 16-bit NDP table */
827 ctx->tx_ncm.ndp16.dwSignature =
828 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
829 rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
830 sizeof(struct usb_cdc_ncm_dpe16));
831 ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000832 ctx->tx_ncm.ndp16.wNextNdpIndex = 0; /* reserved */
Alexey Orishko900d4952010-11-29 23:23:28 +0000833
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000834 memcpy(((u8 *)skb_out->data) + index,
Alexey Orishko900d4952010-11-29 23:23:28 +0000835 &(ctx->tx_ncm.ndp16),
836 sizeof(ctx->tx_ncm.ndp16));
837
Giuseppe Scrivanof85eb4c2011-08-03 22:10:29 +0000838 memcpy(((u8 *)skb_out->data) + index + sizeof(ctx->tx_ncm.ndp16),
Alexey Orishko900d4952010-11-29 23:23:28 +0000839 &(ctx->tx_ncm.dpe16),
840 (ctx->tx_curr_frame_num + 1) *
841 sizeof(struct usb_cdc_ncm_dpe16));
842
843 /* set frame length */
844 skb_put(skb_out, last_offset);
845
846 /* return skb */
847 ctx->tx_curr_skb = NULL;
848 return skb_out;
849
850exit_no_skb:
851 return NULL;
852}
853
854static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
855{
856 /* start timer, if not already started */
857 if (timer_pending(&ctx->tx_timer) == 0) {
858 ctx->tx_timer.function = &cdc_ncm_tx_timeout;
859 ctx->tx_timer.data = (unsigned long)ctx;
860 ctx->tx_timer.expires = jiffies + ((HZ + 999) / 1000);
861 add_timer(&ctx->tx_timer);
862 }
863}
864
865static void cdc_ncm_tx_timeout(unsigned long arg)
866{
867 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)arg;
868 u8 restart;
869
870 spin_lock(&ctx->mtx);
871 if (ctx->tx_timer_pending != 0) {
872 ctx->tx_timer_pending--;
873 restart = 1;
Alexey Orishkof742aa82011-01-17 07:07:25 +0000874 } else {
Alexey Orishko900d4952010-11-29 23:23:28 +0000875 restart = 0;
Alexey Orishkof742aa82011-01-17 07:07:25 +0000876 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000877
878 spin_unlock(&ctx->mtx);
879
Alexey Orishkof742aa82011-01-17 07:07:25 +0000880 if (restart) {
881 spin_lock(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000882 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000883 spin_unlock(&ctx->mtx);
884 } else if (ctx->netdev != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000885 usbnet_start_xmit(NULL, ctx->netdev);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000886 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000887}
888
889static struct sk_buff *
890cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
891{
892 struct sk_buff *skb_out;
893 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
894 u8 need_timer = 0;
895
896 /*
897 * The Ethernet API we are using does not support transmitting
898 * multiple Ethernet frames in a single call. This driver will
899 * accumulate multiple Ethernet frames and send out a larger
900 * USB frame when the USB buffer is full or when a single jiffies
901 * timeout happens.
902 */
903 if (ctx == NULL)
904 goto error;
905
906 spin_lock(&ctx->mtx);
907 skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
908 if (ctx->tx_curr_skb != NULL)
909 need_timer = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000910
911 /* Start timer, if there is a remaining skb */
912 if (need_timer)
913 cdc_ncm_tx_timeout_start(ctx);
914
915 if (skb_out)
916 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
Alexey Orishkof742aa82011-01-17 07:07:25 +0000917
918 spin_unlock(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000919 return skb_out;
920
921error:
922 if (skb != NULL)
923 dev_kfree_skb_any(skb);
924
925 return NULL;
926}
927
928static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
929{
930 struct sk_buff *skb;
931 struct cdc_ncm_ctx *ctx;
932 int sumlen;
933 int actlen;
934 int temp;
935 int nframes;
936 int x;
937 int offset;
938
939 ctx = (struct cdc_ncm_ctx *)dev->data[0];
940 if (ctx == NULL)
941 goto error;
942
943 actlen = skb_in->len;
944 sumlen = CDC_NCM_NTB_MAX_SIZE_RX;
945
946 if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) {
947 pr_debug("frame too short\n");
948 goto error;
949 }
950
951 memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data),
952 sizeof(ctx->rx_ncm.nth16));
953
954 if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) !=
955 USB_CDC_NCM_NTH16_SIGN) {
956 pr_debug("invalid NTH16 signature <%u>\n",
957 le32_to_cpu(ctx->rx_ncm.nth16.dwSignature));
958 goto error;
959 }
960
961 temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength);
962 if (temp > sumlen) {
963 pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen);
964 goto error;
965 }
966
Alexey Orishko84e77a82011-02-07 09:45:10 +0000967 temp = le16_to_cpu(ctx->rx_ncm.nth16.wNdpIndex);
Alexey Orishko900d4952010-11-29 23:23:28 +0000968 if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) {
969 pr_debug("invalid DPT16 index\n");
970 goto error;
971 }
972
973 memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp,
974 sizeof(ctx->rx_ncm.ndp16));
975
976 if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) !=
977 USB_CDC_NCM_NDP16_NOCRC_SIGN) {
978 pr_debug("invalid DPT16 signature <%u>\n",
979 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
980 goto error;
981 }
982
983 if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) <
984 USB_CDC_NCM_NDP16_LENGTH_MIN) {
985 pr_debug("invalid DPT16 length <%u>\n",
986 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
987 goto error;
988 }
989
990 nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) -
991 sizeof(struct usb_cdc_ncm_ndp16)) /
992 sizeof(struct usb_cdc_ncm_dpe16));
993 nframes--; /* we process NDP entries except for the last one */
994
995 pr_debug("nframes = %u\n", nframes);
996
997 temp += sizeof(ctx->rx_ncm.ndp16);
998
999 if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) {
1000 pr_debug("Invalid nframes = %d\n", nframes);
1001 goto error;
1002 }
1003
1004 if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) {
1005 pr_debug("Truncating number of frames from %u to %u\n",
1006 nframes, CDC_NCM_DPT_DATAGRAMS_MAX);
1007 nframes = CDC_NCM_DPT_DATAGRAMS_MAX;
1008 }
1009
1010 memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp,
1011 nframes * (sizeof(struct usb_cdc_ncm_dpe16)));
1012
1013 for (x = 0; x < nframes; x++) {
1014 offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex);
1015 temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength);
1016
1017 /*
1018 * CDC NCM ch. 3.7
1019 * All entries after first NULL entry are to be ignored
1020 */
1021 if ((offset == 0) || (temp == 0)) {
1022 if (!x)
1023 goto error; /* empty NTB */
1024 break;
1025 }
1026
1027 /* sanity checking */
1028 if (((offset + temp) > actlen) ||
1029 (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) {
1030 pr_debug("invalid frame detected (ignored)"
Alexey Orishkof742aa82011-01-17 07:07:25 +00001031 "offset[%u]=%u, length=%u, skb=%p\n",
1032 x, offset, temp, skb_in);
Alexey Orishko900d4952010-11-29 23:23:28 +00001033 if (!x)
1034 goto error;
1035 break;
1036
1037 } else {
1038 skb = skb_clone(skb_in, GFP_ATOMIC);
Jesper Juhl9e567902011-01-13 11:40:11 +00001039 if (!skb)
1040 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +00001041 skb->len = temp;
1042 skb->data = ((u8 *)skb_in->data) + offset;
1043 skb_set_tail_pointer(skb, temp);
1044 usbnet_skb_return(dev, skb);
1045 }
1046 }
1047 return 1;
1048error:
1049 return 0;
1050}
1051
1052static void
1053cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001054 struct usb_cdc_speed_change *data)
Alexey Orishko900d4952010-11-29 23:23:28 +00001055{
Alexey Orishko84e77a82011-02-07 09:45:10 +00001056 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1057 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
Alexey Orishko900d4952010-11-29 23:23:28 +00001058
1059 /*
1060 * Currently the USB-NET API does not support reporting the actual
1061 * device speed. Do print it instead.
1062 */
1063 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1064 ctx->tx_speed = tx_speed;
1065 ctx->rx_speed = rx_speed;
1066
1067 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1068 printk(KERN_INFO KBUILD_MODNAME
1069 ": %s: %u mbit/s downlink "
1070 "%u mbit/s uplink\n",
1071 ctx->netdev->name,
1072 (unsigned int)(rx_speed / 1000000U),
1073 (unsigned int)(tx_speed / 1000000U));
1074 } else {
1075 printk(KERN_INFO KBUILD_MODNAME
1076 ": %s: %u kbit/s downlink "
1077 "%u kbit/s uplink\n",
1078 ctx->netdev->name,
1079 (unsigned int)(rx_speed / 1000U),
1080 (unsigned int)(tx_speed / 1000U));
1081 }
1082 }
1083}
1084
1085static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1086{
1087 struct cdc_ncm_ctx *ctx;
1088 struct usb_cdc_notification *event;
1089
1090 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1091
1092 if (urb->actual_length < sizeof(*event))
1093 return;
1094
1095 /* test for split data in 8-byte chunks */
1096 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1097 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001098 (struct usb_cdc_speed_change *)urb->transfer_buffer);
Alexey Orishko900d4952010-11-29 23:23:28 +00001099 return;
1100 }
1101
1102 event = urb->transfer_buffer;
1103
1104 switch (event->bNotificationType) {
1105 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1106 /*
1107 * According to the CDC NCM specification ch.7.1
1108 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1109 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1110 */
1111 ctx->connected = event->wValue;
1112
1113 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1114 " %sconnected\n",
1115 ctx->netdev->name, ctx->connected ? "" : "dis");
1116
1117 if (ctx->connected)
1118 netif_carrier_on(dev->net);
1119 else {
1120 netif_carrier_off(dev->net);
1121 ctx->tx_speed = ctx->rx_speed = 0;
1122 }
1123 break;
1124
1125 case USB_CDC_NOTIFY_SPEED_CHANGE:
Alexey Orishko84e77a82011-02-07 09:45:10 +00001126 if (urb->actual_length < (sizeof(*event) +
1127 sizeof(struct usb_cdc_speed_change)))
Alexey Orishko900d4952010-11-29 23:23:28 +00001128 set_bit(EVENT_STS_SPLIT, &dev->flags);
1129 else
1130 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001131 (struct usb_cdc_speed_change *) &event[1]);
Alexey Orishko900d4952010-11-29 23:23:28 +00001132 break;
1133
1134 default:
1135 dev_err(&dev->udev->dev, "NCM: unexpected "
1136 "notification 0x%02x!\n", event->bNotificationType);
1137 break;
1138 }
1139}
1140
1141static int cdc_ncm_check_connect(struct usbnet *dev)
1142{
1143 struct cdc_ncm_ctx *ctx;
1144
1145 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1146 if (ctx == NULL)
1147 return 1; /* disconnected */
1148
1149 return !ctx->connected;
1150}
1151
1152static int
1153cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1154{
1155 return usbnet_probe(udev, prod);
1156}
1157
1158static void cdc_ncm_disconnect(struct usb_interface *intf)
1159{
1160 struct usbnet *dev = usb_get_intfdata(intf);
1161
1162 if (dev == NULL)
1163 return; /* already disconnected */
1164
1165 usbnet_disconnect(intf);
1166}
1167
1168static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1169{
1170 dev->intf->needs_remote_wakeup = status;
1171 return 0;
1172}
1173
1174static const struct driver_info cdc_ncm_info = {
1175 .description = "CDC NCM",
Arnd Bergmannc2613442011-04-01 20:12:02 -07001176 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
Alexey Orishko900d4952010-11-29 23:23:28 +00001177 .bind = cdc_ncm_bind,
1178 .unbind = cdc_ncm_unbind,
1179 .check_connect = cdc_ncm_check_connect,
1180 .manage_power = cdc_ncm_manage_power,
1181 .status = cdc_ncm_status,
1182 .rx_fixup = cdc_ncm_rx_fixup,
1183 .tx_fixup = cdc_ncm_tx_fixup,
1184};
1185
1186static struct usb_driver cdc_ncm_driver = {
1187 .name = "cdc_ncm",
1188 .id_table = cdc_devs,
1189 .probe = cdc_ncm_probe,
1190 .disconnect = cdc_ncm_disconnect,
1191 .suspend = usbnet_suspend,
1192 .resume = usbnet_resume,
Stefan Metzmacher85e3c652011-06-01 02:01:41 +00001193 .reset_resume = usbnet_resume,
Alexey Orishko900d4952010-11-29 23:23:28 +00001194 .supports_autosuspend = 1,
1195};
1196
1197static struct ethtool_ops cdc_ncm_ethtool_ops = {
1198 .get_drvinfo = cdc_ncm_get_drvinfo,
1199 .get_link = usbnet_get_link,
1200 .get_msglevel = usbnet_get_msglevel,
1201 .set_msglevel = usbnet_set_msglevel,
1202 .get_settings = usbnet_get_settings,
1203 .set_settings = usbnet_set_settings,
1204 .nway_reset = usbnet_nway_reset,
1205};
1206
1207static int __init cdc_ncm_init(void)
1208{
1209 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
1210 return usb_register(&cdc_ncm_driver);
1211}
1212
1213module_init(cdc_ncm_init);
1214
1215static void __exit cdc_ncm_exit(void)
1216{
1217 usb_deregister(&cdc_ncm_driver);
1218}
1219
1220module_exit(cdc_ncm_exit);
1221
1222MODULE_AUTHOR("Hans Petter Selasky");
1223MODULE_DESCRIPTION("USB CDC NCM host driver");
1224MODULE_LICENSE("Dual BSD/GPL");