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