1 // SPDX-License-Identifier: GPL-2.0+
3 * f_ncm.c -- USB CDC Network (NCM) link function driver
5 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
8 * The driver borrows from f_ecm.c which is:
10 * Copyright (C) 2003-2005,2008 David Brownell
11 * Copyright (C) 2008 Nokia Corporation
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>
21 #include <linux/usb/cdc.h>
24 #include "u_ether_configfs.h"
28 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
29 * NCM is intended to be used with high-speed network attachments.
31 * Note that NCM requires the use of "alternate settings" for its data
32 * interface. This means that the set_alt() method has real work to do,
33 * and also means that a get_alt() method is required.
36 /* to trigger crc/non-crc ndp signature */
38 #define NCM_NDP_HDR_CRC_MASK 0x01000000
39 #define NCM_NDP_HDR_CRC 0x01000000
40 #define NCM_NDP_HDR_NOCRC 0x00000000
42 enum ncm_notify_state
{
43 NCM_NOTIFY_NONE
, /* don't notify */
44 NCM_NOTIFY_CONNECT
, /* issue CONNECT next */
45 NCM_NOTIFY_SPEED
, /* issue SPEED_CHANGE next */
54 struct usb_ep
*notify
;
55 struct usb_request
*notify_req
;
59 const struct ndp_parser_opts
*parser_opts
;
64 * for notification, it is accessed from both
65 * callback and ethernet open/close
69 struct net_device
*netdev
;
71 /* For multi-frame NDP TX */
72 struct sk_buff
*skb_tx_data
;
73 struct sk_buff
*skb_tx_ndp
;
76 struct tasklet_struct tx_tasklet
;
77 struct hrtimer task_timer
;
82 static inline struct f_ncm
*func_to_ncm(struct usb_function
*f
)
84 return container_of(f
, struct f_ncm
, port
.func
);
87 /* peak (theoretical) bulk transfer rate in bits-per-second */
88 static inline unsigned ncm_bitrate(struct usb_gadget
*g
)
90 if (gadget_is_superspeed(g
) && g
->speed
== USB_SPEED_SUPER
)
91 return 13 * 1024 * 8 * 1000 * 8;
92 else if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
93 return 13 * 512 * 8 * 1000 * 8;
95 return 19 * 64 * 1 * 1000 * 8;
98 /*-------------------------------------------------------------------------*/
101 * We cannot group frames so use just the minimal size which ok to put
102 * one max-size ethernet frame.
103 * If the host can group frames, allow it to do that, 16K is selected,
104 * because it's used by default by the current linux host driver
106 #define NTB_DEFAULT_IN_SIZE 16384
107 #define NTB_OUT_SIZE 16384
109 /* Allocation for storing the NDP, 32 should suffice for a
110 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
111 * be transmitted in a single 16kB skb, though when sending full size
112 * packets this limit will be plenty.
113 * Smaller packets are not likely to be trying to maximize the
114 * throughput and will be mstly sending smaller infrequent frames.
116 #define TX_MAX_NUM_DPE 32
118 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
119 #define TX_TIMEOUT_NSECS 300000
121 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
122 USB_CDC_NCM_NTB32_SUPPORTED)
124 static struct usb_cdc_ncm_ntb_parameters ntb_parameters
= {
125 .wLength
= cpu_to_le16(sizeof(ntb_parameters
)),
126 .bmNtbFormatsSupported
= cpu_to_le16(FORMATS_SUPPORTED
),
127 .dwNtbInMaxSize
= cpu_to_le32(NTB_DEFAULT_IN_SIZE
),
128 .wNdpInDivisor
= cpu_to_le16(4),
129 .wNdpInPayloadRemainder
= cpu_to_le16(0),
130 .wNdpInAlignment
= cpu_to_le16(4),
132 .dwNtbOutMaxSize
= cpu_to_le32(NTB_OUT_SIZE
),
133 .wNdpOutDivisor
= cpu_to_le16(4),
134 .wNdpOutPayloadRemainder
= cpu_to_le16(0),
135 .wNdpOutAlignment
= cpu_to_le16(4),
139 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
140 * packet, to simplify cancellation; and a big transfer interval, to
141 * waste less bandwidth.
144 #define NCM_STATUS_INTERVAL_MS 32
145 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
147 static struct usb_interface_assoc_descriptor ncm_iad_desc
= {
148 .bLength
= sizeof ncm_iad_desc
,
149 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
151 /* .bFirstInterface = DYNAMIC, */
152 .bInterfaceCount
= 2, /* control + data */
153 .bFunctionClass
= USB_CLASS_COMM
,
154 .bFunctionSubClass
= USB_CDC_SUBCLASS_NCM
,
155 .bFunctionProtocol
= USB_CDC_PROTO_NONE
,
156 /* .iFunction = DYNAMIC */
159 /* interface descriptor: */
161 static struct usb_interface_descriptor ncm_control_intf
= {
162 .bLength
= sizeof ncm_control_intf
,
163 .bDescriptorType
= USB_DT_INTERFACE
,
165 /* .bInterfaceNumber = DYNAMIC */
167 .bInterfaceClass
= USB_CLASS_COMM
,
168 .bInterfaceSubClass
= USB_CDC_SUBCLASS_NCM
,
169 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
170 /* .iInterface = DYNAMIC */
173 static struct usb_cdc_header_desc ncm_header_desc
= {
174 .bLength
= sizeof ncm_header_desc
,
175 .bDescriptorType
= USB_DT_CS_INTERFACE
,
176 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
178 .bcdCDC
= cpu_to_le16(0x0110),
181 static struct usb_cdc_union_desc ncm_union_desc
= {
182 .bLength
= sizeof(ncm_union_desc
),
183 .bDescriptorType
= USB_DT_CS_INTERFACE
,
184 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
185 /* .bMasterInterface0 = DYNAMIC */
186 /* .bSlaveInterface0 = DYNAMIC */
189 static struct usb_cdc_ether_desc ecm_desc
= {
190 .bLength
= sizeof ecm_desc
,
191 .bDescriptorType
= USB_DT_CS_INTERFACE
,
192 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
194 /* this descriptor actually adds value, surprise! */
195 /* .iMACAddress = DYNAMIC */
196 .bmEthernetStatistics
= cpu_to_le32(0), /* no statistics */
197 .wMaxSegmentSize
= cpu_to_le16(ETH_FRAME_LEN
),
198 .wNumberMCFilters
= cpu_to_le16(0),
199 .bNumberPowerFilters
= 0,
202 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
204 static struct usb_cdc_ncm_desc ncm_desc
= {
205 .bLength
= sizeof ncm_desc
,
206 .bDescriptorType
= USB_DT_CS_INTERFACE
,
207 .bDescriptorSubType
= USB_CDC_NCM_TYPE
,
209 .bcdNcmVersion
= cpu_to_le16(0x0100),
210 /* can process SetEthernetPacketFilter */
211 .bmNetworkCapabilities
= NCAPS
,
214 /* the default data interface has no endpoints ... */
216 static struct usb_interface_descriptor ncm_data_nop_intf
= {
217 .bLength
= sizeof ncm_data_nop_intf
,
218 .bDescriptorType
= USB_DT_INTERFACE
,
220 .bInterfaceNumber
= 1,
221 .bAlternateSetting
= 0,
223 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
224 .bInterfaceSubClass
= 0,
225 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
226 /* .iInterface = DYNAMIC */
229 /* ... but the "real" data interface has two bulk endpoints */
231 static struct usb_interface_descriptor ncm_data_intf
= {
232 .bLength
= sizeof ncm_data_intf
,
233 .bDescriptorType
= USB_DT_INTERFACE
,
235 .bInterfaceNumber
= 1,
236 .bAlternateSetting
= 1,
238 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
239 .bInterfaceSubClass
= 0,
240 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
241 /* .iInterface = DYNAMIC */
244 /* full speed support: */
246 static struct usb_endpoint_descriptor fs_ncm_notify_desc
= {
247 .bLength
= USB_DT_ENDPOINT_SIZE
,
248 .bDescriptorType
= USB_DT_ENDPOINT
,
250 .bEndpointAddress
= USB_DIR_IN
,
251 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
252 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
253 .bInterval
= NCM_STATUS_INTERVAL_MS
,
256 static struct usb_endpoint_descriptor fs_ncm_in_desc
= {
257 .bLength
= USB_DT_ENDPOINT_SIZE
,
258 .bDescriptorType
= USB_DT_ENDPOINT
,
260 .bEndpointAddress
= USB_DIR_IN
,
261 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
264 static struct usb_endpoint_descriptor fs_ncm_out_desc
= {
265 .bLength
= USB_DT_ENDPOINT_SIZE
,
266 .bDescriptorType
= USB_DT_ENDPOINT
,
268 .bEndpointAddress
= USB_DIR_OUT
,
269 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
272 static struct usb_descriptor_header
*ncm_fs_function
[] = {
273 (struct usb_descriptor_header
*) &ncm_iad_desc
,
274 /* CDC NCM control descriptors */
275 (struct usb_descriptor_header
*) &ncm_control_intf
,
276 (struct usb_descriptor_header
*) &ncm_header_desc
,
277 (struct usb_descriptor_header
*) &ncm_union_desc
,
278 (struct usb_descriptor_header
*) &ecm_desc
,
279 (struct usb_descriptor_header
*) &ncm_desc
,
280 (struct usb_descriptor_header
*) &fs_ncm_notify_desc
,
281 /* data interface, altsettings 0 and 1 */
282 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
283 (struct usb_descriptor_header
*) &ncm_data_intf
,
284 (struct usb_descriptor_header
*) &fs_ncm_in_desc
,
285 (struct usb_descriptor_header
*) &fs_ncm_out_desc
,
289 /* high speed support: */
291 static struct usb_endpoint_descriptor hs_ncm_notify_desc
= {
292 .bLength
= USB_DT_ENDPOINT_SIZE
,
293 .bDescriptorType
= USB_DT_ENDPOINT
,
295 .bEndpointAddress
= USB_DIR_IN
,
296 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
297 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
298 .bInterval
= USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS
),
300 static struct usb_endpoint_descriptor hs_ncm_in_desc
= {
301 .bLength
= USB_DT_ENDPOINT_SIZE
,
302 .bDescriptorType
= USB_DT_ENDPOINT
,
304 .bEndpointAddress
= USB_DIR_IN
,
305 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
306 .wMaxPacketSize
= cpu_to_le16(512),
309 static struct usb_endpoint_descriptor hs_ncm_out_desc
= {
310 .bLength
= USB_DT_ENDPOINT_SIZE
,
311 .bDescriptorType
= USB_DT_ENDPOINT
,
313 .bEndpointAddress
= USB_DIR_OUT
,
314 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
315 .wMaxPacketSize
= cpu_to_le16(512),
318 static struct usb_descriptor_header
*ncm_hs_function
[] = {
319 (struct usb_descriptor_header
*) &ncm_iad_desc
,
320 /* CDC NCM control descriptors */
321 (struct usb_descriptor_header
*) &ncm_control_intf
,
322 (struct usb_descriptor_header
*) &ncm_header_desc
,
323 (struct usb_descriptor_header
*) &ncm_union_desc
,
324 (struct usb_descriptor_header
*) &ecm_desc
,
325 (struct usb_descriptor_header
*) &ncm_desc
,
326 (struct usb_descriptor_header
*) &hs_ncm_notify_desc
,
327 /* data interface, altsettings 0 and 1 */
328 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
329 (struct usb_descriptor_header
*) &ncm_data_intf
,
330 (struct usb_descriptor_header
*) &hs_ncm_in_desc
,
331 (struct usb_descriptor_header
*) &hs_ncm_out_desc
,
336 /* super speed support: */
338 static struct usb_endpoint_descriptor ss_ncm_notify_desc
= {
339 .bLength
= USB_DT_ENDPOINT_SIZE
,
340 .bDescriptorType
= USB_DT_ENDPOINT
,
342 .bEndpointAddress
= USB_DIR_IN
,
343 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
344 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
345 .bInterval
= USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS
)
348 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc
= {
349 .bLength
= sizeof(ss_ncm_notify_comp_desc
),
350 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
352 /* the following 3 values can be tweaked if necessary */
353 /* .bMaxBurst = 0, */
354 /* .bmAttributes = 0, */
355 .wBytesPerInterval
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
358 static struct usb_endpoint_descriptor ss_ncm_in_desc
= {
359 .bLength
= USB_DT_ENDPOINT_SIZE
,
360 .bDescriptorType
= USB_DT_ENDPOINT
,
362 .bEndpointAddress
= USB_DIR_IN
,
363 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
364 .wMaxPacketSize
= cpu_to_le16(1024),
367 static struct usb_endpoint_descriptor ss_ncm_out_desc
= {
368 .bLength
= USB_DT_ENDPOINT_SIZE
,
369 .bDescriptorType
= USB_DT_ENDPOINT
,
371 .bEndpointAddress
= USB_DIR_OUT
,
372 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
373 .wMaxPacketSize
= cpu_to_le16(1024),
376 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc
= {
377 .bLength
= sizeof(ss_ncm_bulk_comp_desc
),
378 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
380 /* the following 2 values can be tweaked if necessary */
381 /* .bMaxBurst = 0, */
382 /* .bmAttributes = 0, */
385 static struct usb_descriptor_header
*ncm_ss_function
[] = {
386 (struct usb_descriptor_header
*) &ncm_iad_desc
,
387 /* CDC NCM control descriptors */
388 (struct usb_descriptor_header
*) &ncm_control_intf
,
389 (struct usb_descriptor_header
*) &ncm_header_desc
,
390 (struct usb_descriptor_header
*) &ncm_union_desc
,
391 (struct usb_descriptor_header
*) &ecm_desc
,
392 (struct usb_descriptor_header
*) &ncm_desc
,
393 (struct usb_descriptor_header
*) &ss_ncm_notify_desc
,
394 (struct usb_descriptor_header
*) &ss_ncm_notify_comp_desc
,
395 /* data interface, altsettings 0 and 1 */
396 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
397 (struct usb_descriptor_header
*) &ncm_data_intf
,
398 (struct usb_descriptor_header
*) &ss_ncm_in_desc
,
399 (struct usb_descriptor_header
*) &ss_ncm_bulk_comp_desc
,
400 (struct usb_descriptor_header
*) &ss_ncm_out_desc
,
401 (struct usb_descriptor_header
*) &ss_ncm_bulk_comp_desc
,
405 /* string descriptors: */
407 #define STRING_CTRL_IDX 0
408 #define STRING_MAC_IDX 1
409 #define STRING_DATA_IDX 2
410 #define STRING_IAD_IDX 3
412 static struct usb_string ncm_string_defs
[] = {
413 [STRING_CTRL_IDX
].s
= "CDC Network Control Model (NCM)",
414 [STRING_MAC_IDX
].s
= "",
415 [STRING_DATA_IDX
].s
= "CDC Network Data",
416 [STRING_IAD_IDX
].s
= "CDC NCM",
417 { } /* end of list */
420 static struct usb_gadget_strings ncm_string_table
= {
421 .language
= 0x0409, /* en-us */
422 .strings
= ncm_string_defs
,
425 static struct usb_gadget_strings
*ncm_strings
[] = {
431 * Here are options for NCM Datagram Pointer table (NDP) parser.
432 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
433 * in NDP16 offsets and sizes fields are 1 16bit word wide,
434 * in NDP32 -- 2 16bit words wide. Also signatures are different.
435 * To make the parser code the same, put the differences in the structure,
436 * and switch pointers to the structures when the format is changed.
439 struct ndp_parser_opts
{
445 unsigned ndplen_align
;
446 /* sizes in u16 units */
447 unsigned dgram_item_len
; /* index or length */
448 unsigned block_length
;
452 unsigned next_ndp_index
;
455 #define INIT_NDP16_OPTS { \
456 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
457 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
458 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
459 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
460 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
462 .dgram_item_len = 1, \
467 .next_ndp_index = 1, \
471 #define INIT_NDP32_OPTS { \
472 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
473 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
474 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
475 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
476 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
478 .dgram_item_len = 2, \
483 .next_ndp_index = 2, \
486 static const struct ndp_parser_opts ndp16_opts
= INIT_NDP16_OPTS
;
487 static const struct ndp_parser_opts ndp32_opts
= INIT_NDP32_OPTS
;
489 static inline void put_ncm(__le16
**p
, unsigned size
, unsigned val
)
493 put_unaligned_le16((u16
)val
, *p
);
496 put_unaligned_le32((u32
)val
, *p
);
506 static inline unsigned get_ncm(__le16
**p
, unsigned size
)
512 tmp
= get_unaligned_le16(*p
);
515 tmp
= get_unaligned_le32(*p
);
525 /*-------------------------------------------------------------------------*/
527 static inline void ncm_reset_values(struct f_ncm
*ncm
)
529 ncm
->parser_opts
= &ndp16_opts
;
531 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
533 /* doesn't make sense for ncm, fixed size used */
534 ncm
->port
.header_len
= 0;
536 ncm
->port
.fixed_out_len
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
537 ncm
->port
.fixed_in_len
= NTB_DEFAULT_IN_SIZE
;
541 * Context: ncm->lock held
543 static void ncm_do_notify(struct f_ncm
*ncm
)
545 struct usb_request
*req
= ncm
->notify_req
;
546 struct usb_cdc_notification
*event
;
547 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
551 /* notification already in flight? */
556 switch (ncm
->notify_state
) {
557 case NCM_NOTIFY_NONE
:
560 case NCM_NOTIFY_CONNECT
:
561 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
563 event
->wValue
= cpu_to_le16(1);
565 event
->wValue
= cpu_to_le16(0);
567 req
->length
= sizeof *event
;
569 DBG(cdev
, "notify connect %s\n",
570 ncm
->is_open
? "true" : "false");
571 ncm
->notify_state
= NCM_NOTIFY_NONE
;
574 case NCM_NOTIFY_SPEED
:
575 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
576 event
->wValue
= cpu_to_le16(0);
577 event
->wLength
= cpu_to_le16(8);
578 req
->length
= NCM_STATUS_BYTECOUNT
;
580 /* SPEED_CHANGE data is up/down speeds in bits/sec */
581 data
= req
->buf
+ sizeof *event
;
582 data
[0] = cpu_to_le32(ncm_bitrate(cdev
->gadget
));
585 DBG(cdev
, "notify speed %d\n", ncm_bitrate(cdev
->gadget
));
586 ncm
->notify_state
= NCM_NOTIFY_CONNECT
;
589 event
->bmRequestType
= 0xA1;
590 event
->wIndex
= cpu_to_le16(ncm
->ctrl_id
);
592 ncm
->notify_req
= NULL
;
594 * In double buffering if there is a space in FIFO,
595 * completion callback can be called right after the call,
598 spin_unlock(&ncm
->lock
);
599 status
= usb_ep_queue(ncm
->notify
, req
, GFP_ATOMIC
);
600 spin_lock(&ncm
->lock
);
602 ncm
->notify_req
= req
;
603 DBG(cdev
, "notify --> %d\n", status
);
608 * Context: ncm->lock held
610 static void ncm_notify(struct f_ncm
*ncm
)
613 * NOTE on most versions of Linux, host side cdc-ethernet
614 * won't listen for notifications until its netdevice opens.
615 * The first notification then sits in the FIFO for a long
616 * time, and the second one is queued.
618 * If ncm_notify() is called before the second (CONNECT)
619 * notification is sent, then it will reset to send the SPEED
620 * notificaion again (and again, and again), but it's not a problem
622 ncm
->notify_state
= NCM_NOTIFY_SPEED
;
626 static void ncm_notify_complete(struct usb_ep
*ep
, struct usb_request
*req
)
628 struct f_ncm
*ncm
= req
->context
;
629 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
630 struct usb_cdc_notification
*event
= req
->buf
;
632 spin_lock(&ncm
->lock
);
633 switch (req
->status
) {
635 VDBG(cdev
, "Notification %02x sent\n",
636 event
->bNotificationType
);
640 ncm
->notify_state
= NCM_NOTIFY_NONE
;
643 DBG(cdev
, "event %02x --> %d\n",
644 event
->bNotificationType
, req
->status
);
647 ncm
->notify_req
= req
;
649 spin_unlock(&ncm
->lock
);
652 static void ncm_ep0out_complete(struct usb_ep
*ep
, struct usb_request
*req
)
654 /* now for SET_NTB_INPUT_SIZE only */
656 struct usb_function
*f
= req
->context
;
657 struct f_ncm
*ncm
= func_to_ncm(f
);
658 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
661 if (req
->status
|| req
->actual
!= req
->length
) {
662 DBG(cdev
, "Bad control-OUT transfer\n");
666 in_size
= get_unaligned_le32(req
->buf
);
667 if (in_size
< USB_CDC_NCM_NTB_MIN_IN_SIZE
||
668 in_size
> le32_to_cpu(ntb_parameters
.dwNtbInMaxSize
)) {
669 DBG(cdev
, "Got wrong INPUT SIZE (%d) from host\n", in_size
);
673 ncm
->port
.fixed_in_len
= in_size
;
674 VDBG(cdev
, "Set NTB INPUT SIZE %d\n", in_size
);
682 static int ncm_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
684 struct f_ncm
*ncm
= func_to_ncm(f
);
685 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
686 struct usb_request
*req
= cdev
->req
;
687 int value
= -EOPNOTSUPP
;
688 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
689 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
690 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
693 * composite driver infrastructure handles everything except
694 * CDC class messages; interface activation uses set_alt().
696 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
697 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
698 | USB_CDC_SET_ETHERNET_PACKET_FILTER
:
700 * see 6.2.30: no data, wIndex = interface,
701 * wValue = packet filter bitmap
703 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
705 DBG(cdev
, "packet filter %02x\n", w_value
);
707 * REVISIT locking of cdc_filter. This assumes the UDC
708 * driver won't have a concurrent packet TX irq running on
709 * another CPU; or that if it does, this write is atomic...
711 ncm
->port
.cdc_filter
= w_value
;
716 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
717 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
718 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
719 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
720 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
721 * case USB_CDC_GET_ETHERNET_STATISTIC:
724 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
725 | USB_CDC_GET_NTB_PARAMETERS
:
727 if (w_length
== 0 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
729 value
= w_length
> sizeof ntb_parameters
?
730 sizeof ntb_parameters
: w_length
;
731 memcpy(req
->buf
, &ntb_parameters
, value
);
732 VDBG(cdev
, "Host asked NTB parameters\n");
735 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
736 | USB_CDC_GET_NTB_INPUT_SIZE
:
738 if (w_length
< 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
740 put_unaligned_le32(ncm
->port
.fixed_in_len
, req
->buf
);
742 VDBG(cdev
, "Host asked INPUT SIZE, sending %d\n",
743 ncm
->port
.fixed_in_len
);
746 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
747 | USB_CDC_SET_NTB_INPUT_SIZE
:
749 if (w_length
!= 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
751 req
->complete
= ncm_ep0out_complete
;
752 req
->length
= w_length
;
759 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
760 | USB_CDC_GET_NTB_FORMAT
:
764 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
766 format
= (ncm
->parser_opts
== &ndp16_opts
) ? 0x0000 : 0x0001;
767 put_unaligned_le16(format
, req
->buf
);
769 VDBG(cdev
, "Host asked NTB FORMAT, sending %d\n", format
);
773 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
774 | USB_CDC_SET_NTB_FORMAT
:
776 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
780 ncm
->parser_opts
= &ndp16_opts
;
781 DBG(cdev
, "NCM16 selected\n");
784 ncm
->parser_opts
= &ndp32_opts
;
785 DBG(cdev
, "NCM32 selected\n");
793 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
794 | USB_CDC_GET_CRC_MODE
:
798 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
800 is_crc
= ncm
->is_crc
? 0x0001 : 0x0000;
801 put_unaligned_le16(is_crc
, req
->buf
);
803 VDBG(cdev
, "Host asked CRC MODE, sending %d\n", is_crc
);
807 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
808 | USB_CDC_SET_CRC_MODE
:
812 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
817 ndp_hdr_crc
= NCM_NDP_HDR_NOCRC
;
818 DBG(cdev
, "non-CRC mode selected\n");
822 ndp_hdr_crc
= NCM_NDP_HDR_CRC
;
823 DBG(cdev
, "CRC mode selected\n");
828 ncm
->ndp_sign
= ncm
->parser_opts
->ndp_sign
| ndp_hdr_crc
;
833 /* and disabled in ncm descriptor: */
834 /* case USB_CDC_GET_NET_ADDRESS: */
835 /* case USB_CDC_SET_NET_ADDRESS: */
836 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
837 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
841 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
842 ctrl
->bRequestType
, ctrl
->bRequest
,
843 w_value
, w_index
, w_length
);
846 /* respond with data transfer or status phase? */
848 DBG(cdev
, "ncm req%02x.%02x v%04x i%04x l%d\n",
849 ctrl
->bRequestType
, ctrl
->bRequest
,
850 w_value
, w_index
, w_length
);
853 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
855 ERROR(cdev
, "ncm req %02x.%02x response err %d\n",
856 ctrl
->bRequestType
, ctrl
->bRequest
,
860 /* device either stalls (value < 0) or reports success */
865 static int ncm_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
867 struct f_ncm
*ncm
= func_to_ncm(f
);
868 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
870 /* Control interface has only altsetting 0 */
871 if (intf
== ncm
->ctrl_id
) {
875 DBG(cdev
, "reset ncm control %d\n", intf
);
876 usb_ep_disable(ncm
->notify
);
878 if (!(ncm
->notify
->desc
)) {
879 DBG(cdev
, "init ncm ctrl %d\n", intf
);
880 if (config_ep_by_speed(cdev
->gadget
, f
, ncm
->notify
))
883 usb_ep_enable(ncm
->notify
);
885 /* Data interface has two altsettings, 0 and 1 */
886 } else if (intf
== ncm
->data_id
) {
890 if (ncm
->port
.in_ep
->enabled
) {
891 DBG(cdev
, "reset ncm\n");
892 ncm
->timer_stopping
= true;
894 gether_disconnect(&ncm
->port
);
895 ncm_reset_values(ncm
);
899 * CDC Network only sends data in non-default altsettings.
900 * Changing altsettings resets filters, statistics, etc.
903 struct net_device
*net
;
905 if (!ncm
->port
.in_ep
->desc
||
906 !ncm
->port
.out_ep
->desc
) {
907 DBG(cdev
, "init ncm\n");
908 if (config_ep_by_speed(cdev
->gadget
, f
,
910 config_ep_by_speed(cdev
->gadget
, f
,
912 ncm
->port
.in_ep
->desc
= NULL
;
913 ncm
->port
.out_ep
->desc
= NULL
;
919 /* Enable zlps by default for NCM conformance;
920 * override for musb_hdrc (avoids txdma ovhead)
922 ncm
->port
.is_zlp_ok
=
923 gadget_is_zlp_supported(cdev
->gadget
);
924 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
925 DBG(cdev
, "activate ncm\n");
926 net
= gether_connect(&ncm
->port
);
930 ncm
->timer_stopping
= false;
933 spin_lock(&ncm
->lock
);
935 spin_unlock(&ncm
->lock
);
945 * Because the data interface supports multiple altsettings,
946 * this NCM function *MUST* implement a get_alt() method.
948 static int ncm_get_alt(struct usb_function
*f
, unsigned intf
)
950 struct f_ncm
*ncm
= func_to_ncm(f
);
952 if (intf
== ncm
->ctrl_id
)
954 return ncm
->port
.in_ep
->enabled
? 1 : 0;
957 static struct sk_buff
*package_for_tx(struct f_ncm
*ncm
)
960 struct sk_buff
*skb2
= NULL
;
965 const struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
966 const int ndp_align
= le16_to_cpu(ntb_parameters
.wNdpInAlignment
);
967 const int dgram_idx_len
= 2 * 2 * opts
->dgram_item_len
;
970 hrtimer_try_to_cancel(&ncm
->task_timer
);
972 ndp_pad
= ALIGN(ncm
->skb_tx_data
->len
, ndp_align
) -
973 ncm
->skb_tx_data
->len
;
974 ndp_index
= ncm
->skb_tx_data
->len
+ ndp_pad
;
975 new_len
= ndp_index
+ dgram_idx_len
+ ncm
->skb_tx_ndp
->len
;
977 /* Set the final BlockLength and wNdpIndex */
978 ntb_iter
= (void *) ncm
->skb_tx_data
->data
;
979 /* Increment pointer to BlockLength */
980 ntb_iter
+= 2 + 1 + 1;
981 put_ncm(&ntb_iter
, opts
->block_length
, new_len
);
982 put_ncm(&ntb_iter
, opts
->ndp_index
, ndp_index
);
984 /* Set the final NDP wLength */
985 new_len
= opts
->ndp_size
+
986 (ncm
->ndp_dgram_count
* dgram_idx_len
);
987 ncm
->ndp_dgram_count
= 0;
988 /* Increment from start to wLength */
989 ntb_iter
= (void *) ncm
->skb_tx_ndp
->data
;
991 put_unaligned_le16(new_len
, ntb_iter
);
994 swap(skb2
, ncm
->skb_tx_data
);
995 if (ncm
->skb_tx_data
) {
996 dev_consume_skb_any(ncm
->skb_tx_data
);
997 ncm
->skb_tx_data
= NULL
;
1000 /* Insert NDP alignment. */
1001 skb_put_zero(skb2
, ndp_pad
);
1003 /* Copy NTB across. */
1004 skb_put_data(skb2
, ncm
->skb_tx_ndp
->data
, ncm
->skb_tx_ndp
->len
);
1005 dev_consume_skb_any(ncm
->skb_tx_ndp
);
1006 ncm
->skb_tx_ndp
= NULL
;
1008 /* Insert zero'd datagram. */
1009 skb_put_zero(skb2
, dgram_idx_len
);
1014 static struct sk_buff
*ncm_wrap_ntb(struct gether
*port
,
1015 struct sk_buff
*skb
)
1017 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
1018 struct sk_buff
*skb2
= NULL
;
1024 unsigned max_size
= ncm
->port
.fixed_in_len
;
1025 const struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
1026 const int ndp_align
= le16_to_cpu(ntb_parameters
.wNdpInAlignment
);
1027 const int div
= le16_to_cpu(ntb_parameters
.wNdpInDivisor
);
1028 const int rem
= le16_to_cpu(ntb_parameters
.wNdpInPayloadRemainder
);
1029 const int dgram_idx_len
= 2 * 2 * opts
->dgram_item_len
;
1031 if (!skb
&& !ncm
->skb_tx_data
)
1035 /* Add the CRC if required up front */
1043 crc_pos
= skb_put(skb
, sizeof(uint32_t));
1044 put_unaligned_le32(crc
, crc_pos
);
1047 /* If the new skb is too big for the current NCM NTB then
1048 * set the current stored skb to be sent now and clear it
1049 * ready for new data.
1050 * NOTE: Assume maximum align for speed of calculation.
1052 if (ncm
->skb_tx_data
1053 && (ncm
->ndp_dgram_count
>= TX_MAX_NUM_DPE
1054 || (ncm
->skb_tx_data
->len
+
1055 div
+ rem
+ skb
->len
+
1056 ncm
->skb_tx_ndp
->len
+ ndp_align
+ (2 * dgram_idx_len
))
1058 skb2
= package_for_tx(ncm
);
1063 if (!ncm
->skb_tx_data
) {
1064 ncb_len
= opts
->nth_size
;
1065 dgram_pad
= ALIGN(ncb_len
, div
) + rem
- ncb_len
;
1066 ncb_len
+= dgram_pad
;
1068 /* Create a new skb for the NTH and datagrams. */
1069 ncm
->skb_tx_data
= alloc_skb(max_size
, GFP_ATOMIC
);
1070 if (!ncm
->skb_tx_data
)
1073 ncm
->skb_tx_data
->dev
= ncm
->netdev
;
1074 ntb_data
= skb_put_zero(ncm
->skb_tx_data
, ncb_len
);
1076 put_unaligned_le32(opts
->nth_sign
, ntb_data
);
1079 put_unaligned_le16(opts
->nth_size
, ntb_data
++);
1081 /* Allocate an skb for storing the NDP,
1082 * TX_MAX_NUM_DPE should easily suffice for a
1085 ncm
->skb_tx_ndp
= alloc_skb((int)(opts
->ndp_size
1089 if (!ncm
->skb_tx_ndp
)
1092 ncm
->skb_tx_ndp
->dev
= ncm
->netdev
;
1093 ntb_ndp
= skb_put(ncm
->skb_tx_ndp
, opts
->ndp_size
);
1094 memset(ntb_ndp
, 0, ncb_len
);
1096 put_unaligned_le32(ncm
->ndp_sign
, ntb_ndp
);
1099 /* There is always a zeroed entry */
1100 ncm
->ndp_dgram_count
= 1;
1102 /* Note: we skip opts->next_ndp_index */
1105 /* Delay the timer. */
1106 hrtimer_start(&ncm
->task_timer
, TX_TIMEOUT_NSECS
,
1109 /* Add the datagram position entries */
1110 ntb_ndp
= skb_put_zero(ncm
->skb_tx_ndp
, dgram_idx_len
);
1112 ncb_len
= ncm
->skb_tx_data
->len
;
1113 dgram_pad
= ALIGN(ncb_len
, div
) + rem
- ncb_len
;
1114 ncb_len
+= dgram_pad
;
1116 /* (d)wDatagramIndex */
1117 put_ncm(&ntb_ndp
, opts
->dgram_item_len
, ncb_len
);
1118 /* (d)wDatagramLength */
1119 put_ncm(&ntb_ndp
, opts
->dgram_item_len
, skb
->len
);
1120 ncm
->ndp_dgram_count
++;
1122 /* Add the new data to the skb */
1123 skb_put_zero(ncm
->skb_tx_data
, dgram_pad
);
1124 skb_put_data(ncm
->skb_tx_data
, skb
->data
, skb
->len
);
1125 dev_consume_skb_any(skb
);
1128 } else if (ncm
->skb_tx_data
&& ncm
->timer_force_tx
) {
1129 /* If the tx was requested because of a timeout then send */
1130 skb2
= package_for_tx(ncm
);
1138 ncm
->netdev
->stats
.tx_dropped
++;
1141 dev_kfree_skb_any(skb
);
1142 if (ncm
->skb_tx_data
)
1143 dev_kfree_skb_any(ncm
->skb_tx_data
);
1144 if (ncm
->skb_tx_ndp
)
1145 dev_kfree_skb_any(ncm
->skb_tx_ndp
);
1151 * This transmits the NTB if there are frames waiting.
1153 static void ncm_tx_tasklet(unsigned long data
)
1155 struct f_ncm
*ncm
= (void *)data
;
1157 if (ncm
->timer_stopping
)
1160 /* Only send if data is available. */
1161 if (ncm
->skb_tx_data
) {
1162 ncm
->timer_force_tx
= true;
1164 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1165 * XXX is not sane. The gadget layer should be redesigned so
1166 * XXX that the dev->wrap() invocations to build SKBs is transparent
1167 * XXX and performed in some way outside of the ndo_start_xmit
1170 ncm
->netdev
->netdev_ops
->ndo_start_xmit(NULL
, ncm
->netdev
);
1172 ncm
->timer_force_tx
= false;
1177 * The transmit should only be run if no skb data has been sent
1178 * for a certain duration.
1180 static enum hrtimer_restart
ncm_tx_timeout(struct hrtimer
*data
)
1182 struct f_ncm
*ncm
= container_of(data
, struct f_ncm
, task_timer
);
1183 tasklet_schedule(&ncm
->tx_tasklet
);
1184 return HRTIMER_NORESTART
;
1187 static int ncm_unwrap_ntb(struct gether
*port
,
1188 struct sk_buff
*skb
,
1189 struct sk_buff_head
*list
)
1191 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
1192 __le16
*tmp
= (void *) skb
->data
;
1193 unsigned index
, index2
;
1195 unsigned dg_len
, dg_len2
;
1197 struct sk_buff
*skb2
;
1199 unsigned max_size
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
1200 const struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
1201 unsigned crc_len
= ncm
->is_crc
? sizeof(uint32_t) : 0;
1205 if (get_unaligned_le32(tmp
) != opts
->nth_sign
) {
1206 INFO(port
->func
.config
->cdev
, "Wrong NTH SIGN, skblen %d\n",
1208 print_hex_dump(KERN_INFO
, "HEAD:", DUMP_PREFIX_ADDRESS
, 32, 1,
1209 skb
->data
, 32, false);
1215 if (get_unaligned_le16(tmp
++) != opts
->nth_size
) {
1216 INFO(port
->func
.config
->cdev
, "Wrong NTB headersize\n");
1219 tmp
++; /* skip wSequence */
1221 /* (d)wBlockLength */
1222 if (get_ncm(&tmp
, opts
->block_length
) > max_size
) {
1223 INFO(port
->func
.config
->cdev
, "OUT size exceeded\n");
1227 ndp_index
= get_ncm(&tmp
, opts
->ndp_index
);
1229 /* Run through all the NDP's in the NTB */
1232 if (((ndp_index
% 4) != 0) &&
1233 (ndp_index
< opts
->nth_size
)) {
1234 INFO(port
->func
.config
->cdev
, "Bad index: %#X\n",
1239 /* walk through NDP */
1240 tmp
= (void *)(skb
->data
+ ndp_index
);
1241 if (get_unaligned_le32(tmp
) != ncm
->ndp_sign
) {
1242 INFO(port
->func
.config
->cdev
, "Wrong NDP SIGN\n");
1247 ndp_len
= get_unaligned_le16(tmp
++);
1251 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1252 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1253 * Each entry is a dgram index and a dgram length.
1255 if ((ndp_len
< opts
->ndp_size
1256 + 2 * 2 * (opts
->dgram_item_len
* 2))
1257 || (ndp_len
% opts
->ndplen_align
!= 0)) {
1258 INFO(port
->func
.config
->cdev
, "Bad NDP length: %#X\n",
1262 tmp
+= opts
->reserved1
;
1263 /* Check for another NDP (d)wNextNdpIndex */
1264 ndp_index
= get_ncm(&tmp
, opts
->next_ndp_index
);
1265 tmp
+= opts
->reserved2
;
1267 ndp_len
-= opts
->ndp_size
;
1268 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1269 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1275 if (dg_len
< 14 + crc_len
) { /* ethernet hdr + crc */
1276 INFO(port
->func
.config
->cdev
,
1277 "Bad dgram length: %#X\n", dg_len
);
1283 crc
= get_unaligned_le32(skb
->data
+
1286 crc2
= ~crc32_le(~0,
1290 INFO(port
->func
.config
->cdev
,
1296 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1297 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1300 * Copy the data into a new skb.
1301 * This ensures the truesize is correct
1303 skb2
= netdev_alloc_skb_ip_align(ncm
->netdev
,
1307 skb_put_data(skb2
, skb
->data
+ index
,
1310 skb_queue_tail(list
, skb2
);
1312 ndp_len
-= 2 * (opts
->dgram_item_len
* 2);
1316 if (index2
== 0 || dg_len2
== 0)
1318 } while (ndp_len
> 2 * (opts
->dgram_item_len
* 2));
1319 } while (ndp_index
);
1321 dev_consume_skb_any(skb
);
1323 VDBG(port
->func
.config
->cdev
,
1324 "Parsed NTB with %d frames\n", dgram_counter
);
1327 skb_queue_purge(list
);
1328 dev_kfree_skb_any(skb
);
1332 static void ncm_disable(struct usb_function
*f
)
1334 struct f_ncm
*ncm
= func_to_ncm(f
);
1335 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
1337 DBG(cdev
, "ncm deactivated\n");
1339 if (ncm
->port
.in_ep
->enabled
) {
1340 ncm
->timer_stopping
= true;
1342 gether_disconnect(&ncm
->port
);
1345 if (ncm
->notify
->enabled
) {
1346 usb_ep_disable(ncm
->notify
);
1347 ncm
->notify
->desc
= NULL
;
1351 /*-------------------------------------------------------------------------*/
1354 * Callbacks let us notify the host about connect/disconnect when the
1355 * net device is opened or closed.
1357 * For testing, note that link states on this side include both opened
1358 * and closed variants of:
1360 * - disconnected/unconfigured
1361 * - configured but inactive (data alt 0)
1362 * - configured and active (data alt 1)
1364 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1365 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1366 * imply the host is actually polling the notification endpoint, and
1367 * likewise that "active" doesn't imply it's actually using the data
1368 * endpoints for traffic.
1371 static void ncm_open(struct gether
*geth
)
1373 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1375 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1377 spin_lock(&ncm
->lock
);
1378 ncm
->is_open
= true;
1380 spin_unlock(&ncm
->lock
);
1383 static void ncm_close(struct gether
*geth
)
1385 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1387 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1389 spin_lock(&ncm
->lock
);
1390 ncm
->is_open
= false;
1392 spin_unlock(&ncm
->lock
);
1395 /*-------------------------------------------------------------------------*/
1397 /* ethernet function driver setup/binding */
1399 static int ncm_bind(struct usb_configuration
*c
, struct usb_function
*f
)
1401 struct usb_composite_dev
*cdev
= c
->cdev
;
1402 struct f_ncm
*ncm
= func_to_ncm(f
);
1403 struct usb_string
*us
;
1406 struct f_ncm_opts
*ncm_opts
;
1408 if (!can_support_ecm(cdev
->gadget
))
1411 ncm_opts
= container_of(f
->fi
, struct f_ncm_opts
, func_inst
);
1413 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1414 * configurations are bound in sequence with list_for_each_entry,
1415 * in each configuration its functions are bound in sequence
1416 * with list_for_each_entry, so we assume no race condition
1417 * with regard to ncm_opts->bound access
1419 if (!ncm_opts
->bound
) {
1420 mutex_lock(&ncm_opts
->lock
);
1421 gether_set_gadget(ncm_opts
->net
, cdev
->gadget
);
1422 status
= gether_register_netdev(ncm_opts
->net
);
1423 mutex_unlock(&ncm_opts
->lock
);
1426 ncm_opts
->bound
= true;
1428 us
= usb_gstrings_attach(cdev
, ncm_strings
,
1429 ARRAY_SIZE(ncm_string_defs
));
1432 ncm_control_intf
.iInterface
= us
[STRING_CTRL_IDX
].id
;
1433 ncm_data_nop_intf
.iInterface
= us
[STRING_DATA_IDX
].id
;
1434 ncm_data_intf
.iInterface
= us
[STRING_DATA_IDX
].id
;
1435 ecm_desc
.iMACAddress
= us
[STRING_MAC_IDX
].id
;
1436 ncm_iad_desc
.iFunction
= us
[STRING_IAD_IDX
].id
;
1438 /* allocate instance-specific interface IDs */
1439 status
= usb_interface_id(c
, f
);
1442 ncm
->ctrl_id
= status
;
1443 ncm_iad_desc
.bFirstInterface
= status
;
1445 ncm_control_intf
.bInterfaceNumber
= status
;
1446 ncm_union_desc
.bMasterInterface0
= status
;
1448 status
= usb_interface_id(c
, f
);
1451 ncm
->data_id
= status
;
1453 ncm_data_nop_intf
.bInterfaceNumber
= status
;
1454 ncm_data_intf
.bInterfaceNumber
= status
;
1455 ncm_union_desc
.bSlaveInterface0
= status
;
1459 /* allocate instance-specific endpoints */
1460 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_in_desc
);
1463 ncm
->port
.in_ep
= ep
;
1465 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_out_desc
);
1468 ncm
->port
.out_ep
= ep
;
1470 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_notify_desc
);
1477 /* allocate notification request and buffer */
1478 ncm
->notify_req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
1479 if (!ncm
->notify_req
)
1481 ncm
->notify_req
->buf
= kmalloc(NCM_STATUS_BYTECOUNT
, GFP_KERNEL
);
1482 if (!ncm
->notify_req
->buf
)
1484 ncm
->notify_req
->context
= ncm
;
1485 ncm
->notify_req
->complete
= ncm_notify_complete
;
1488 * support all relevant hardware speeds... we expect that when
1489 * hardware is dual speed, all bulk-capable endpoints work at
1492 hs_ncm_in_desc
.bEndpointAddress
= fs_ncm_in_desc
.bEndpointAddress
;
1493 hs_ncm_out_desc
.bEndpointAddress
= fs_ncm_out_desc
.bEndpointAddress
;
1494 hs_ncm_notify_desc
.bEndpointAddress
=
1495 fs_ncm_notify_desc
.bEndpointAddress
;
1497 ss_ncm_in_desc
.bEndpointAddress
= fs_ncm_in_desc
.bEndpointAddress
;
1498 ss_ncm_out_desc
.bEndpointAddress
= fs_ncm_out_desc
.bEndpointAddress
;
1499 ss_ncm_notify_desc
.bEndpointAddress
=
1500 fs_ncm_notify_desc
.bEndpointAddress
;
1502 status
= usb_assign_descriptors(f
, ncm_fs_function
, ncm_hs_function
,
1503 ncm_ss_function
, NULL
);
1508 * NOTE: all that is done without knowing or caring about
1509 * the network link ... which is unavailable to this code
1510 * until we're activated via set_alt().
1513 ncm
->port
.open
= ncm_open
;
1514 ncm
->port
.close
= ncm_close
;
1516 tasklet_init(&ncm
->tx_tasklet
, ncm_tx_tasklet
, (unsigned long) ncm
);
1517 hrtimer_init(&ncm
->task_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1518 ncm
->task_timer
.function
= ncm_tx_timeout
;
1520 DBG(cdev
, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1521 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
1522 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
1523 ncm
->port
.in_ep
->name
, ncm
->port
.out_ep
->name
,
1528 if (ncm
->notify_req
) {
1529 kfree(ncm
->notify_req
->buf
);
1530 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1533 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
1538 static inline struct f_ncm_opts
*to_f_ncm_opts(struct config_item
*item
)
1540 return container_of(to_config_group(item
), struct f_ncm_opts
,
1544 /* f_ncm_item_ops */
1545 USB_ETHERNET_CONFIGFS_ITEM(ncm
);
1547 /* f_ncm_opts_dev_addr */
1548 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm
);
1550 /* f_ncm_opts_host_addr */
1551 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm
);
1553 /* f_ncm_opts_qmult */
1554 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm
);
1556 /* f_ncm_opts_ifname */
1557 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm
);
1559 static struct configfs_attribute
*ncm_attrs
[] = {
1560 &ncm_opts_attr_dev_addr
,
1561 &ncm_opts_attr_host_addr
,
1562 &ncm_opts_attr_qmult
,
1563 &ncm_opts_attr_ifname
,
1567 static struct config_item_type ncm_func_type
= {
1568 .ct_item_ops
= &ncm_item_ops
,
1569 .ct_attrs
= ncm_attrs
,
1570 .ct_owner
= THIS_MODULE
,
1573 static void ncm_free_inst(struct usb_function_instance
*f
)
1575 struct f_ncm_opts
*opts
;
1577 opts
= container_of(f
, struct f_ncm_opts
, func_inst
);
1579 gether_cleanup(netdev_priv(opts
->net
));
1581 free_netdev(opts
->net
);
1585 static struct usb_function_instance
*ncm_alloc_inst(void)
1587 struct f_ncm_opts
*opts
;
1589 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1591 return ERR_PTR(-ENOMEM
);
1592 mutex_init(&opts
->lock
);
1593 opts
->func_inst
.free_func_inst
= ncm_free_inst
;
1594 opts
->net
= gether_setup_default();
1595 if (IS_ERR(opts
->net
)) {
1596 struct net_device
*net
= opts
->net
;
1598 return ERR_CAST(net
);
1601 config_group_init_type_name(&opts
->func_inst
.group
, "", &ncm_func_type
);
1603 return &opts
->func_inst
;
1606 static void ncm_free(struct usb_function
*f
)
1609 struct f_ncm_opts
*opts
;
1611 ncm
= func_to_ncm(f
);
1612 opts
= container_of(f
->fi
, struct f_ncm_opts
, func_inst
);
1614 mutex_lock(&opts
->lock
);
1616 mutex_unlock(&opts
->lock
);
1619 static void ncm_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1621 struct f_ncm
*ncm
= func_to_ncm(f
);
1623 DBG(c
->cdev
, "ncm unbind\n");
1625 hrtimer_cancel(&ncm
->task_timer
);
1626 tasklet_kill(&ncm
->tx_tasklet
);
1628 ncm_string_defs
[0].id
= 0;
1629 usb_free_all_descriptors(f
);
1631 kfree(ncm
->notify_req
->buf
);
1632 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1635 static struct usb_function
*ncm_alloc(struct usb_function_instance
*fi
)
1638 struct f_ncm_opts
*opts
;
1641 /* allocate and initialize one new instance */
1642 ncm
= kzalloc(sizeof(*ncm
), GFP_KERNEL
);
1644 return ERR_PTR(-ENOMEM
);
1646 opts
= container_of(fi
, struct f_ncm_opts
, func_inst
);
1647 mutex_lock(&opts
->lock
);
1650 /* export host's Ethernet address in CDC format */
1651 status
= gether_get_host_addr_cdc(opts
->net
, ncm
->ethaddr
,
1652 sizeof(ncm
->ethaddr
));
1653 if (status
< 12) { /* strlen("01234567890a") */
1655 mutex_unlock(&opts
->lock
);
1656 return ERR_PTR(-EINVAL
);
1658 ncm_string_defs
[STRING_MAC_IDX
].s
= ncm
->ethaddr
;
1660 spin_lock_init(&ncm
->lock
);
1661 ncm_reset_values(ncm
);
1662 ncm
->port
.ioport
= netdev_priv(opts
->net
);
1663 mutex_unlock(&opts
->lock
);
1664 ncm
->port
.is_fixed
= true;
1665 ncm
->port
.supports_multi_frame
= true;
1667 ncm
->port
.func
.name
= "cdc_network";
1668 /* descriptors are per-instance copies */
1669 ncm
->port
.func
.bind
= ncm_bind
;
1670 ncm
->port
.func
.unbind
= ncm_unbind
;
1671 ncm
->port
.func
.set_alt
= ncm_set_alt
;
1672 ncm
->port
.func
.get_alt
= ncm_get_alt
;
1673 ncm
->port
.func
.setup
= ncm_setup
;
1674 ncm
->port
.func
.disable
= ncm_disable
;
1675 ncm
->port
.func
.free_func
= ncm_free
;
1677 ncm
->port
.wrap
= ncm_wrap_ntb
;
1678 ncm
->port
.unwrap
= ncm_unwrap_ntb
;
1680 return &ncm
->port
.func
;
1683 DECLARE_USB_FUNCTION_INIT(ncm
, ncm_alloc_inst
, ncm_alloc
);
1684 MODULE_LICENSE("GPL");
1685 MODULE_AUTHOR("Yauheni Kaliuta");