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