blob: f33ca6aa29e9cb95a8277891298317ca810cefa7 [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
Stefan Metzmacher85e3c652011-06-01 02:01:41 +000057#define DRIVER_VERSION "01-June-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
167static int
168cdc_ncm_do_request(struct cdc_ncm_ctx *ctx, struct usb_cdc_notification *req,
169 void *data, u16 flags, u16 *actlen, u16 timeout)
170{
171 int err;
172
173 err = usb_control_msg(ctx->udev, (req->bmRequestType & USB_DIR_IN) ?
174 usb_rcvctrlpipe(ctx->udev, 0) :
175 usb_sndctrlpipe(ctx->udev, 0),
176 req->bNotificationType, req->bmRequestType,
177 req->wValue,
178 req->wIndex, data,
179 req->wLength, timeout);
180
181 if (err < 0) {
182 if (actlen)
183 *actlen = 0;
184 return err;
185 }
186
187 if (actlen)
188 *actlen = err;
189
190 return 0;
191}
192
193static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
194{
195 struct usb_cdc_notification req;
196 u32 val;
Alexey Orishko900d4952010-11-29 23:23:28 +0000197 u8 flags;
198 u8 iface_no;
199 int err;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000200 u16 ntb_fmt_supported;
Alexey Orishko900d4952010-11-29 23:23:28 +0000201
202 iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
203
204 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE;
205 req.bNotificationType = USB_CDC_GET_NTB_PARAMETERS;
206 req.wValue = 0;
207 req.wIndex = cpu_to_le16(iface_no);
208 req.wLength = cpu_to_le16(sizeof(ctx->ncm_parm));
209
210 err = cdc_ncm_do_request(ctx, &req, &ctx->ncm_parm, 0, NULL, 1000);
211 if (err) {
212 pr_debug("failed GET_NTB_PARAMETERS\n");
213 return 1;
214 }
215
216 /* read correct set of parameters according to device mode */
217 ctx->rx_max = le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize);
218 ctx->tx_max = le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize);
219 ctx->tx_remainder = le16_to_cpu(ctx->ncm_parm.wNdpOutPayloadRemainder);
220 ctx->tx_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutDivisor);
221 ctx->tx_ndp_modulus = le16_to_cpu(ctx->ncm_parm.wNdpOutAlignment);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000222 /* devices prior to NCM Errata shall set this field to zero */
223 ctx->tx_max_datagrams = le16_to_cpu(ctx->ncm_parm.wNtbOutMaxDatagrams);
224 ntb_fmt_supported = le16_to_cpu(ctx->ncm_parm.bmNtbFormatsSupported);
Alexey Orishko900d4952010-11-29 23:23:28 +0000225
226 if (ctx->func_desc != NULL)
227 flags = ctx->func_desc->bmNetworkCapabilities;
228 else
229 flags = 0;
230
231 pr_debug("dwNtbInMaxSize=%u dwNtbOutMaxSize=%u "
232 "wNdpOutPayloadRemainder=%u wNdpOutDivisor=%u "
Alexey Orishko84e77a82011-02-07 09:45:10 +0000233 "wNdpOutAlignment=%u wNtbOutMaxDatagrams=%u flags=0x%x\n",
Alexey Orishko900d4952010-11-29 23:23:28 +0000234 ctx->rx_max, ctx->tx_max, ctx->tx_remainder, ctx->tx_modulus,
Alexey Orishko84e77a82011-02-07 09:45:10 +0000235 ctx->tx_ndp_modulus, ctx->tx_max_datagrams, flags);
Alexey Orishko900d4952010-11-29 23:23:28 +0000236
Alexey Orishko84e77a82011-02-07 09:45:10 +0000237 /* max count of tx datagrams */
238 if ((ctx->tx_max_datagrams == 0) ||
239 (ctx->tx_max_datagrams > CDC_NCM_DPT_DATAGRAMS_MAX))
240 ctx->tx_max_datagrams = CDC_NCM_DPT_DATAGRAMS_MAX;
Alexey Orishko900d4952010-11-29 23:23:28 +0000241
242 /* verify maximum size of received NTB in bytes */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000243 if (ctx->rx_max < USB_CDC_NCM_NTB_MIN_IN_SIZE) {
244 pr_debug("Using min receive length=%d\n",
245 USB_CDC_NCM_NTB_MIN_IN_SIZE);
246 ctx->rx_max = USB_CDC_NCM_NTB_MIN_IN_SIZE;
247 }
248
249 if (ctx->rx_max > CDC_NCM_NTB_MAX_SIZE_RX) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000250 pr_debug("Using default maximum receive length=%d\n",
251 CDC_NCM_NTB_MAX_SIZE_RX);
252 ctx->rx_max = CDC_NCM_NTB_MAX_SIZE_RX;
253 }
254
Alexey Orishko84e77a82011-02-07 09:45:10 +0000255 /* inform device about NTB input size changes */
256 if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
257 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
258 USB_RECIP_INTERFACE;
259 req.bNotificationType = USB_CDC_SET_NTB_INPUT_SIZE;
260 req.wValue = 0;
261 req.wIndex = cpu_to_le16(iface_no);
262
263 if (flags & USB_CDC_NCM_NCAP_NTB_INPUT_SIZE) {
264 struct usb_cdc_ncm_ndp_input_size ndp_in_sz;
265
266 req.wLength = 8;
267 ndp_in_sz.dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
268 ndp_in_sz.wNtbInMaxDatagrams =
269 cpu_to_le16(CDC_NCM_DPT_DATAGRAMS_MAX);
270 ndp_in_sz.wReserved = 0;
271 err = cdc_ncm_do_request(ctx, &req, &ndp_in_sz, 0, NULL,
272 1000);
273 } else {
274 __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
275
276 req.wLength = 4;
277 err = cdc_ncm_do_request(ctx, &req, &dwNtbInMaxSize, 0,
278 NULL, 1000);
279 }
280
281 if (err)
282 pr_debug("Setting NTB Input Size failed\n");
283 }
284
Alexey Orishko900d4952010-11-29 23:23:28 +0000285 /* verify maximum size of transmitted NTB in bytes */
286 if ((ctx->tx_max <
287 (CDC_NCM_MIN_HDR_SIZE + CDC_NCM_MIN_DATAGRAM_SIZE)) ||
288 (ctx->tx_max > CDC_NCM_NTB_MAX_SIZE_TX)) {
289 pr_debug("Using default maximum transmit length=%d\n",
290 CDC_NCM_NTB_MAX_SIZE_TX);
291 ctx->tx_max = CDC_NCM_NTB_MAX_SIZE_TX;
292 }
293
294 /*
295 * verify that the structure alignment is:
296 * - power of two
297 * - not greater than the maximum transmit length
298 * - not less than four bytes
299 */
300 val = ctx->tx_ndp_modulus;
301
302 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
303 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
304 pr_debug("Using default alignment: 4 bytes\n");
305 ctx->tx_ndp_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
306 }
307
308 /*
309 * verify that the payload alignment is:
310 * - power of two
311 * - not greater than the maximum transmit length
312 * - not less than four bytes
313 */
314 val = ctx->tx_modulus;
315
316 if ((val < USB_CDC_NCM_NDP_ALIGN_MIN_SIZE) ||
317 (val != ((-val) & val)) || (val >= ctx->tx_max)) {
318 pr_debug("Using default transmit modulus: 4 bytes\n");
319 ctx->tx_modulus = USB_CDC_NCM_NDP_ALIGN_MIN_SIZE;
320 }
321
322 /* verify the payload remainder */
323 if (ctx->tx_remainder >= ctx->tx_modulus) {
324 pr_debug("Using default transmit remainder: 0 bytes\n");
325 ctx->tx_remainder = 0;
326 }
327
328 /* adjust TX-remainder according to NCM specification. */
329 ctx->tx_remainder = ((ctx->tx_remainder - ETH_HLEN) &
330 (ctx->tx_modulus - 1));
331
332 /* additional configuration */
333
334 /* set CRC Mode */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000335 if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
336 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
337 USB_RECIP_INTERFACE;
338 req.bNotificationType = USB_CDC_SET_CRC_MODE;
339 req.wValue = cpu_to_le16(USB_CDC_NCM_CRC_NOT_APPENDED);
340 req.wIndex = cpu_to_le16(iface_no);
341 req.wLength = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000342
Alexey Orishko84e77a82011-02-07 09:45:10 +0000343 err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
344 if (err)
345 pr_debug("Setting CRC mode off failed\n");
346 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000347
Alexey Orishko84e77a82011-02-07 09:45:10 +0000348 /* set NTB format, if both formats are supported */
349 if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
350 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
351 USB_RECIP_INTERFACE;
352 req.bNotificationType = USB_CDC_SET_NTB_FORMAT;
353 req.wValue = cpu_to_le16(USB_CDC_NCM_NTB16_FORMAT);
354 req.wIndex = cpu_to_le16(iface_no);
355 req.wLength = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000356
Alexey Orishko84e77a82011-02-07 09:45:10 +0000357 err = cdc_ncm_do_request(ctx, &req, NULL, 0, NULL, 1000);
358 if (err)
359 pr_debug("Setting NTB format to 16-bit failed\n");
360 }
361
362 ctx->max_datagram_size = CDC_NCM_MIN_DATAGRAM_SIZE;
Alexey Orishko900d4952010-11-29 23:23:28 +0000363
364 /* set Max Datagram Size (MTU) */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000365 if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
366 __le16 max_datagram_size;
367 u16 eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
Alexey Orishko900d4952010-11-29 23:23:28 +0000368
Alexey Orishko84e77a82011-02-07 09:45:10 +0000369 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_IN |
370 USB_RECIP_INTERFACE;
371 req.bNotificationType = USB_CDC_GET_MAX_DATAGRAM_SIZE;
372 req.wValue = 0;
373 req.wIndex = cpu_to_le16(iface_no);
374 req.wLength = cpu_to_le16(2);
Alexey Orishko900d4952010-11-29 23:23:28 +0000375
Alexey Orishko84e77a82011-02-07 09:45:10 +0000376 err = cdc_ncm_do_request(ctx, &req, &max_datagram_size, 0, NULL,
377 1000);
378 if (err) {
379 pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
380 CDC_NCM_MIN_DATAGRAM_SIZE);
381 } else {
382 ctx->max_datagram_size = le16_to_cpu(max_datagram_size);
383 /* Check Eth descriptor value */
384 if (eth_max_sz < CDC_NCM_MAX_DATAGRAM_SIZE) {
385 if (ctx->max_datagram_size > eth_max_sz)
386 ctx->max_datagram_size = eth_max_sz;
387 } else {
388 if (ctx->max_datagram_size >
389 CDC_NCM_MAX_DATAGRAM_SIZE)
390 ctx->max_datagram_size =
391 CDC_NCM_MAX_DATAGRAM_SIZE;
392 }
393
394 if (ctx->max_datagram_size < CDC_NCM_MIN_DATAGRAM_SIZE)
395 ctx->max_datagram_size =
396 CDC_NCM_MIN_DATAGRAM_SIZE;
397
398 /* if value changed, update device */
399 req.bmRequestType = USB_TYPE_CLASS | USB_DIR_OUT |
400 USB_RECIP_INTERFACE;
401 req.bNotificationType = USB_CDC_SET_MAX_DATAGRAM_SIZE;
402 req.wValue = 0;
403 req.wIndex = cpu_to_le16(iface_no);
404 req.wLength = 2;
405 max_datagram_size = cpu_to_le16(ctx->max_datagram_size);
406
407 err = cdc_ncm_do_request(ctx, &req, &max_datagram_size,
408 0, NULL, 1000);
409 if (err)
410 pr_debug("SET_MAX_DATAGRAM_SIZE failed\n");
411 }
412
Alexey Orishko900d4952010-11-29 23:23:28 +0000413 }
414
415 if (ctx->netdev->mtu != (ctx->max_datagram_size - ETH_HLEN))
416 ctx->netdev->mtu = ctx->max_datagram_size - ETH_HLEN;
417
418 return 0;
419}
420
421static void
422cdc_ncm_find_endpoints(struct cdc_ncm_ctx *ctx, struct usb_interface *intf)
423{
424 struct usb_host_endpoint *e;
425 u8 ep;
426
427 for (ep = 0; ep < intf->cur_altsetting->desc.bNumEndpoints; ep++) {
428
429 e = intf->cur_altsetting->endpoint + ep;
430 switch (e->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
431 case USB_ENDPOINT_XFER_INT:
432 if (usb_endpoint_dir_in(&e->desc)) {
433 if (ctx->status_ep == NULL)
434 ctx->status_ep = e;
435 }
436 break;
437
438 case USB_ENDPOINT_XFER_BULK:
439 if (usb_endpoint_dir_in(&e->desc)) {
440 if (ctx->in_ep == NULL)
441 ctx->in_ep = e;
442 } else {
443 if (ctx->out_ep == NULL)
444 ctx->out_ep = e;
445 }
446 break;
447
448 default:
449 break;
450 }
451 }
452}
453
454static void cdc_ncm_free(struct cdc_ncm_ctx *ctx)
455{
456 if (ctx == NULL)
457 return;
458
459 del_timer_sync(&ctx->tx_timer);
460
Alexey Orishko900d4952010-11-29 23:23:28 +0000461 if (ctx->tx_rem_skb != NULL) {
462 dev_kfree_skb_any(ctx->tx_rem_skb);
463 ctx->tx_rem_skb = NULL;
464 }
465
466 if (ctx->tx_curr_skb != NULL) {
467 dev_kfree_skb_any(ctx->tx_curr_skb);
468 ctx->tx_curr_skb = NULL;
469 }
470
471 kfree(ctx);
472}
473
474static int cdc_ncm_bind(struct usbnet *dev, struct usb_interface *intf)
475{
476 struct cdc_ncm_ctx *ctx;
477 struct usb_driver *driver;
478 u8 *buf;
479 int len;
480 int temp;
481 u8 iface_no;
482
483 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
484 if (ctx == NULL)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000485 return -ENODEV;
Alexey Orishko900d4952010-11-29 23:23:28 +0000486
487 memset(ctx, 0, sizeof(*ctx));
488
489 init_timer(&ctx->tx_timer);
490 spin_lock_init(&ctx->mtx);
491 ctx->netdev = dev->net;
492
493 /* store ctx pointer in device data field */
494 dev->data[0] = (unsigned long)ctx;
495
496 /* get some pointers */
497 driver = driver_of(intf);
498 buf = intf->cur_altsetting->extra;
499 len = intf->cur_altsetting->extralen;
500
501 ctx->udev = dev->udev;
502 ctx->intf = intf;
503
504 /* parse through descriptors associated with control interface */
505 while ((len > 0) && (buf[0] > 2) && (buf[0] <= len)) {
506
507 if (buf[1] != USB_DT_CS_INTERFACE)
508 goto advance;
509
510 switch (buf[2]) {
511 case USB_CDC_UNION_TYPE:
512 if (buf[0] < sizeof(*(ctx->union_desc)))
513 break;
514
515 ctx->union_desc =
516 (const struct usb_cdc_union_desc *)buf;
517
518 ctx->control = usb_ifnum_to_if(dev->udev,
519 ctx->union_desc->bMasterInterface0);
520 ctx->data = usb_ifnum_to_if(dev->udev,
521 ctx->union_desc->bSlaveInterface0);
522 break;
523
524 case USB_CDC_ETHERNET_TYPE:
525 if (buf[0] < sizeof(*(ctx->ether_desc)))
526 break;
527
528 ctx->ether_desc =
529 (const struct usb_cdc_ether_desc *)buf;
Alexey Orishko900d4952010-11-29 23:23:28 +0000530 dev->hard_mtu =
531 le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
532
Alexey Orishko84e77a82011-02-07 09:45:10 +0000533 if (dev->hard_mtu < CDC_NCM_MIN_DATAGRAM_SIZE)
534 dev->hard_mtu = CDC_NCM_MIN_DATAGRAM_SIZE;
535 else if (dev->hard_mtu > CDC_NCM_MAX_DATAGRAM_SIZE)
536 dev->hard_mtu = CDC_NCM_MAX_DATAGRAM_SIZE;
Alexey Orishko900d4952010-11-29 23:23:28 +0000537 break;
538
539 case USB_CDC_NCM_TYPE:
540 if (buf[0] < sizeof(*(ctx->func_desc)))
541 break;
542
543 ctx->func_desc = (const struct usb_cdc_ncm_desc *)buf;
544 break;
545
546 default:
547 break;
548 }
549advance:
550 /* advance to next descriptor */
551 temp = buf[0];
552 buf += temp;
553 len -= temp;
554 }
555
556 /* check if we got everything */
557 if ((ctx->control == NULL) || (ctx->data == NULL) ||
Alexey Orishko19694ac2011-05-24 05:26:13 +0000558 (ctx->ether_desc == NULL) || (ctx->control != intf))
Alexey Orishko900d4952010-11-29 23:23:28 +0000559 goto error;
560
561 /* claim interfaces, if any */
Alexey Orishko19694ac2011-05-24 05:26:13 +0000562 temp = usb_driver_claim_interface(driver, ctx->data, dev);
563 if (temp)
564 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +0000565
566 iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
567
568 /* reset data interface */
569 temp = usb_set_interface(dev->udev, iface_no, 0);
570 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000571 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000572
573 /* initialize data interface */
574 if (cdc_ncm_setup(ctx))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000575 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000576
577 /* configure data interface */
578 temp = usb_set_interface(dev->udev, iface_no, 1);
579 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000580 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000581
582 cdc_ncm_find_endpoints(ctx, ctx->data);
583 cdc_ncm_find_endpoints(ctx, ctx->control);
584
585 if ((ctx->in_ep == NULL) || (ctx->out_ep == NULL) ||
586 (ctx->status_ep == NULL))
Alexey Orishko19694ac2011-05-24 05:26:13 +0000587 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000588
589 dev->net->ethtool_ops = &cdc_ncm_ethtool_ops;
590
591 usb_set_intfdata(ctx->data, dev);
592 usb_set_intfdata(ctx->control, dev);
593 usb_set_intfdata(ctx->intf, dev);
594
595 temp = usbnet_get_ethernet_addr(dev, ctx->ether_desc->iMACAddress);
596 if (temp)
Alexey Orishko19694ac2011-05-24 05:26:13 +0000597 goto error2;
Alexey Orishko900d4952010-11-29 23:23:28 +0000598
599 dev_info(&dev->udev->dev, "MAC-Address: "
600 "0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x\n",
601 dev->net->dev_addr[0], dev->net->dev_addr[1],
602 dev->net->dev_addr[2], dev->net->dev_addr[3],
603 dev->net->dev_addr[4], dev->net->dev_addr[5]);
604
605 dev->in = usb_rcvbulkpipe(dev->udev,
606 ctx->in_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
607 dev->out = usb_sndbulkpipe(dev->udev,
608 ctx->out_ep->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
609 dev->status = ctx->status_ep;
610 dev->rx_urb_size = ctx->rx_max;
611
612 /*
613 * We should get an event when network connection is "connected" or
614 * "disconnected". Set network connection in "disconnected" state
615 * (carrier is OFF) during attach, so the IP network stack does not
616 * start IPv6 negotiation and more.
617 */
618 netif_carrier_off(dev->net);
619 ctx->tx_speed = ctx->rx_speed = 0;
620 return 0;
621
Alexey Orishko19694ac2011-05-24 05:26:13 +0000622error2:
623 usb_set_intfdata(ctx->control, NULL);
624 usb_set_intfdata(ctx->data, NULL);
625 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko900d4952010-11-29 23:23:28 +0000626error:
627 cdc_ncm_free((struct cdc_ncm_ctx *)dev->data[0]);
628 dev->data[0] = 0;
Alexey Orishko19694ac2011-05-24 05:26:13 +0000629 dev_info(&dev->udev->dev, "bind() failure\n");
Alexey Orishko900d4952010-11-29 23:23:28 +0000630 return -ENODEV;
631}
632
633static void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
634{
635 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
Alexey Orishko19694ac2011-05-24 05:26:13 +0000636 struct usb_driver *driver = driver_of(intf);
Alexey Orishko900d4952010-11-29 23:23:28 +0000637
638 if (ctx == NULL)
639 return; /* no setup */
640
Alexey Orishko19694ac2011-05-24 05:26:13 +0000641 /* disconnect master --> disconnect slave */
642 if (intf == ctx->control && ctx->data) {
643 usb_set_intfdata(ctx->data, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000644 usb_driver_release_interface(driver, ctx->data);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000645 ctx->data = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000646
Alexey Orishko19694ac2011-05-24 05:26:13 +0000647 } else if (intf == ctx->data && ctx->control) {
648 usb_set_intfdata(ctx->control, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000649 usb_driver_release_interface(driver, ctx->control);
Alexey Orishko19694ac2011-05-24 05:26:13 +0000650 ctx->control = NULL;
Alexey Orishko900d4952010-11-29 23:23:28 +0000651 }
652
Alexey Orishko19694ac2011-05-24 05:26:13 +0000653 usb_set_intfdata(ctx->intf, NULL);
Alexey Orishko900d4952010-11-29 23:23:28 +0000654 cdc_ncm_free(ctx);
655}
656
657static void cdc_ncm_zero_fill(u8 *ptr, u32 first, u32 end, u32 max)
658{
659 if (first >= max)
660 return;
661 if (first >= end)
662 return;
663 if (end > max)
664 end = max;
665 memset(ptr + first, 0, end - first);
666}
667
668static struct sk_buff *
669cdc_ncm_fill_tx_frame(struct cdc_ncm_ctx *ctx, struct sk_buff *skb)
670{
671 struct sk_buff *skb_out;
672 u32 rem;
673 u32 offset;
674 u32 last_offset;
675 u16 n = 0;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000676 u8 ready2send = 0;
Alexey Orishko900d4952010-11-29 23:23:28 +0000677
678 /* if there is a remaining skb, it gets priority */
679 if (skb != NULL)
680 swap(skb, ctx->tx_rem_skb);
681 else
Alexey Orishko84e77a82011-02-07 09:45:10 +0000682 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000683
684 /*
685 * +----------------+
686 * | skb_out |
687 * +----------------+
688 * ^ offset
689 * ^ last_offset
690 */
691
692 /* check if we are resuming an OUT skb */
693 if (ctx->tx_curr_skb != NULL) {
694 /* pop variables */
695 skb_out = ctx->tx_curr_skb;
696 offset = ctx->tx_curr_offset;
697 last_offset = ctx->tx_curr_last_offset;
698 n = ctx->tx_curr_frame_num;
699
700 } else {
701 /* reset variables */
Alexey Orishko6c604082011-05-06 03:01:30 +0000702 skb_out = alloc_skb((ctx->tx_max + 1), GFP_ATOMIC);
Alexey Orishko900d4952010-11-29 23:23:28 +0000703 if (skb_out == NULL) {
704 if (skb != NULL) {
705 dev_kfree_skb_any(skb);
706 ctx->netdev->stats.tx_dropped++;
707 }
708 goto exit_no_skb;
709 }
710
711 /* make room for NTH and NDP */
712 offset = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
713 ctx->tx_ndp_modulus) +
714 sizeof(struct usb_cdc_ncm_ndp16) +
715 (ctx->tx_max_datagrams + 1) *
716 sizeof(struct usb_cdc_ncm_dpe16);
717
718 /* store last valid offset before alignment */
719 last_offset = offset;
720 /* align first Datagram offset correctly */
721 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
722 /* zero buffer till the first IP datagram */
723 cdc_ncm_zero_fill(skb_out->data, 0, offset, offset);
724 n = 0;
725 ctx->tx_curr_frame_num = 0;
726 }
727
728 for (; n < ctx->tx_max_datagrams; n++) {
729 /* check if end of transmit buffer is reached */
Alexey Orishko84e77a82011-02-07 09:45:10 +0000730 if (offset >= ctx->tx_max) {
731 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000732 break;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000733 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000734 /* compute maximum buffer size */
735 rem = ctx->tx_max - offset;
736
737 if (skb == NULL) {
738 skb = ctx->tx_rem_skb;
739 ctx->tx_rem_skb = NULL;
740
741 /* check for end of skb */
742 if (skb == NULL)
743 break;
744 }
745
746 if (skb->len > rem) {
747 if (n == 0) {
748 /* won't fit, MTU problem? */
749 dev_kfree_skb_any(skb);
750 skb = NULL;
751 ctx->netdev->stats.tx_dropped++;
752 } else {
753 /* no room for skb - store for later */
754 if (ctx->tx_rem_skb != NULL) {
755 dev_kfree_skb_any(ctx->tx_rem_skb);
756 ctx->netdev->stats.tx_dropped++;
757 }
758 ctx->tx_rem_skb = skb;
759 skb = NULL;
Alexey Orishko84e77a82011-02-07 09:45:10 +0000760 ready2send = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000761 }
762 break;
763 }
764
765 memcpy(((u8 *)skb_out->data) + offset, skb->data, skb->len);
766
767 ctx->tx_ncm.dpe16[n].wDatagramLength = cpu_to_le16(skb->len);
768 ctx->tx_ncm.dpe16[n].wDatagramIndex = cpu_to_le16(offset);
769
770 /* update offset */
771 offset += skb->len;
772
773 /* store last valid offset before alignment */
774 last_offset = offset;
775
776 /* align offset correctly */
777 offset = ALIGN(offset, ctx->tx_modulus) + ctx->tx_remainder;
778
779 /* zero padding */
780 cdc_ncm_zero_fill(skb_out->data, last_offset, offset,
781 ctx->tx_max);
782 dev_kfree_skb_any(skb);
783 skb = NULL;
784 }
785
786 /* free up any dangling skb */
787 if (skb != NULL) {
788 dev_kfree_skb_any(skb);
789 skb = NULL;
790 ctx->netdev->stats.tx_dropped++;
791 }
792
793 ctx->tx_curr_frame_num = n;
794
795 if (n == 0) {
796 /* wait for more frames */
797 /* push variables */
798 ctx->tx_curr_skb = skb_out;
799 ctx->tx_curr_offset = offset;
800 ctx->tx_curr_last_offset = last_offset;
801 goto exit_no_skb;
802
Alexey Orishko84e77a82011-02-07 09:45:10 +0000803 } else if ((n < ctx->tx_max_datagrams) && (ready2send == 0)) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000804 /* wait for more frames */
805 /* push variables */
806 ctx->tx_curr_skb = skb_out;
807 ctx->tx_curr_offset = offset;
808 ctx->tx_curr_last_offset = last_offset;
809 /* set the pending count */
810 if (n < CDC_NCM_RESTART_TIMER_DATAGRAM_CNT)
811 ctx->tx_timer_pending = 2;
812 goto exit_no_skb;
813
814 } else {
815 /* frame goes out */
816 /* variables will be reset at next call */
817 }
818
819 /* check for overflow */
820 if (last_offset > ctx->tx_max)
821 last_offset = ctx->tx_max;
822
823 /* revert offset */
824 offset = last_offset;
825
826 /*
827 * If collected data size is less or equal CDC_NCM_MIN_TX_PKT bytes,
828 * we send buffers as it is. If we get more data, it would be more
829 * efficient for USB HS mobile device with DMA engine to receive a full
830 * size NTB, than canceling DMA transfer and receiving a short packet.
831 */
832 if (offset > CDC_NCM_MIN_TX_PKT)
833 offset = ctx->tx_max;
834
835 /* final zero padding */
836 cdc_ncm_zero_fill(skb_out->data, last_offset, offset, ctx->tx_max);
837
838 /* store last offset */
839 last_offset = offset;
840
Alexey Orishko6c604082011-05-06 03:01:30 +0000841 if (((last_offset < ctx->tx_max) && ((last_offset %
842 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) ||
843 (((last_offset == ctx->tx_max) && ((ctx->tx_max %
844 le16_to_cpu(ctx->out_ep->desc.wMaxPacketSize)) == 0)) &&
845 (ctx->tx_max < le32_to_cpu(ctx->ncm_parm.dwNtbOutMaxSize)))) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000846 /* force short packet */
847 *(((u8 *)skb_out->data) + last_offset) = 0;
848 last_offset++;
849 }
850
851 /* zero the rest of the DPEs plus the last NULL entry */
852 for (; n <= CDC_NCM_DPT_DATAGRAMS_MAX; n++) {
853 ctx->tx_ncm.dpe16[n].wDatagramLength = 0;
854 ctx->tx_ncm.dpe16[n].wDatagramIndex = 0;
855 }
856
857 /* fill out 16-bit NTB header */
858 ctx->tx_ncm.nth16.dwSignature = cpu_to_le32(USB_CDC_NCM_NTH16_SIGN);
859 ctx->tx_ncm.nth16.wHeaderLength =
860 cpu_to_le16(sizeof(ctx->tx_ncm.nth16));
861 ctx->tx_ncm.nth16.wSequence = cpu_to_le16(ctx->tx_seq);
862 ctx->tx_ncm.nth16.wBlockLength = cpu_to_le16(last_offset);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000863 ctx->tx_ncm.nth16.wNdpIndex = ALIGN(sizeof(struct usb_cdc_ncm_nth16),
Alexey Orishko900d4952010-11-29 23:23:28 +0000864 ctx->tx_ndp_modulus);
865
866 memcpy(skb_out->data, &(ctx->tx_ncm.nth16), sizeof(ctx->tx_ncm.nth16));
867 ctx->tx_seq++;
868
869 /* fill out 16-bit NDP table */
870 ctx->tx_ncm.ndp16.dwSignature =
871 cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN);
872 rem = sizeof(ctx->tx_ncm.ndp16) + ((ctx->tx_curr_frame_num + 1) *
873 sizeof(struct usb_cdc_ncm_dpe16));
874 ctx->tx_ncm.ndp16.wLength = cpu_to_le16(rem);
Alexey Orishko84e77a82011-02-07 09:45:10 +0000875 ctx->tx_ncm.ndp16.wNextNdpIndex = 0; /* reserved */
Alexey Orishko900d4952010-11-29 23:23:28 +0000876
Alexey Orishko84e77a82011-02-07 09:45:10 +0000877 memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wNdpIndex,
Alexey Orishko900d4952010-11-29 23:23:28 +0000878 &(ctx->tx_ncm.ndp16),
879 sizeof(ctx->tx_ncm.ndp16));
880
Alexey Orishko84e77a82011-02-07 09:45:10 +0000881 memcpy(((u8 *)skb_out->data) + ctx->tx_ncm.nth16.wNdpIndex +
Alexey Orishko900d4952010-11-29 23:23:28 +0000882 sizeof(ctx->tx_ncm.ndp16),
883 &(ctx->tx_ncm.dpe16),
884 (ctx->tx_curr_frame_num + 1) *
885 sizeof(struct usb_cdc_ncm_dpe16));
886
887 /* set frame length */
888 skb_put(skb_out, last_offset);
889
890 /* return skb */
891 ctx->tx_curr_skb = NULL;
892 return skb_out;
893
894exit_no_skb:
895 return NULL;
896}
897
898static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
899{
900 /* start timer, if not already started */
901 if (timer_pending(&ctx->tx_timer) == 0) {
902 ctx->tx_timer.function = &cdc_ncm_tx_timeout;
903 ctx->tx_timer.data = (unsigned long)ctx;
904 ctx->tx_timer.expires = jiffies + ((HZ + 999) / 1000);
905 add_timer(&ctx->tx_timer);
906 }
907}
908
909static void cdc_ncm_tx_timeout(unsigned long arg)
910{
911 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)arg;
912 u8 restart;
913
914 spin_lock(&ctx->mtx);
915 if (ctx->tx_timer_pending != 0) {
916 ctx->tx_timer_pending--;
917 restart = 1;
Alexey Orishkof742aa82011-01-17 07:07:25 +0000918 } else {
Alexey Orishko900d4952010-11-29 23:23:28 +0000919 restart = 0;
Alexey Orishkof742aa82011-01-17 07:07:25 +0000920 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000921
922 spin_unlock(&ctx->mtx);
923
Alexey Orishkof742aa82011-01-17 07:07:25 +0000924 if (restart) {
925 spin_lock(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000926 cdc_ncm_tx_timeout_start(ctx);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000927 spin_unlock(&ctx->mtx);
928 } else if (ctx->netdev != NULL) {
Alexey Orishko900d4952010-11-29 23:23:28 +0000929 usbnet_start_xmit(NULL, ctx->netdev);
Alexey Orishkof742aa82011-01-17 07:07:25 +0000930 }
Alexey Orishko900d4952010-11-29 23:23:28 +0000931}
932
933static struct sk_buff *
934cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
935{
936 struct sk_buff *skb_out;
937 struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
938 u8 need_timer = 0;
939
940 /*
941 * The Ethernet API we are using does not support transmitting
942 * multiple Ethernet frames in a single call. This driver will
943 * accumulate multiple Ethernet frames and send out a larger
944 * USB frame when the USB buffer is full or when a single jiffies
945 * timeout happens.
946 */
947 if (ctx == NULL)
948 goto error;
949
950 spin_lock(&ctx->mtx);
951 skb_out = cdc_ncm_fill_tx_frame(ctx, skb);
952 if (ctx->tx_curr_skb != NULL)
953 need_timer = 1;
Alexey Orishko900d4952010-11-29 23:23:28 +0000954
955 /* Start timer, if there is a remaining skb */
956 if (need_timer)
957 cdc_ncm_tx_timeout_start(ctx);
958
959 if (skb_out)
960 dev->net->stats.tx_packets += ctx->tx_curr_frame_num;
Alexey Orishkof742aa82011-01-17 07:07:25 +0000961
962 spin_unlock(&ctx->mtx);
Alexey Orishko900d4952010-11-29 23:23:28 +0000963 return skb_out;
964
965error:
966 if (skb != NULL)
967 dev_kfree_skb_any(skb);
968
969 return NULL;
970}
971
972static int cdc_ncm_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
973{
974 struct sk_buff *skb;
975 struct cdc_ncm_ctx *ctx;
976 int sumlen;
977 int actlen;
978 int temp;
979 int nframes;
980 int x;
981 int offset;
982
983 ctx = (struct cdc_ncm_ctx *)dev->data[0];
984 if (ctx == NULL)
985 goto error;
986
987 actlen = skb_in->len;
988 sumlen = CDC_NCM_NTB_MAX_SIZE_RX;
989
990 if (actlen < (sizeof(ctx->rx_ncm.nth16) + sizeof(ctx->rx_ncm.ndp16))) {
991 pr_debug("frame too short\n");
992 goto error;
993 }
994
995 memcpy(&(ctx->rx_ncm.nth16), ((u8 *)skb_in->data),
996 sizeof(ctx->rx_ncm.nth16));
997
998 if (le32_to_cpu(ctx->rx_ncm.nth16.dwSignature) !=
999 USB_CDC_NCM_NTH16_SIGN) {
1000 pr_debug("invalid NTH16 signature <%u>\n",
1001 le32_to_cpu(ctx->rx_ncm.nth16.dwSignature));
1002 goto error;
1003 }
1004
1005 temp = le16_to_cpu(ctx->rx_ncm.nth16.wBlockLength);
1006 if (temp > sumlen) {
1007 pr_debug("unsupported NTB block length %u/%u\n", temp, sumlen);
1008 goto error;
1009 }
1010
Alexey Orishko84e77a82011-02-07 09:45:10 +00001011 temp = le16_to_cpu(ctx->rx_ncm.nth16.wNdpIndex);
Alexey Orishko900d4952010-11-29 23:23:28 +00001012 if ((temp + sizeof(ctx->rx_ncm.ndp16)) > actlen) {
1013 pr_debug("invalid DPT16 index\n");
1014 goto error;
1015 }
1016
1017 memcpy(&(ctx->rx_ncm.ndp16), ((u8 *)skb_in->data) + temp,
1018 sizeof(ctx->rx_ncm.ndp16));
1019
1020 if (le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature) !=
1021 USB_CDC_NCM_NDP16_NOCRC_SIGN) {
1022 pr_debug("invalid DPT16 signature <%u>\n",
1023 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
1024 goto error;
1025 }
1026
1027 if (le16_to_cpu(ctx->rx_ncm.ndp16.wLength) <
1028 USB_CDC_NCM_NDP16_LENGTH_MIN) {
1029 pr_debug("invalid DPT16 length <%u>\n",
1030 le32_to_cpu(ctx->rx_ncm.ndp16.dwSignature));
1031 goto error;
1032 }
1033
1034 nframes = ((le16_to_cpu(ctx->rx_ncm.ndp16.wLength) -
1035 sizeof(struct usb_cdc_ncm_ndp16)) /
1036 sizeof(struct usb_cdc_ncm_dpe16));
1037 nframes--; /* we process NDP entries except for the last one */
1038
1039 pr_debug("nframes = %u\n", nframes);
1040
1041 temp += sizeof(ctx->rx_ncm.ndp16);
1042
1043 if ((temp + nframes * (sizeof(struct usb_cdc_ncm_dpe16))) > actlen) {
1044 pr_debug("Invalid nframes = %d\n", nframes);
1045 goto error;
1046 }
1047
1048 if (nframes > CDC_NCM_DPT_DATAGRAMS_MAX) {
1049 pr_debug("Truncating number of frames from %u to %u\n",
1050 nframes, CDC_NCM_DPT_DATAGRAMS_MAX);
1051 nframes = CDC_NCM_DPT_DATAGRAMS_MAX;
1052 }
1053
1054 memcpy(&(ctx->rx_ncm.dpe16), ((u8 *)skb_in->data) + temp,
1055 nframes * (sizeof(struct usb_cdc_ncm_dpe16)));
1056
1057 for (x = 0; x < nframes; x++) {
1058 offset = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramIndex);
1059 temp = le16_to_cpu(ctx->rx_ncm.dpe16[x].wDatagramLength);
1060
1061 /*
1062 * CDC NCM ch. 3.7
1063 * All entries after first NULL entry are to be ignored
1064 */
1065 if ((offset == 0) || (temp == 0)) {
1066 if (!x)
1067 goto error; /* empty NTB */
1068 break;
1069 }
1070
1071 /* sanity checking */
1072 if (((offset + temp) > actlen) ||
1073 (temp > CDC_NCM_MAX_DATAGRAM_SIZE) || (temp < ETH_HLEN)) {
1074 pr_debug("invalid frame detected (ignored)"
Alexey Orishkof742aa82011-01-17 07:07:25 +00001075 "offset[%u]=%u, length=%u, skb=%p\n",
1076 x, offset, temp, skb_in);
Alexey Orishko900d4952010-11-29 23:23:28 +00001077 if (!x)
1078 goto error;
1079 break;
1080
1081 } else {
1082 skb = skb_clone(skb_in, GFP_ATOMIC);
Jesper Juhl9e567902011-01-13 11:40:11 +00001083 if (!skb)
1084 goto error;
Alexey Orishko900d4952010-11-29 23:23:28 +00001085 skb->len = temp;
1086 skb->data = ((u8 *)skb_in->data) + offset;
1087 skb_set_tail_pointer(skb, temp);
1088 usbnet_skb_return(dev, skb);
1089 }
1090 }
1091 return 1;
1092error:
1093 return 0;
1094}
1095
1096static void
1097cdc_ncm_speed_change(struct cdc_ncm_ctx *ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001098 struct usb_cdc_speed_change *data)
Alexey Orishko900d4952010-11-29 23:23:28 +00001099{
Alexey Orishko84e77a82011-02-07 09:45:10 +00001100 uint32_t rx_speed = le32_to_cpu(data->DLBitRRate);
1101 uint32_t tx_speed = le32_to_cpu(data->ULBitRate);
Alexey Orishko900d4952010-11-29 23:23:28 +00001102
1103 /*
1104 * Currently the USB-NET API does not support reporting the actual
1105 * device speed. Do print it instead.
1106 */
1107 if ((tx_speed != ctx->tx_speed) || (rx_speed != ctx->rx_speed)) {
1108 ctx->tx_speed = tx_speed;
1109 ctx->rx_speed = rx_speed;
1110
1111 if ((tx_speed > 1000000) && (rx_speed > 1000000)) {
1112 printk(KERN_INFO KBUILD_MODNAME
1113 ": %s: %u mbit/s downlink "
1114 "%u mbit/s uplink\n",
1115 ctx->netdev->name,
1116 (unsigned int)(rx_speed / 1000000U),
1117 (unsigned int)(tx_speed / 1000000U));
1118 } else {
1119 printk(KERN_INFO KBUILD_MODNAME
1120 ": %s: %u kbit/s downlink "
1121 "%u kbit/s uplink\n",
1122 ctx->netdev->name,
1123 (unsigned int)(rx_speed / 1000U),
1124 (unsigned int)(tx_speed / 1000U));
1125 }
1126 }
1127}
1128
1129static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
1130{
1131 struct cdc_ncm_ctx *ctx;
1132 struct usb_cdc_notification *event;
1133
1134 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1135
1136 if (urb->actual_length < sizeof(*event))
1137 return;
1138
1139 /* test for split data in 8-byte chunks */
1140 if (test_and_clear_bit(EVENT_STS_SPLIT, &dev->flags)) {
1141 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001142 (struct usb_cdc_speed_change *)urb->transfer_buffer);
Alexey Orishko900d4952010-11-29 23:23:28 +00001143 return;
1144 }
1145
1146 event = urb->transfer_buffer;
1147
1148 switch (event->bNotificationType) {
1149 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1150 /*
1151 * According to the CDC NCM specification ch.7.1
1152 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
1153 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
1154 */
1155 ctx->connected = event->wValue;
1156
1157 printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
1158 " %sconnected\n",
1159 ctx->netdev->name, ctx->connected ? "" : "dis");
1160
1161 if (ctx->connected)
1162 netif_carrier_on(dev->net);
1163 else {
1164 netif_carrier_off(dev->net);
1165 ctx->tx_speed = ctx->rx_speed = 0;
1166 }
1167 break;
1168
1169 case USB_CDC_NOTIFY_SPEED_CHANGE:
Alexey Orishko84e77a82011-02-07 09:45:10 +00001170 if (urb->actual_length < (sizeof(*event) +
1171 sizeof(struct usb_cdc_speed_change)))
Alexey Orishko900d4952010-11-29 23:23:28 +00001172 set_bit(EVENT_STS_SPLIT, &dev->flags);
1173 else
1174 cdc_ncm_speed_change(ctx,
Alexey Orishko84e77a82011-02-07 09:45:10 +00001175 (struct usb_cdc_speed_change *) &event[1]);
Alexey Orishko900d4952010-11-29 23:23:28 +00001176 break;
1177
1178 default:
1179 dev_err(&dev->udev->dev, "NCM: unexpected "
1180 "notification 0x%02x!\n", event->bNotificationType);
1181 break;
1182 }
1183}
1184
1185static int cdc_ncm_check_connect(struct usbnet *dev)
1186{
1187 struct cdc_ncm_ctx *ctx;
1188
1189 ctx = (struct cdc_ncm_ctx *)dev->data[0];
1190 if (ctx == NULL)
1191 return 1; /* disconnected */
1192
1193 return !ctx->connected;
1194}
1195
1196static int
1197cdc_ncm_probe(struct usb_interface *udev, const struct usb_device_id *prod)
1198{
1199 return usbnet_probe(udev, prod);
1200}
1201
1202static void cdc_ncm_disconnect(struct usb_interface *intf)
1203{
1204 struct usbnet *dev = usb_get_intfdata(intf);
1205
1206 if (dev == NULL)
1207 return; /* already disconnected */
1208
1209 usbnet_disconnect(intf);
1210}
1211
1212static int cdc_ncm_manage_power(struct usbnet *dev, int status)
1213{
1214 dev->intf->needs_remote_wakeup = status;
1215 return 0;
1216}
1217
1218static const struct driver_info cdc_ncm_info = {
1219 .description = "CDC NCM",
Arnd Bergmannc2613442011-04-01 20:12:02 -07001220 .flags = FLAG_POINTTOPOINT | FLAG_NO_SETINT | FLAG_MULTI_PACKET,
Alexey Orishko900d4952010-11-29 23:23:28 +00001221 .bind = cdc_ncm_bind,
1222 .unbind = cdc_ncm_unbind,
1223 .check_connect = cdc_ncm_check_connect,
1224 .manage_power = cdc_ncm_manage_power,
1225 .status = cdc_ncm_status,
1226 .rx_fixup = cdc_ncm_rx_fixup,
1227 .tx_fixup = cdc_ncm_tx_fixup,
1228};
1229
1230static struct usb_driver cdc_ncm_driver = {
1231 .name = "cdc_ncm",
1232 .id_table = cdc_devs,
1233 .probe = cdc_ncm_probe,
1234 .disconnect = cdc_ncm_disconnect,
1235 .suspend = usbnet_suspend,
1236 .resume = usbnet_resume,
Stefan Metzmacher85e3c652011-06-01 02:01:41 +00001237 .reset_resume = usbnet_resume,
Alexey Orishko900d4952010-11-29 23:23:28 +00001238 .supports_autosuspend = 1,
1239};
1240
1241static struct ethtool_ops cdc_ncm_ethtool_ops = {
1242 .get_drvinfo = cdc_ncm_get_drvinfo,
1243 .get_link = usbnet_get_link,
1244 .get_msglevel = usbnet_get_msglevel,
1245 .set_msglevel = usbnet_set_msglevel,
1246 .get_settings = usbnet_get_settings,
1247 .set_settings = usbnet_set_settings,
1248 .nway_reset = usbnet_nway_reset,
1249};
1250
1251static int __init cdc_ncm_init(void)
1252{
1253 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION "\n");
1254 return usb_register(&cdc_ncm_driver);
1255}
1256
1257module_init(cdc_ncm_init);
1258
1259static void __exit cdc_ncm_exit(void)
1260{
1261 usb_deregister(&cdc_ncm_driver);
1262}
1263
1264module_exit(cdc_ncm_exit);
1265
1266MODULE_AUTHOR("Hans Petter Selasky");
1267MODULE_DESCRIPTION("USB CDC NCM host driver");
1268MODULE_LICENSE("Dual BSD/GPL");