usb: gadget: NCM: Add transmit multi-frame.
[linux-2.6-block.git] / drivers / usb / gadget / f_ncm.c
CommitLineData
9f6ce424
YK
1/*
2 * f_ncm.c -- USB CDC Network (NCM) link function driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
6 *
7 * The driver borrows from f_ecm.c which is:
8 *
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2008 Nokia Corporation
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
9f6ce424
YK
16 */
17
18#include <linux/kernel.h>
40d133d7 19#include <linux/module.h>
9f6ce424
YK
20#include <linux/device.h>
21#include <linux/etherdevice.h>
22#include <linux/crc32.h>
23
24#include <linux/usb/cdc.h>
25
26#include "u_ether.h"
aa83c6ad 27#include "u_ether_configfs.h"
40d133d7 28#include "u_ncm.h"
9f6ce424
YK
29
30/*
31 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
32 * NCM is intended to be used with high-speed network attachments.
33 *
34 * Note that NCM requires the use of "alternate settings" for its data
35 * interface. This means that the set_alt() method has real work to do,
36 * and also means that a get_alt() method is required.
37 */
38
39/* to trigger crc/non-crc ndp signature */
40
41#define NCM_NDP_HDR_CRC_MASK 0x01000000
42#define NCM_NDP_HDR_CRC 0x01000000
43#define NCM_NDP_HDR_NOCRC 0x00000000
44
9f6ce424
YK
45enum ncm_notify_state {
46 NCM_NOTIFY_NONE, /* don't notify */
47 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
48 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
49};
50
51struct f_ncm {
52 struct gether port;
53 u8 ctrl_id, data_id;
54
55 char ethaddr[14];
56
9f6ce424 57 struct usb_ep *notify;
9f6ce424
YK
58 struct usb_request *notify_req;
59 u8 notify_state;
60 bool is_open;
61
8d640ad3 62 const struct ndp_parser_opts *parser_opts;
9f6ce424 63 bool is_crc;
8d640ad3 64 u32 ndp_sign;
9f6ce424
YK
65
66 /*
67 * for notification, it is accessed from both
68 * callback and ethernet open/close
69 */
70 spinlock_t lock;
6d3865f9
JB
71
72 struct net_device *netdev;
73
74 /* For multi-frame NDP TX */
75 struct sk_buff *skb_tx_data;
76 struct sk_buff *skb_tx_ndp;
77 u16 ndp_dgram_count;
78 bool timer_force_tx;
79 struct tasklet_struct tx_tasklet;
80 struct hrtimer task_timer;
81
82 bool timer_stopping;
9f6ce424
YK
83};
84
85static inline struct f_ncm *func_to_ncm(struct usb_function *f)
86{
87 return container_of(f, struct f_ncm, port.func);
88}
89
90/* peak (theoretical) bulk transfer rate in bits-per-second */
91static inline unsigned ncm_bitrate(struct usb_gadget *g)
92{
93 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
94 return 13 * 512 * 8 * 1000 * 8;
95 else
96 return 19 * 64 * 1 * 1000 * 8;
97}
98
99/*-------------------------------------------------------------------------*/
100
101/*
102 * We cannot group frames so use just the minimal size which ok to put
103 * one max-size ethernet frame.
104 * If the host can group frames, allow it to do that, 16K is selected,
105 * because it's used by default by the current linux host driver
106 */
6d3865f9 107#define NTB_DEFAULT_IN_SIZE 16384
9f6ce424
YK
108#define NTB_OUT_SIZE 16384
109
6d3865f9
JB
110/* Allocation for storing the NDP, 32 should suffice for a
111 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
112 * be transmitted in a single 16kB skb, though when sending full size
113 * packets this limit will be plenty.
114 * Smaller packets are not likely to be trying to maximize the
115 * throughput and will be mstly sending smaller infrequent frames.
9f6ce424 116 */
6d3865f9 117#define TX_MAX_NUM_DPE 32
9f6ce424 118
6d3865f9
JB
119/* Delay for the transmit to wait before sending an unfilled NTB frame. */
120#define TX_TIMEOUT_NSECS 300000
9f6ce424
YK
121
122#define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
123 USB_CDC_NCM_NTB32_SUPPORTED)
124
125static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
f72e3b78 126 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
9f6ce424
YK
127 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
128 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
129 .wNdpInDivisor = cpu_to_le16(4),
130 .wNdpInPayloadRemainder = cpu_to_le16(0),
131 .wNdpInAlignment = cpu_to_le16(4),
132
133 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
134 .wNdpOutDivisor = cpu_to_le16(4),
135 .wNdpOutPayloadRemainder = cpu_to_le16(0),
136 .wNdpOutAlignment = cpu_to_le16(4),
137};
138
139/*
140 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
141 * packet, to simplify cancellation; and a big transfer interval, to
142 * waste less bandwidth.
143 */
144
bcb2f99c 145#define NCM_STATUS_INTERVAL_MS 32
9f6ce424
YK
146#define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
147
40d133d7 148static struct usb_interface_assoc_descriptor ncm_iad_desc = {
9f6ce424
YK
149 .bLength = sizeof ncm_iad_desc,
150 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
151
152 /* .bFirstInterface = DYNAMIC, */
153 .bInterfaceCount = 2, /* control + data */
154 .bFunctionClass = USB_CLASS_COMM,
155 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
156 .bFunctionProtocol = USB_CDC_PROTO_NONE,
157 /* .iFunction = DYNAMIC */
158};
159
160/* interface descriptor: */
161
40d133d7 162static struct usb_interface_descriptor ncm_control_intf = {
9f6ce424
YK
163 .bLength = sizeof ncm_control_intf,
164 .bDescriptorType = USB_DT_INTERFACE,
165
166 /* .bInterfaceNumber = DYNAMIC */
167 .bNumEndpoints = 1,
168 .bInterfaceClass = USB_CLASS_COMM,
169 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
170 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
171 /* .iInterface = DYNAMIC */
172};
173
40d133d7 174static struct usb_cdc_header_desc ncm_header_desc = {
9f6ce424
YK
175 .bLength = sizeof ncm_header_desc,
176 .bDescriptorType = USB_DT_CS_INTERFACE,
177 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
178
179 .bcdCDC = cpu_to_le16(0x0110),
180};
181
40d133d7 182static struct usb_cdc_union_desc ncm_union_desc = {
9f6ce424
YK
183 .bLength = sizeof(ncm_union_desc),
184 .bDescriptorType = USB_DT_CS_INTERFACE,
185 .bDescriptorSubType = USB_CDC_UNION_TYPE,
186 /* .bMasterInterface0 = DYNAMIC */
187 /* .bSlaveInterface0 = DYNAMIC */
188};
189
40d133d7 190static struct usb_cdc_ether_desc ecm_desc = {
9f6ce424
YK
191 .bLength = sizeof ecm_desc,
192 .bDescriptorType = USB_DT_CS_INTERFACE,
193 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
194
195 /* this descriptor actually adds value, surprise! */
196 /* .iMACAddress = DYNAMIC */
197 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
198 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
199 .wNumberMCFilters = cpu_to_le16(0),
200 .bNumberPowerFilters = 0,
201};
202
203#define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
204
40d133d7 205static struct usb_cdc_ncm_desc ncm_desc = {
9f6ce424
YK
206 .bLength = sizeof ncm_desc,
207 .bDescriptorType = USB_DT_CS_INTERFACE,
208 .bDescriptorSubType = USB_CDC_NCM_TYPE,
209
210 .bcdNcmVersion = cpu_to_le16(0x0100),
211 /* can process SetEthernetPacketFilter */
212 .bmNetworkCapabilities = NCAPS,
213};
214
215/* the default data interface has no endpoints ... */
216
40d133d7 217static struct usb_interface_descriptor ncm_data_nop_intf = {
9f6ce424
YK
218 .bLength = sizeof ncm_data_nop_intf,
219 .bDescriptorType = USB_DT_INTERFACE,
220
221 .bInterfaceNumber = 1,
222 .bAlternateSetting = 0,
223 .bNumEndpoints = 0,
224 .bInterfaceClass = USB_CLASS_CDC_DATA,
225 .bInterfaceSubClass = 0,
226 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
227 /* .iInterface = DYNAMIC */
228};
229
230/* ... but the "real" data interface has two bulk endpoints */
231
40d133d7 232static struct usb_interface_descriptor ncm_data_intf = {
9f6ce424
YK
233 .bLength = sizeof ncm_data_intf,
234 .bDescriptorType = USB_DT_INTERFACE,
235
236 .bInterfaceNumber = 1,
237 .bAlternateSetting = 1,
238 .bNumEndpoints = 2,
239 .bInterfaceClass = USB_CLASS_CDC_DATA,
240 .bInterfaceSubClass = 0,
241 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
242 /* .iInterface = DYNAMIC */
243};
244
245/* full speed support: */
246
40d133d7 247static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
9f6ce424
YK
248 .bLength = USB_DT_ENDPOINT_SIZE,
249 .bDescriptorType = USB_DT_ENDPOINT,
250
251 .bEndpointAddress = USB_DIR_IN,
252 .bmAttributes = USB_ENDPOINT_XFER_INT,
253 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
bcb2f99c 254 .bInterval = NCM_STATUS_INTERVAL_MS,
9f6ce424
YK
255};
256
40d133d7 257static struct usb_endpoint_descriptor fs_ncm_in_desc = {
9f6ce424
YK
258 .bLength = USB_DT_ENDPOINT_SIZE,
259 .bDescriptorType = USB_DT_ENDPOINT,
260
261 .bEndpointAddress = USB_DIR_IN,
262 .bmAttributes = USB_ENDPOINT_XFER_BULK,
263};
264
40d133d7 265static struct usb_endpoint_descriptor fs_ncm_out_desc = {
9f6ce424
YK
266 .bLength = USB_DT_ENDPOINT_SIZE,
267 .bDescriptorType = USB_DT_ENDPOINT,
268
269 .bEndpointAddress = USB_DIR_OUT,
270 .bmAttributes = USB_ENDPOINT_XFER_BULK,
271};
272
40d133d7 273static struct usb_descriptor_header *ncm_fs_function[] = {
9f6ce424
YK
274 (struct usb_descriptor_header *) &ncm_iad_desc,
275 /* CDC NCM control descriptors */
276 (struct usb_descriptor_header *) &ncm_control_intf,
277 (struct usb_descriptor_header *) &ncm_header_desc,
278 (struct usb_descriptor_header *) &ncm_union_desc,
279 (struct usb_descriptor_header *) &ecm_desc,
280 (struct usb_descriptor_header *) &ncm_desc,
281 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
282 /* data interface, altsettings 0 and 1 */
283 (struct usb_descriptor_header *) &ncm_data_nop_intf,
284 (struct usb_descriptor_header *) &ncm_data_intf,
285 (struct usb_descriptor_header *) &fs_ncm_in_desc,
286 (struct usb_descriptor_header *) &fs_ncm_out_desc,
287 NULL,
288};
289
290/* high speed support: */
291
40d133d7 292static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
9f6ce424
YK
293 .bLength = USB_DT_ENDPOINT_SIZE,
294 .bDescriptorType = USB_DT_ENDPOINT,
295
296 .bEndpointAddress = USB_DIR_IN,
297 .bmAttributes = USB_ENDPOINT_XFER_INT,
298 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
bcb2f99c 299 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
9f6ce424 300};
40d133d7 301static struct usb_endpoint_descriptor hs_ncm_in_desc = {
9f6ce424
YK
302 .bLength = USB_DT_ENDPOINT_SIZE,
303 .bDescriptorType = USB_DT_ENDPOINT,
304
305 .bEndpointAddress = USB_DIR_IN,
306 .bmAttributes = USB_ENDPOINT_XFER_BULK,
307 .wMaxPacketSize = cpu_to_le16(512),
308};
309
40d133d7 310static struct usb_endpoint_descriptor hs_ncm_out_desc = {
9f6ce424
YK
311 .bLength = USB_DT_ENDPOINT_SIZE,
312 .bDescriptorType = USB_DT_ENDPOINT,
313
314 .bEndpointAddress = USB_DIR_OUT,
315 .bmAttributes = USB_ENDPOINT_XFER_BULK,
316 .wMaxPacketSize = cpu_to_le16(512),
317};
318
40d133d7 319static struct usb_descriptor_header *ncm_hs_function[] = {
9f6ce424
YK
320 (struct usb_descriptor_header *) &ncm_iad_desc,
321 /* CDC NCM control descriptors */
322 (struct usb_descriptor_header *) &ncm_control_intf,
323 (struct usb_descriptor_header *) &ncm_header_desc,
324 (struct usb_descriptor_header *) &ncm_union_desc,
325 (struct usb_descriptor_header *) &ecm_desc,
326 (struct usb_descriptor_header *) &ncm_desc,
327 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
328 /* data interface, altsettings 0 and 1 */
329 (struct usb_descriptor_header *) &ncm_data_nop_intf,
330 (struct usb_descriptor_header *) &ncm_data_intf,
331 (struct usb_descriptor_header *) &hs_ncm_in_desc,
332 (struct usb_descriptor_header *) &hs_ncm_out_desc,
333 NULL,
334};
335
336/* string descriptors: */
337
338#define STRING_CTRL_IDX 0
339#define STRING_MAC_IDX 1
340#define STRING_DATA_IDX 2
341#define STRING_IAD_IDX 3
342
343static struct usb_string ncm_string_defs[] = {
344 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
1616e99d 345 [STRING_MAC_IDX].s = "",
9f6ce424
YK
346 [STRING_DATA_IDX].s = "CDC Network Data",
347 [STRING_IAD_IDX].s = "CDC NCM",
348 { } /* end of list */
349};
350
351static struct usb_gadget_strings ncm_string_table = {
352 .language = 0x0409, /* en-us */
353 .strings = ncm_string_defs,
354};
355
356static struct usb_gadget_strings *ncm_strings[] = {
357 &ncm_string_table,
358 NULL,
359};
360
361/*
362 * Here are options for NCM Datagram Pointer table (NDP) parser.
363 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
364 * in NDP16 offsets and sizes fields are 1 16bit word wide,
365 * in NDP32 -- 2 16bit words wide. Also signatures are different.
366 * To make the parser code the same, put the differences in the structure,
367 * and switch pointers to the structures when the format is changed.
368 */
369
370struct ndp_parser_opts {
371 u32 nth_sign;
372 u32 ndp_sign;
373 unsigned nth_size;
374 unsigned ndp_size;
6d3865f9 375 unsigned dpe_size;
9f6ce424
YK
376 unsigned ndplen_align;
377 /* sizes in u16 units */
378 unsigned dgram_item_len; /* index or length */
379 unsigned block_length;
6d3865f9 380 unsigned ndp_index;
9f6ce424
YK
381 unsigned reserved1;
382 unsigned reserved2;
6d3865f9 383 unsigned next_ndp_index;
9f6ce424
YK
384};
385
386#define INIT_NDP16_OPTS { \
387 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
388 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
389 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
390 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
6d3865f9 391 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
9f6ce424
YK
392 .ndplen_align = 4, \
393 .dgram_item_len = 1, \
394 .block_length = 1, \
6d3865f9 395 .ndp_index = 1, \
9f6ce424
YK
396 .reserved1 = 0, \
397 .reserved2 = 0, \
6d3865f9 398 .next_ndp_index = 1, \
9f6ce424
YK
399 }
400
401
402#define INIT_NDP32_OPTS { \
403 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
404 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
405 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
406 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
6d3865f9 407 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
9f6ce424
YK
408 .ndplen_align = 8, \
409 .dgram_item_len = 2, \
410 .block_length = 2, \
6d3865f9 411 .ndp_index = 2, \
9f6ce424
YK
412 .reserved1 = 1, \
413 .reserved2 = 2, \
6d3865f9 414 .next_ndp_index = 2, \
9f6ce424
YK
415 }
416
8d640ad3
SAS
417static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
418static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
9f6ce424
YK
419
420static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
421{
422 switch (size) {
423 case 1:
424 put_unaligned_le16((u16)val, *p);
425 break;
426 case 2:
427 put_unaligned_le32((u32)val, *p);
428
429 break;
430 default:
431 BUG();
432 }
433
434 *p += size;
435}
436
437static inline unsigned get_ncm(__le16 **p, unsigned size)
438{
439 unsigned tmp;
440
441 switch (size) {
442 case 1:
443 tmp = get_unaligned_le16(*p);
444 break;
445 case 2:
446 tmp = get_unaligned_le32(*p);
447 break;
448 default:
449 BUG();
450 }
451
452 *p += size;
453 return tmp;
454}
455
456/*-------------------------------------------------------------------------*/
457
458static inline void ncm_reset_values(struct f_ncm *ncm)
459{
460 ncm->parser_opts = &ndp16_opts;
461 ncm->is_crc = false;
462 ncm->port.cdc_filter = DEFAULT_FILTER;
463
464 /* doesn't make sense for ncm, fixed size used */
465 ncm->port.header_len = 0;
466
467 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
468 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
469}
470
471/*
472 * Context: ncm->lock held
473 */
474static void ncm_do_notify(struct f_ncm *ncm)
475{
476 struct usb_request *req = ncm->notify_req;
477 struct usb_cdc_notification *event;
478 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
479 __le32 *data;
480 int status;
481
482 /* notification already in flight? */
483 if (!req)
484 return;
485
486 event = req->buf;
487 switch (ncm->notify_state) {
488 case NCM_NOTIFY_NONE:
489 return;
490
491 case NCM_NOTIFY_CONNECT:
492 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
493 if (ncm->is_open)
494 event->wValue = cpu_to_le16(1);
495 else
496 event->wValue = cpu_to_le16(0);
497 event->wLength = 0;
498 req->length = sizeof *event;
499
500 DBG(cdev, "notify connect %s\n",
501 ncm->is_open ? "true" : "false");
502 ncm->notify_state = NCM_NOTIFY_NONE;
503 break;
504
505 case NCM_NOTIFY_SPEED:
506 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
507 event->wValue = cpu_to_le16(0);
508 event->wLength = cpu_to_le16(8);
509 req->length = NCM_STATUS_BYTECOUNT;
510
511 /* SPEED_CHANGE data is up/down speeds in bits/sec */
512 data = req->buf + sizeof *event;
513 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
514 data[1] = data[0];
515
516 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
517 ncm->notify_state = NCM_NOTIFY_CONNECT;
518 break;
519 }
520 event->bmRequestType = 0xA1;
521 event->wIndex = cpu_to_le16(ncm->ctrl_id);
522
523 ncm->notify_req = NULL;
524 /*
525 * In double buffering if there is a space in FIFO,
526 * completion callback can be called right after the call,
527 * so unlocking
528 */
529 spin_unlock(&ncm->lock);
530 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
531 spin_lock(&ncm->lock);
532 if (status < 0) {
533 ncm->notify_req = req;
534 DBG(cdev, "notify --> %d\n", status);
535 }
536}
537
538/*
539 * Context: ncm->lock held
540 */
541static void ncm_notify(struct f_ncm *ncm)
542{
543 /*
544 * NOTE on most versions of Linux, host side cdc-ethernet
545 * won't listen for notifications until its netdevice opens.
546 * The first notification then sits in the FIFO for a long
547 * time, and the second one is queued.
548 *
549 * If ncm_notify() is called before the second (CONNECT)
550 * notification is sent, then it will reset to send the SPEED
551 * notificaion again (and again, and again), but it's not a problem
552 */
553 ncm->notify_state = NCM_NOTIFY_SPEED;
554 ncm_do_notify(ncm);
555}
556
557static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
558{
559 struct f_ncm *ncm = req->context;
560 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
561 struct usb_cdc_notification *event = req->buf;
562
563 spin_lock(&ncm->lock);
564 switch (req->status) {
565 case 0:
566 VDBG(cdev, "Notification %02x sent\n",
567 event->bNotificationType);
568 break;
569 case -ECONNRESET:
570 case -ESHUTDOWN:
571 ncm->notify_state = NCM_NOTIFY_NONE;
572 break;
573 default:
574 DBG(cdev, "event %02x --> %d\n",
575 event->bNotificationType, req->status);
576 break;
577 }
578 ncm->notify_req = req;
579 ncm_do_notify(ncm);
580 spin_unlock(&ncm->lock);
581}
582
583static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
584{
585 /* now for SET_NTB_INPUT_SIZE only */
586 unsigned in_size;
587 struct usb_function *f = req->context;
588 struct f_ncm *ncm = func_to_ncm(f);
589 struct usb_composite_dev *cdev = ep->driver_data;
590
591 req->context = NULL;
592 if (req->status || req->actual != req->length) {
593 DBG(cdev, "Bad control-OUT transfer\n");
594 goto invalid;
595 }
596
597 in_size = get_unaligned_le32(req->buf);
598 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
599 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
600 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
601 goto invalid;
602 }
603
604 ncm->port.fixed_in_len = in_size;
605 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
606 return;
607
608invalid:
609 usb_ep_set_halt(ep);
610 return;
611}
612
613static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
614{
615 struct f_ncm *ncm = func_to_ncm(f);
616 struct usb_composite_dev *cdev = f->config->cdev;
617 struct usb_request *req = cdev->req;
618 int value = -EOPNOTSUPP;
619 u16 w_index = le16_to_cpu(ctrl->wIndex);
620 u16 w_value = le16_to_cpu(ctrl->wValue);
621 u16 w_length = le16_to_cpu(ctrl->wLength);
622
623 /*
624 * composite driver infrastructure handles everything except
625 * CDC class messages; interface activation uses set_alt().
626 */
627 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
628 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
629 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
630 /*
631 * see 6.2.30: no data, wIndex = interface,
632 * wValue = packet filter bitmap
633 */
634 if (w_length != 0 || w_index != ncm->ctrl_id)
635 goto invalid;
636 DBG(cdev, "packet filter %02x\n", w_value);
637 /*
638 * REVISIT locking of cdc_filter. This assumes the UDC
639 * driver won't have a concurrent packet TX irq running on
640 * another CPU; or that if it does, this write is atomic...
641 */
642 ncm->port.cdc_filter = w_value;
643 value = 0;
644 break;
645 /*
646 * and optionally:
647 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
648 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
649 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
650 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
651 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
652 * case USB_CDC_GET_ETHERNET_STATISTIC:
653 */
654
655 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
656 | USB_CDC_GET_NTB_PARAMETERS:
657
658 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
659 goto invalid;
660 value = w_length > sizeof ntb_parameters ?
661 sizeof ntb_parameters : w_length;
662 memcpy(req->buf, &ntb_parameters, value);
663 VDBG(cdev, "Host asked NTB parameters\n");
664 break;
665
666 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
667 | USB_CDC_GET_NTB_INPUT_SIZE:
668
669 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
670 goto invalid;
671 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
672 value = 4;
673 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
674 ncm->port.fixed_in_len);
675 break;
676
677 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
678 | USB_CDC_SET_NTB_INPUT_SIZE:
679 {
680 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
681 goto invalid;
682 req->complete = ncm_ep0out_complete;
683 req->length = w_length;
684 req->context = f;
685
686 value = req->length;
687 break;
688 }
689
690 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
691 | USB_CDC_GET_NTB_FORMAT:
692 {
693 uint16_t format;
694
695 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
696 goto invalid;
697 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
698 put_unaligned_le16(format, req->buf);
699 value = 2;
700 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
701 break;
702 }
703
704 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
705 | USB_CDC_SET_NTB_FORMAT:
706 {
707 if (w_length != 0 || w_index != ncm->ctrl_id)
708 goto invalid;
709 switch (w_value) {
710 case 0x0000:
711 ncm->parser_opts = &ndp16_opts;
712 DBG(cdev, "NCM16 selected\n");
713 break;
714 case 0x0001:
715 ncm->parser_opts = &ndp32_opts;
716 DBG(cdev, "NCM32 selected\n");
717 break;
718 default:
719 goto invalid;
720 }
721 value = 0;
722 break;
723 }
724 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
725 | USB_CDC_GET_CRC_MODE:
726 {
727 uint16_t is_crc;
728
729 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
730 goto invalid;
731 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
732 put_unaligned_le16(is_crc, req->buf);
733 value = 2;
734 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
735 break;
736 }
737
738 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
739 | USB_CDC_SET_CRC_MODE:
740 {
741 int ndp_hdr_crc = 0;
742
743 if (w_length != 0 || w_index != ncm->ctrl_id)
744 goto invalid;
745 switch (w_value) {
746 case 0x0000:
747 ncm->is_crc = false;
748 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
749 DBG(cdev, "non-CRC mode selected\n");
750 break;
751 case 0x0001:
752 ncm->is_crc = true;
753 ndp_hdr_crc = NCM_NDP_HDR_CRC;
754 DBG(cdev, "CRC mode selected\n");
755 break;
756 default:
757 goto invalid;
758 }
8d640ad3 759 ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
9f6ce424
YK
760 value = 0;
761 break;
762 }
763
764 /* and disabled in ncm descriptor: */
765 /* case USB_CDC_GET_NET_ADDRESS: */
766 /* case USB_CDC_SET_NET_ADDRESS: */
767 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
768 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
769
770 default:
771invalid:
772 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
773 ctrl->bRequestType, ctrl->bRequest,
774 w_value, w_index, w_length);
775 }
776
777 /* respond with data transfer or status phase? */
778 if (value >= 0) {
779 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
780 ctrl->bRequestType, ctrl->bRequest,
781 w_value, w_index, w_length);
782 req->zero = 0;
783 req->length = value;
784 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
785 if (value < 0)
786 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
787 ctrl->bRequestType, ctrl->bRequest,
788 value);
789 }
790
791 /* device either stalls (value < 0) or reports success */
792 return value;
793}
794
795
796static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
797{
798 struct f_ncm *ncm = func_to_ncm(f);
799 struct usb_composite_dev *cdev = f->config->cdev;
800
801 /* Control interface has only altsetting 0 */
802 if (intf == ncm->ctrl_id) {
803 if (alt != 0)
804 goto fail;
805
806 if (ncm->notify->driver_data) {
807 DBG(cdev, "reset ncm control %d\n", intf);
808 usb_ep_disable(ncm->notify);
ea2a1df7
TB
809 }
810
811 if (!(ncm->notify->desc)) {
9f6ce424 812 DBG(cdev, "init ncm ctrl %d\n", intf);
ea2a1df7
TB
813 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
814 goto fail;
9f6ce424 815 }
72c973dd 816 usb_ep_enable(ncm->notify);
9f6ce424
YK
817 ncm->notify->driver_data = ncm;
818
819 /* Data interface has two altsettings, 0 and 1 */
820 } else if (intf == ncm->data_id) {
821 if (alt > 1)
822 goto fail;
823
824 if (ncm->port.in_ep->driver_data) {
825 DBG(cdev, "reset ncm\n");
6d3865f9
JB
826 ncm->timer_stopping = true;
827 ncm->netdev = NULL;
9f6ce424
YK
828 gether_disconnect(&ncm->port);
829 ncm_reset_values(ncm);
830 }
831
832 /*
833 * CDC Network only sends data in non-default altsettings.
834 * Changing altsettings resets filters, statistics, etc.
835 */
836 if (alt == 1) {
837 struct net_device *net;
838
ea2a1df7
TB
839 if (!ncm->port.in_ep->desc ||
840 !ncm->port.out_ep->desc) {
9f6ce424 841 DBG(cdev, "init ncm\n");
ea2a1df7
TB
842 if (config_ep_by_speed(cdev->gadget, f,
843 ncm->port.in_ep) ||
844 config_ep_by_speed(cdev->gadget, f,
845 ncm->port.out_ep)) {
846 ncm->port.in_ep->desc = NULL;
847 ncm->port.out_ep->desc = NULL;
848 goto fail;
849 }
9f6ce424
YK
850 }
851
852 /* TODO */
853 /* Enable zlps by default for NCM conformance;
854 * override for musb_hdrc (avoids txdma ovhead)
855 */
856 ncm->port.is_zlp_ok = !(
857 gadget_is_musbhdrc(cdev->gadget)
858 );
859 ncm->port.cdc_filter = DEFAULT_FILTER;
860 DBG(cdev, "activate ncm\n");
861 net = gether_connect(&ncm->port);
862 if (IS_ERR(net))
863 return PTR_ERR(net);
6d3865f9
JB
864 ncm->netdev = net;
865 ncm->timer_stopping = false;
9f6ce424
YK
866 }
867
868 spin_lock(&ncm->lock);
869 ncm_notify(ncm);
870 spin_unlock(&ncm->lock);
871 } else
872 goto fail;
873
874 return 0;
875fail:
876 return -EINVAL;
877}
878
879/*
880 * Because the data interface supports multiple altsettings,
881 * this NCM function *MUST* implement a get_alt() method.
882 */
883static int ncm_get_alt(struct usb_function *f, unsigned intf)
884{
885 struct f_ncm *ncm = func_to_ncm(f);
886
887 if (intf == ncm->ctrl_id)
888 return 0;
889 return ncm->port.in_ep->driver_data ? 1 : 0;
890}
891
6d3865f9
JB
892static struct sk_buff *package_for_tx(struct f_ncm *ncm)
893{
894 __le16 *ntb_iter;
895 struct sk_buff *skb2 = NULL;
896 unsigned ndp_pad;
897 unsigned ndp_index;
898 unsigned new_len;
899
900 const struct ndp_parser_opts *opts = ncm->parser_opts;
901 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
902 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
903
904 /* Stop the timer */
905 hrtimer_try_to_cancel(&ncm->task_timer);
906
907 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
908 ncm->skb_tx_data->len;
909 ndp_index = ncm->skb_tx_data->len + ndp_pad;
910 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
911
912 /* Set the final BlockLength and wNdpIndex */
913 ntb_iter = (void *) ncm->skb_tx_data->data;
914 /* Increment pointer to BlockLength */
915 ntb_iter += 2 + 1 + 1;
916 put_ncm(&ntb_iter, opts->block_length, new_len);
917 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
918
919 /* Set the final NDP wLength */
920 new_len = opts->ndp_size +
921 (ncm->ndp_dgram_count * dgram_idx_len);
922 ncm->ndp_dgram_count = 0;
923 /* Increment from start to wLength */
924 ntb_iter = (void *) ncm->skb_tx_ndp->data;
925 ntb_iter += 2;
926 put_unaligned_le16(new_len, ntb_iter);
927
928 /* Merge the skbs */
929 swap(skb2, ncm->skb_tx_data);
930 if (ncm->skb_tx_data) {
931 dev_kfree_skb_any(ncm->skb_tx_data);
932 ncm->skb_tx_data = NULL;
933 }
934
935 /* Insert NDP alignment. */
936 ntb_iter = (void *) skb_put(skb2, ndp_pad);
937 memset(ntb_iter, 0, ndp_pad);
938
939 /* Copy NTB across. */
940 ntb_iter = (void *) skb_put(skb2, ncm->skb_tx_ndp->len);
941 memcpy(ntb_iter, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
942 dev_kfree_skb_any(ncm->skb_tx_ndp);
943 ncm->skb_tx_ndp = NULL;
944
945 /* Insert zero'd datagram. */
946 ntb_iter = (void *) skb_put(skb2, dgram_idx_len);
947 memset(ntb_iter, 0, dgram_idx_len);
948
949 return skb2;
950}
951
9f6ce424
YK
952static struct sk_buff *ncm_wrap_ntb(struct gether *port,
953 struct sk_buff *skb)
954{
955 struct f_ncm *ncm = func_to_ncm(&port->func);
6d3865f9 956 struct sk_buff *skb2 = NULL;
9f6ce424 957 int ncb_len = 0;
6d3865f9
JB
958 __le16 *ntb_data;
959 __le16 *ntb_ndp;
960 int dgram_pad;
961
9f6ce424 962 unsigned max_size = ncm->port.fixed_in_len;
8d640ad3 963 const struct ndp_parser_opts *opts = ncm->parser_opts;
6d3865f9
JB
964 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
965 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
966 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
967 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
9f6ce424 968
6d3865f9 969 if (!skb && !ncm->skb_tx_data)
9f6ce424 970 return NULL;
9f6ce424 971
6d3865f9
JB
972 if (skb) {
973 /* Add the CRC if required up front */
974 if (ncm->is_crc) {
975 uint32_t crc;
976 __le16 *crc_pos;
977
978 crc = ~crc32_le(~0,
979 skb->data,
980 skb->len);
981 crc_pos = (void *) skb_put(skb, sizeof(uint32_t));
982 put_unaligned_le32(crc, crc_pos);
983 }
9f6ce424 984
6d3865f9
JB
985 /* If the new skb is too big for the current NCM NTB then
986 * set the current stored skb to be sent now and clear it
987 * ready for new data.
988 * NOTE: Assume maximum align for speed of calculation.
989 */
990 if (ncm->skb_tx_data
991 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
992 || (ncm->skb_tx_data->len +
993 div + rem + skb->len +
994 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
995 > max_size)) {
996 skb2 = package_for_tx(ncm);
997 if (!skb2)
998 goto err;
999 }
9f6ce424 1000
6d3865f9
JB
1001 if (!ncm->skb_tx_data) {
1002 ncb_len = opts->nth_size;
1003 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1004 ncb_len += dgram_pad;
9f6ce424 1005
6d3865f9
JB
1006 /* Create a new skb for the NTH and datagrams. */
1007 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1008 if (!ncm->skb_tx_data)
1009 goto err;
9f6ce424 1010
6d3865f9
JB
1011 ntb_data = (void *) skb_put(ncm->skb_tx_data, ncb_len);
1012 memset(ntb_data, 0, ncb_len);
1013 /* dwSignature */
1014 put_unaligned_le32(opts->nth_sign, ntb_data);
1015 ntb_data += 2;
1016 /* wHeaderLength */
1017 put_unaligned_le16(opts->nth_size, ntb_data++);
1018
1019 /* Allocate an skb for storing the NDP,
1020 * TX_MAX_NUM_DPE should easily suffice for a
1021 * 16k packet.
1022 */
1023 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1024 + opts->dpe_size
1025 * TX_MAX_NUM_DPE),
1026 GFP_ATOMIC);
1027 if (!ncm->skb_tx_ndp)
1028 goto err;
1029 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp,
1030 opts->ndp_size);
1031 memset(ntb_ndp, 0, ncb_len);
1032 /* dwSignature */
1033 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1034 ntb_ndp += 2;
9f6ce424 1035
6d3865f9
JB
1036 /* There is always a zeroed entry */
1037 ncm->ndp_dgram_count = 1;
9f6ce424 1038
6d3865f9
JB
1039 /* Note: we skip opts->next_ndp_index */
1040 }
9f6ce424 1041
6d3865f9
JB
1042 /* Delay the timer. */
1043 hrtimer_start(&ncm->task_timer,
1044 ktime_set(0, TX_TIMEOUT_NSECS),
1045 HRTIMER_MODE_REL);
1046
1047 /* Add the datagram position entries */
1048 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp, dgram_idx_len);
1049 memset(ntb_ndp, 0, dgram_idx_len);
1050
1051 ncb_len = ncm->skb_tx_data->len;
1052 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1053 ncb_len += dgram_pad;
1054
1055 /* (d)wDatagramIndex */
1056 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1057 /* (d)wDatagramLength */
1058 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1059 ncm->ndp_dgram_count++;
1060
1061 /* Add the new data to the skb */
1062 ntb_data = (void *) skb_put(ncm->skb_tx_data, dgram_pad);
1063 memset(ntb_data, 0, dgram_pad);
1064 ntb_data = (void *) skb_put(ncm->skb_tx_data, skb->len);
1065 memcpy(ntb_data, skb->data, skb->len);
1066 dev_kfree_skb_any(skb);
1067 skb = NULL;
9f6ce424 1068
6d3865f9
JB
1069 } else if (ncm->skb_tx_data && ncm->timer_force_tx) {
1070 /* If the tx was requested because of a timeout then send */
1071 skb2 = package_for_tx(ncm);
1072 if (!skb2)
1073 goto err;
9f6ce424
YK
1074 }
1075
6d3865f9
JB
1076 return skb2;
1077
1078err:
1079 ncm->netdev->stats.tx_dropped++;
1080
1081 if (skb)
1082 dev_kfree_skb_any(skb);
1083 if (ncm->skb_tx_data)
1084 dev_kfree_skb_any(ncm->skb_tx_data);
1085 if (ncm->skb_tx_ndp)
1086 dev_kfree_skb_any(ncm->skb_tx_ndp);
1087
1088 return NULL;
1089}
1090
1091/*
1092 * This transmits the NTB if there are frames waiting.
1093 */
1094static void ncm_tx_tasklet(unsigned long data)
1095{
1096 struct f_ncm *ncm = (void *)data;
9f6ce424 1097
6d3865f9
JB
1098 if (ncm->timer_stopping)
1099 return;
1100
1101 /* Only send if data is available. */
1102 if (ncm->skb_tx_data) {
1103 ncm->timer_force_tx = true;
1104 ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
1105 ncm->timer_force_tx = false;
1106 }
1107}
9f6ce424 1108
6d3865f9
JB
1109/*
1110 * The transmit should only be run if no skb data has been sent
1111 * for a certain duration.
1112 */
1113static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1114{
1115 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1116 tasklet_schedule(&ncm->tx_tasklet);
1117 return HRTIMER_NORESTART;
9f6ce424
YK
1118}
1119
1120static int ncm_unwrap_ntb(struct gether *port,
1121 struct sk_buff *skb,
1122 struct sk_buff_head *list)
1123{
1124 struct f_ncm *ncm = func_to_ncm(&port->func);
1125 __le16 *tmp = (void *) skb->data;
1126 unsigned index, index2;
370af734 1127 int ndp_index;
9f6ce424
YK
1128 unsigned dg_len, dg_len2;
1129 unsigned ndp_len;
1130 struct sk_buff *skb2;
1131 int ret = -EINVAL;
1132 unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
8d640ad3 1133 const struct ndp_parser_opts *opts = ncm->parser_opts;
9f6ce424
YK
1134 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1135 int dgram_counter;
1136
1137 /* dwSignature */
1138 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1139 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1140 skb->len);
1141 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1142 skb->data, 32, false);
1143
1144 goto err;
1145 }
1146 tmp += 2;
1147 /* wHeaderLength */
1148 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1149 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1150 goto err;
1151 }
1152 tmp++; /* skip wSequence */
1153
1154 /* (d)wBlockLength */
1155 if (get_ncm(&tmp, opts->block_length) > max_size) {
1156 INFO(port->func.config->cdev, "OUT size exceeded\n");
1157 goto err;
1158 }
1159
6d3865f9 1160 ndp_index = get_ncm(&tmp, opts->ndp_index);
9f6ce424 1161
370af734 1162 /* Run through all the NDP's in the NTB */
9f6ce424 1163 do {
370af734
JB
1164 /* NCM 3.2 */
1165 if (((ndp_index % 4) != 0) &&
1166 (ndp_index < opts->nth_size)) {
1167 INFO(port->func.config->cdev, "Bad index: %#X\n",
1168 ndp_index);
9f6ce424
YK
1169 goto err;
1170 }
370af734
JB
1171
1172 /* walk through NDP */
1173 tmp = (void *)(skb->data + ndp_index);
1174 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1175 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1176 goto err;
9f6ce424 1177 }
370af734 1178 tmp += 2;
9f6ce424 1179
370af734
JB
1180 ndp_len = get_unaligned_le16(tmp++);
1181 /*
1182 * NCM 3.3.1
1183 * entry is 2 items
1184 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1185 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1186 * Each entry is a dgram index and a dgram length.
1187 */
1188 if ((ndp_len < opts->ndp_size
1189 + 2 * 2 * (opts->dgram_item_len * 2))
1190 || (ndp_len % opts->ndplen_align != 0)) {
1191 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1192 ndp_len);
1193 goto err;
1194 }
1195 tmp += opts->reserved1;
1196 /* Check for another NDP (d)wNextNdpIndex */
6d3865f9 1197 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
370af734
JB
1198 tmp += opts->reserved2;
1199
1200 ndp_len -= opts->ndp_size;
9f6ce424
YK
1201 index2 = get_ncm(&tmp, opts->dgram_item_len);
1202 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
370af734
JB
1203 dgram_counter = 0;
1204
1205 do {
1206 index = index2;
1207 dg_len = dg_len2;
1208 if (dg_len < 14 + crc_len) { /* ethernet hdr + crc */
1209 INFO(port->func.config->cdev,
1210 "Bad dgram length: %#X\n", dg_len);
1211 goto err;
1212 }
1213 if (ncm->is_crc) {
1214 uint32_t crc, crc2;
1215
1216 crc = get_unaligned_le32(skb->data +
1217 index + dg_len -
1218 crc_len);
1219 crc2 = ~crc32_le(~0,
1220 skb->data + index,
1221 dg_len - crc_len);
1222 if (crc != crc2) {
1223 INFO(port->func.config->cdev,
1224 "Bad CRC\n");
1225 goto err;
1226 }
1227 }
1228
1229 index2 = get_ncm(&tmp, opts->dgram_item_len);
1230 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
9f6ce424 1231
9f6ce424
YK
1232 skb2 = skb_clone(skb, GFP_ATOMIC);
1233 if (skb2 == NULL)
1234 goto err;
9f6ce424 1235
370af734
JB
1236 if (!skb_pull(skb2, index)) {
1237 ret = -EOVERFLOW;
1238 goto err;
1239 }
9f6ce424 1240
370af734
JB
1241 skb_trim(skb2, dg_len - crc_len);
1242 skb_queue_tail(list, skb2);
9f6ce424 1243
370af734 1244 ndp_len -= 2 * (opts->dgram_item_len * 2);
9f6ce424 1245
370af734 1246 dgram_counter++;
9f6ce424 1247
370af734
JB
1248 if (index2 == 0 || dg_len2 == 0)
1249 break;
1250 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1251 } while (ndp_index);
1252
1253 dev_kfree_skb_any(skb);
9f6ce424
YK
1254
1255 VDBG(port->func.config->cdev,
1256 "Parsed NTB with %d frames\n", dgram_counter);
1257 return 0;
1258err:
1259 skb_queue_purge(list);
1260 dev_kfree_skb_any(skb);
1261 return ret;
1262}
1263
1264static void ncm_disable(struct usb_function *f)
1265{
1266 struct f_ncm *ncm = func_to_ncm(f);
1267 struct usb_composite_dev *cdev = f->config->cdev;
1268
1269 DBG(cdev, "ncm deactivated\n");
1270
6d3865f9
JB
1271 if (ncm->port.in_ep->driver_data) {
1272 ncm->timer_stopping = true;
1273 ncm->netdev = NULL;
9f6ce424 1274 gether_disconnect(&ncm->port);
6d3865f9 1275 }
9f6ce424
YK
1276
1277 if (ncm->notify->driver_data) {
1278 usb_ep_disable(ncm->notify);
1279 ncm->notify->driver_data = NULL;
72c973dd 1280 ncm->notify->desc = NULL;
9f6ce424
YK
1281 }
1282}
1283
1284/*-------------------------------------------------------------------------*/
1285
1286/*
1287 * Callbacks let us notify the host about connect/disconnect when the
1288 * net device is opened or closed.
1289 *
1290 * For testing, note that link states on this side include both opened
1291 * and closed variants of:
1292 *
1293 * - disconnected/unconfigured
1294 * - configured but inactive (data alt 0)
1295 * - configured and active (data alt 1)
1296 *
1297 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1298 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1299 * imply the host is actually polling the notification endpoint, and
1300 * likewise that "active" doesn't imply it's actually using the data
1301 * endpoints for traffic.
1302 */
1303
1304static void ncm_open(struct gether *geth)
1305{
1306 struct f_ncm *ncm = func_to_ncm(&geth->func);
1307
1308 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1309
1310 spin_lock(&ncm->lock);
1311 ncm->is_open = true;
1312 ncm_notify(ncm);
1313 spin_unlock(&ncm->lock);
1314}
1315
1316static void ncm_close(struct gether *geth)
1317{
1318 struct f_ncm *ncm = func_to_ncm(&geth->func);
1319
1320 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1321
1322 spin_lock(&ncm->lock);
1323 ncm->is_open = false;
1324 ncm_notify(ncm);
1325 spin_unlock(&ncm->lock);
1326}
1327
1328/*-------------------------------------------------------------------------*/
1329
1330/* ethernet function driver setup/binding */
1331
40d133d7 1332static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
9f6ce424
YK
1333{
1334 struct usb_composite_dev *cdev = c->cdev;
1335 struct f_ncm *ncm = func_to_ncm(f);
8feffd00 1336 struct usb_string *us;
9f6ce424
YK
1337 int status;
1338 struct usb_ep *ep;
40d133d7
AP
1339 struct f_ncm_opts *ncm_opts;
1340
1341 if (!can_support_ecm(cdev->gadget))
1342 return -EINVAL;
1343
1344 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1345 /*
1346 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1347 * configurations are bound in sequence with list_for_each_entry,
1348 * in each configuration its functions are bound in sequence
1349 * with list_for_each_entry, so we assume no race condition
1350 * with regard to ncm_opts->bound access
1351 */
1352 if (!ncm_opts->bound) {
e7306603 1353 mutex_lock(&ncm_opts->lock);
40d133d7
AP
1354 gether_set_gadget(ncm_opts->net, cdev->gadget);
1355 status = gether_register_netdev(ncm_opts->net);
e7306603 1356 mutex_unlock(&ncm_opts->lock);
40d133d7
AP
1357 if (status)
1358 return status;
1359 ncm_opts->bound = true;
1360 }
8feffd00
AP
1361 us = usb_gstrings_attach(cdev, ncm_strings,
1362 ARRAY_SIZE(ncm_string_defs));
1363 if (IS_ERR(us))
1364 return PTR_ERR(us);
1365 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1366 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1367 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1368 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1369 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
40d133d7 1370
9f6ce424
YK
1371 /* allocate instance-specific interface IDs */
1372 status = usb_interface_id(c, f);
1373 if (status < 0)
1374 goto fail;
1375 ncm->ctrl_id = status;
1376 ncm_iad_desc.bFirstInterface = status;
1377
1378 ncm_control_intf.bInterfaceNumber = status;
1379 ncm_union_desc.bMasterInterface0 = status;
1380
1381 status = usb_interface_id(c, f);
1382 if (status < 0)
1383 goto fail;
1384 ncm->data_id = status;
1385
1386 ncm_data_nop_intf.bInterfaceNumber = status;
1387 ncm_data_intf.bInterfaceNumber = status;
1388 ncm_union_desc.bSlaveInterface0 = status;
1389
1390 status = -ENODEV;
1391
1392 /* allocate instance-specific endpoints */
1393 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1394 if (!ep)
1395 goto fail;
1396 ncm->port.in_ep = ep;
1397 ep->driver_data = cdev; /* claim */
1398
1399 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1400 if (!ep)
1401 goto fail;
1402 ncm->port.out_ep = ep;
1403 ep->driver_data = cdev; /* claim */
1404
1405 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1406 if (!ep)
1407 goto fail;
1408 ncm->notify = ep;
1409 ep->driver_data = cdev; /* claim */
1410
1411 status = -ENOMEM;
1412
1413 /* allocate notification request and buffer */
1414 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1415 if (!ncm->notify_req)
1416 goto fail;
1417 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1418 if (!ncm->notify_req->buf)
1419 goto fail;
1420 ncm->notify_req->context = ncm;
1421 ncm->notify_req->complete = ncm_notify_complete;
1422
9f6ce424
YK
1423 /*
1424 * support all relevant hardware speeds... we expect that when
1425 * hardware is dual speed, all bulk-capable endpoints work at
1426 * both speeds
1427 */
10287bae
SAS
1428 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1429 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1430 hs_ncm_notify_desc.bEndpointAddress =
1431 fs_ncm_notify_desc.bEndpointAddress;
9f6ce424 1432
10287bae
SAS
1433 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1434 NULL);
9f6ce424
YK
1435 /*
1436 * NOTE: all that is done without knowing or caring about
1437 * the network link ... which is unavailable to this code
1438 * until we're activated via set_alt().
1439 */
1440
1441 ncm->port.open = ncm_open;
1442 ncm->port.close = ncm_close;
1443
6d3865f9
JB
1444 tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
1445 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1446 ncm->task_timer.function = ncm_tx_timeout;
1447
9f6ce424
YK
1448 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1449 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1450 ncm->port.in_ep->name, ncm->port.out_ep->name,
1451 ncm->notify->name);
1452 return 0;
1453
1454fail:
10287bae 1455 usb_free_all_descriptors(f);
9f6ce424
YK
1456 if (ncm->notify_req) {
1457 kfree(ncm->notify_req->buf);
1458 usb_ep_free_request(ncm->notify, ncm->notify_req);
1459 }
1460
1461 /* we might as well release our claims on endpoints */
1462 if (ncm->notify)
1463 ncm->notify->driver_data = NULL;
e79cc615 1464 if (ncm->port.out_ep)
9f6ce424 1465 ncm->port.out_ep->driver_data = NULL;
e79cc615 1466 if (ncm->port.in_ep)
9f6ce424
YK
1467 ncm->port.in_ep->driver_data = NULL;
1468
1469 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1470
1471 return status;
1472}
1473
e7306603
AP
1474static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1475{
1476 return container_of(to_config_group(item), struct f_ncm_opts,
1477 func_inst.group);
1478}
1479
aa83c6ad
AP
1480/* f_ncm_item_ops */
1481USB_ETHERNET_CONFIGFS_ITEM(ncm);
e7306603 1482
aa83c6ad
AP
1483/* f_ncm_opts_dev_addr */
1484USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
e7306603 1485
aa83c6ad
AP
1486/* f_ncm_opts_host_addr */
1487USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
e7306603 1488
aa83c6ad
AP
1489/* f_ncm_opts_qmult */
1490USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
e7306603 1491
aa83c6ad
AP
1492/* f_ncm_opts_ifname */
1493USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
e7306603
AP
1494
1495static struct configfs_attribute *ncm_attrs[] = {
1496 &f_ncm_opts_dev_addr.attr,
1497 &f_ncm_opts_host_addr.attr,
1498 &f_ncm_opts_qmult.attr,
1499 &f_ncm_opts_ifname.attr,
1500 NULL,
1501};
1502
1503static struct config_item_type ncm_func_type = {
1504 .ct_item_ops = &ncm_item_ops,
1505 .ct_attrs = ncm_attrs,
1506 .ct_owner = THIS_MODULE,
1507};
1508
40d133d7
AP
1509static void ncm_free_inst(struct usb_function_instance *f)
1510{
1511 struct f_ncm_opts *opts;
1512
1513 opts = container_of(f, struct f_ncm_opts, func_inst);
1514 if (opts->bound)
1515 gether_cleanup(netdev_priv(opts->net));
1516 else
1517 free_netdev(opts->net);
1518 kfree(opts);
1519}
1520
1521static struct usb_function_instance *ncm_alloc_inst(void)
1522{
1523 struct f_ncm_opts *opts;
1524
1525 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1526 if (!opts)
1527 return ERR_PTR(-ENOMEM);
e7306603 1528 mutex_init(&opts->lock);
40d133d7
AP
1529 opts->func_inst.free_func_inst = ncm_free_inst;
1530 opts->net = gether_setup_default();
172d934c
AP
1531 if (IS_ERR(opts->net)) {
1532 struct net_device *net = opts->net;
1533 kfree(opts);
1534 return ERR_CAST(net);
1535 }
40d133d7 1536
e7306603
AP
1537 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1538
40d133d7
AP
1539 return &opts->func_inst;
1540}
1541
1542static void ncm_free(struct usb_function *f)
1543{
1544 struct f_ncm *ncm;
e7306603 1545 struct f_ncm_opts *opts;
40d133d7
AP
1546
1547 ncm = func_to_ncm(f);
e7306603 1548 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
40d133d7 1549 kfree(ncm);
e7306603
AP
1550 mutex_lock(&opts->lock);
1551 opts->refcnt--;
1552 mutex_unlock(&opts->lock);
40d133d7
AP
1553}
1554
1555static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1556{
1557 struct f_ncm *ncm = func_to_ncm(f);
1558
1559 DBG(c->cdev, "ncm unbind\n");
1560
6d3865f9
JB
1561 hrtimer_cancel(&ncm->task_timer);
1562 tasklet_kill(&ncm->tx_tasklet);
1563
1564 ncm_string_defs[0].id = 0;
40d133d7
AP
1565 usb_free_all_descriptors(f);
1566
1567 kfree(ncm->notify_req->buf);
1568 usb_ep_free_request(ncm->notify, ncm->notify_req);
1569}
1570
36a61125 1571static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
40d133d7
AP
1572{
1573 struct f_ncm *ncm;
1574 struct f_ncm_opts *opts;
1575 int status;
1576
1577 /* allocate and initialize one new instance */
1578 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1579 if (!ncm)
1580 return ERR_PTR(-ENOMEM);
1581
1582 opts = container_of(fi, struct f_ncm_opts, func_inst);
e7306603
AP
1583 mutex_lock(&opts->lock);
1584 opts->refcnt++;
40d133d7
AP
1585
1586 /* export host's Ethernet address in CDC format */
1587 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1588 sizeof(ncm->ethaddr));
1589 if (status < 12) { /* strlen("01234567890a") */
1590 kfree(ncm);
c92834c1 1591 mutex_unlock(&opts->lock);
40d133d7
AP
1592 return ERR_PTR(-EINVAL);
1593 }
1594 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1595
1596 spin_lock_init(&ncm->lock);
1597 ncm_reset_values(ncm);
1598 ncm->port.ioport = netdev_priv(opts->net);
e7306603 1599 mutex_unlock(&opts->lock);
40d133d7 1600 ncm->port.is_fixed = true;
6d3865f9 1601 ncm->port.supports_multi_frame = true;
40d133d7
AP
1602
1603 ncm->port.func.name = "cdc_network";
40d133d7
AP
1604 /* descriptors are per-instance copies */
1605 ncm->port.func.bind = ncm_bind;
1606 ncm->port.func.unbind = ncm_unbind;
1607 ncm->port.func.set_alt = ncm_set_alt;
1608 ncm->port.func.get_alt = ncm_get_alt;
1609 ncm->port.func.setup = ncm_setup;
1610 ncm->port.func.disable = ncm_disable;
1611 ncm->port.func.free_func = ncm_free;
1612
1613 ncm->port.wrap = ncm_wrap_ntb;
1614 ncm->port.unwrap = ncm_unwrap_ntb;
1615
1616 return &ncm->port.func;
1617}
1618
1619DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1620MODULE_LICENSE("GPL");
1621MODULE_AUTHOR("Yauheni Kaliuta");