2 * f_ncm.c -- USB CDC Network (NCM) link function driver
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
7 * The driver borrows from f_ecm.c which is:
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2008 Nokia Corporation
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/etherdevice.h>
21 #include <linux/crc32.h>
23 #include <linux/usb/cdc.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 struct ndp_parser_opts
*parser_opts
;
63 * for notification, it is accessed from both
64 * callback and ethernet open/close
69 static inline struct f_ncm
*func_to_ncm(struct usb_function
*f
)
71 return container_of(f
, struct f_ncm
, port
.func
);
74 /* peak (theoretical) bulk transfer rate in bits-per-second */
75 static inline unsigned ncm_bitrate(struct usb_gadget
*g
)
77 if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
78 return 13 * 512 * 8 * 1000 * 8;
80 return 19 * 64 * 1 * 1000 * 8;
83 /*-------------------------------------------------------------------------*/
86 * We cannot group frames so use just the minimal size which ok to put
87 * one max-size ethernet frame.
88 * If the host can group frames, allow it to do that, 16K is selected,
89 * because it's used by default by the current linux host driver
91 #define NTB_DEFAULT_IN_SIZE USB_CDC_NCM_NTB_MIN_IN_SIZE
92 #define NTB_OUT_SIZE 16384
95 * skbs of size less than that will not be aligned
96 * to NCM's dwNtbInMaxSize to save bus bandwidth
99 #define MAX_TX_NONFIXED (512 * 3)
101 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
102 USB_CDC_NCM_NTB32_SUPPORTED)
104 static struct usb_cdc_ncm_ntb_parameters ntb_parameters
= {
105 .wLength
= sizeof ntb_parameters
,
106 .bmNtbFormatsSupported
= cpu_to_le16(FORMATS_SUPPORTED
),
107 .dwNtbInMaxSize
= cpu_to_le32(NTB_DEFAULT_IN_SIZE
),
108 .wNdpInDivisor
= cpu_to_le16(4),
109 .wNdpInPayloadRemainder
= cpu_to_le16(0),
110 .wNdpInAlignment
= cpu_to_le16(4),
112 .dwNtbOutMaxSize
= cpu_to_le32(NTB_OUT_SIZE
),
113 .wNdpOutDivisor
= cpu_to_le16(4),
114 .wNdpOutPayloadRemainder
= cpu_to_le16(0),
115 .wNdpOutAlignment
= cpu_to_le16(4),
119 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
120 * packet, to simplify cancellation; and a big transfer interval, to
121 * waste less bandwidth.
124 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
125 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
127 static struct usb_interface_assoc_descriptor ncm_iad_desc __initdata
= {
128 .bLength
= sizeof ncm_iad_desc
,
129 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
131 /* .bFirstInterface = DYNAMIC, */
132 .bInterfaceCount
= 2, /* control + data */
133 .bFunctionClass
= USB_CLASS_COMM
,
134 .bFunctionSubClass
= USB_CDC_SUBCLASS_NCM
,
135 .bFunctionProtocol
= USB_CDC_PROTO_NONE
,
136 /* .iFunction = DYNAMIC */
139 /* interface descriptor: */
141 static struct usb_interface_descriptor ncm_control_intf __initdata
= {
142 .bLength
= sizeof ncm_control_intf
,
143 .bDescriptorType
= USB_DT_INTERFACE
,
145 /* .bInterfaceNumber = DYNAMIC */
147 .bInterfaceClass
= USB_CLASS_COMM
,
148 .bInterfaceSubClass
= USB_CDC_SUBCLASS_NCM
,
149 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
150 /* .iInterface = DYNAMIC */
153 static struct usb_cdc_header_desc ncm_header_desc __initdata
= {
154 .bLength
= sizeof ncm_header_desc
,
155 .bDescriptorType
= USB_DT_CS_INTERFACE
,
156 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
158 .bcdCDC
= cpu_to_le16(0x0110),
161 static struct usb_cdc_union_desc ncm_union_desc __initdata
= {
162 .bLength
= sizeof(ncm_union_desc
),
163 .bDescriptorType
= USB_DT_CS_INTERFACE
,
164 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
165 /* .bMasterInterface0 = DYNAMIC */
166 /* .bSlaveInterface0 = DYNAMIC */
169 static struct usb_cdc_ether_desc ecm_desc __initdata
= {
170 .bLength
= sizeof ecm_desc
,
171 .bDescriptorType
= USB_DT_CS_INTERFACE
,
172 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
174 /* this descriptor actually adds value, surprise! */
175 /* .iMACAddress = DYNAMIC */
176 .bmEthernetStatistics
= cpu_to_le32(0), /* no statistics */
177 .wMaxSegmentSize
= cpu_to_le16(ETH_FRAME_LEN
),
178 .wNumberMCFilters
= cpu_to_le16(0),
179 .bNumberPowerFilters
= 0,
182 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
184 static struct usb_cdc_ncm_desc ncm_desc __initdata
= {
185 .bLength
= sizeof ncm_desc
,
186 .bDescriptorType
= USB_DT_CS_INTERFACE
,
187 .bDescriptorSubType
= USB_CDC_NCM_TYPE
,
189 .bcdNcmVersion
= cpu_to_le16(0x0100),
190 /* can process SetEthernetPacketFilter */
191 .bmNetworkCapabilities
= NCAPS
,
194 /* the default data interface has no endpoints ... */
196 static struct usb_interface_descriptor ncm_data_nop_intf __initdata
= {
197 .bLength
= sizeof ncm_data_nop_intf
,
198 .bDescriptorType
= USB_DT_INTERFACE
,
200 .bInterfaceNumber
= 1,
201 .bAlternateSetting
= 0,
203 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
204 .bInterfaceSubClass
= 0,
205 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
206 /* .iInterface = DYNAMIC */
209 /* ... but the "real" data interface has two bulk endpoints */
211 static struct usb_interface_descriptor ncm_data_intf __initdata
= {
212 .bLength
= sizeof ncm_data_intf
,
213 .bDescriptorType
= USB_DT_INTERFACE
,
215 .bInterfaceNumber
= 1,
216 .bAlternateSetting
= 1,
218 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
219 .bInterfaceSubClass
= 0,
220 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
221 /* .iInterface = DYNAMIC */
224 /* full speed support: */
226 static struct usb_endpoint_descriptor fs_ncm_notify_desc __initdata
= {
227 .bLength
= USB_DT_ENDPOINT_SIZE
,
228 .bDescriptorType
= USB_DT_ENDPOINT
,
230 .bEndpointAddress
= USB_DIR_IN
,
231 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
232 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
233 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
236 static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata
= {
237 .bLength
= USB_DT_ENDPOINT_SIZE
,
238 .bDescriptorType
= USB_DT_ENDPOINT
,
240 .bEndpointAddress
= USB_DIR_IN
,
241 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
244 static struct usb_endpoint_descriptor fs_ncm_out_desc __initdata
= {
245 .bLength
= USB_DT_ENDPOINT_SIZE
,
246 .bDescriptorType
= USB_DT_ENDPOINT
,
248 .bEndpointAddress
= USB_DIR_OUT
,
249 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
252 static struct usb_descriptor_header
*ncm_fs_function
[] __initdata
= {
253 (struct usb_descriptor_header
*) &ncm_iad_desc
,
254 /* CDC NCM control descriptors */
255 (struct usb_descriptor_header
*) &ncm_control_intf
,
256 (struct usb_descriptor_header
*) &ncm_header_desc
,
257 (struct usb_descriptor_header
*) &ncm_union_desc
,
258 (struct usb_descriptor_header
*) &ecm_desc
,
259 (struct usb_descriptor_header
*) &ncm_desc
,
260 (struct usb_descriptor_header
*) &fs_ncm_notify_desc
,
261 /* data interface, altsettings 0 and 1 */
262 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
263 (struct usb_descriptor_header
*) &ncm_data_intf
,
264 (struct usb_descriptor_header
*) &fs_ncm_in_desc
,
265 (struct usb_descriptor_header
*) &fs_ncm_out_desc
,
269 /* high speed support: */
271 static struct usb_endpoint_descriptor hs_ncm_notify_desc __initdata
= {
272 .bLength
= USB_DT_ENDPOINT_SIZE
,
273 .bDescriptorType
= USB_DT_ENDPOINT
,
275 .bEndpointAddress
= USB_DIR_IN
,
276 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
277 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
278 .bInterval
= LOG2_STATUS_INTERVAL_MSEC
+ 4,
280 static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata
= {
281 .bLength
= USB_DT_ENDPOINT_SIZE
,
282 .bDescriptorType
= USB_DT_ENDPOINT
,
284 .bEndpointAddress
= USB_DIR_IN
,
285 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
286 .wMaxPacketSize
= cpu_to_le16(512),
289 static struct usb_endpoint_descriptor hs_ncm_out_desc __initdata
= {
290 .bLength
= USB_DT_ENDPOINT_SIZE
,
291 .bDescriptorType
= USB_DT_ENDPOINT
,
293 .bEndpointAddress
= USB_DIR_OUT
,
294 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
295 .wMaxPacketSize
= cpu_to_le16(512),
298 static struct usb_descriptor_header
*ncm_hs_function
[] __initdata
= {
299 (struct usb_descriptor_header
*) &ncm_iad_desc
,
300 /* CDC NCM control descriptors */
301 (struct usb_descriptor_header
*) &ncm_control_intf
,
302 (struct usb_descriptor_header
*) &ncm_header_desc
,
303 (struct usb_descriptor_header
*) &ncm_union_desc
,
304 (struct usb_descriptor_header
*) &ecm_desc
,
305 (struct usb_descriptor_header
*) &ncm_desc
,
306 (struct usb_descriptor_header
*) &hs_ncm_notify_desc
,
307 /* data interface, altsettings 0 and 1 */
308 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
309 (struct usb_descriptor_header
*) &ncm_data_intf
,
310 (struct usb_descriptor_header
*) &hs_ncm_in_desc
,
311 (struct usb_descriptor_header
*) &hs_ncm_out_desc
,
315 /* string descriptors: */
317 #define STRING_CTRL_IDX 0
318 #define STRING_MAC_IDX 1
319 #define STRING_DATA_IDX 2
320 #define STRING_IAD_IDX 3
322 static struct usb_string ncm_string_defs
[] = {
323 [STRING_CTRL_IDX
].s
= "CDC Network Control Model (NCM)",
324 [STRING_MAC_IDX
].s
= NULL
/* DYNAMIC */,
325 [STRING_DATA_IDX
].s
= "CDC Network Data",
326 [STRING_IAD_IDX
].s
= "CDC NCM",
327 { } /* end of list */
330 static struct usb_gadget_strings ncm_string_table
= {
331 .language
= 0x0409, /* en-us */
332 .strings
= ncm_string_defs
,
335 static struct usb_gadget_strings
*ncm_strings
[] = {
341 * Here are options for NCM Datagram Pointer table (NDP) parser.
342 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
343 * in NDP16 offsets and sizes fields are 1 16bit word wide,
344 * in NDP32 -- 2 16bit words wide. Also signatures are different.
345 * To make the parser code the same, put the differences in the structure,
346 * and switch pointers to the structures when the format is changed.
349 struct ndp_parser_opts
{
354 unsigned ndplen_align
;
355 /* sizes in u16 units */
356 unsigned dgram_item_len
; /* index or length */
357 unsigned block_length
;
361 unsigned next_fp_index
;
364 #define INIT_NDP16_OPTS { \
365 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
366 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
367 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
368 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
370 .dgram_item_len = 1, \
375 .next_fp_index = 1, \
379 #define INIT_NDP32_OPTS { \
380 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
381 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
382 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
383 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
385 .dgram_item_len = 2, \
390 .next_fp_index = 2, \
393 static struct ndp_parser_opts ndp16_opts
= INIT_NDP16_OPTS
;
394 static struct ndp_parser_opts ndp32_opts
= INIT_NDP32_OPTS
;
396 static inline void put_ncm(__le16
**p
, unsigned size
, unsigned val
)
400 put_unaligned_le16((u16
)val
, *p
);
403 put_unaligned_le32((u32
)val
, *p
);
413 static inline unsigned get_ncm(__le16
**p
, unsigned size
)
419 tmp
= get_unaligned_le16(*p
);
422 tmp
= get_unaligned_le32(*p
);
432 /*-------------------------------------------------------------------------*/
434 static inline void ncm_reset_values(struct f_ncm
*ncm
)
436 ncm
->parser_opts
= &ndp16_opts
;
438 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
440 /* doesn't make sense for ncm, fixed size used */
441 ncm
->port
.header_len
= 0;
443 ncm
->port
.fixed_out_len
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
444 ncm
->port
.fixed_in_len
= NTB_DEFAULT_IN_SIZE
;
448 * Context: ncm->lock held
450 static void ncm_do_notify(struct f_ncm
*ncm
)
452 struct usb_request
*req
= ncm
->notify_req
;
453 struct usb_cdc_notification
*event
;
454 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
458 /* notification already in flight? */
463 switch (ncm
->notify_state
) {
464 case NCM_NOTIFY_NONE
:
467 case NCM_NOTIFY_CONNECT
:
468 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
470 event
->wValue
= cpu_to_le16(1);
472 event
->wValue
= cpu_to_le16(0);
474 req
->length
= sizeof *event
;
476 DBG(cdev
, "notify connect %s\n",
477 ncm
->is_open
? "true" : "false");
478 ncm
->notify_state
= NCM_NOTIFY_NONE
;
481 case NCM_NOTIFY_SPEED
:
482 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
483 event
->wValue
= cpu_to_le16(0);
484 event
->wLength
= cpu_to_le16(8);
485 req
->length
= NCM_STATUS_BYTECOUNT
;
487 /* SPEED_CHANGE data is up/down speeds in bits/sec */
488 data
= req
->buf
+ sizeof *event
;
489 data
[0] = cpu_to_le32(ncm_bitrate(cdev
->gadget
));
492 DBG(cdev
, "notify speed %d\n", ncm_bitrate(cdev
->gadget
));
493 ncm
->notify_state
= NCM_NOTIFY_CONNECT
;
496 event
->bmRequestType
= 0xA1;
497 event
->wIndex
= cpu_to_le16(ncm
->ctrl_id
);
499 ncm
->notify_req
= NULL
;
501 * In double buffering if there is a space in FIFO,
502 * completion callback can be called right after the call,
505 spin_unlock(&ncm
->lock
);
506 status
= usb_ep_queue(ncm
->notify
, req
, GFP_ATOMIC
);
507 spin_lock(&ncm
->lock
);
509 ncm
->notify_req
= req
;
510 DBG(cdev
, "notify --> %d\n", status
);
515 * Context: ncm->lock held
517 static void ncm_notify(struct f_ncm
*ncm
)
520 * NOTE on most versions of Linux, host side cdc-ethernet
521 * won't listen for notifications until its netdevice opens.
522 * The first notification then sits in the FIFO for a long
523 * time, and the second one is queued.
525 * If ncm_notify() is called before the second (CONNECT)
526 * notification is sent, then it will reset to send the SPEED
527 * notificaion again (and again, and again), but it's not a problem
529 ncm
->notify_state
= NCM_NOTIFY_SPEED
;
533 static void ncm_notify_complete(struct usb_ep
*ep
, struct usb_request
*req
)
535 struct f_ncm
*ncm
= req
->context
;
536 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
537 struct usb_cdc_notification
*event
= req
->buf
;
539 spin_lock(&ncm
->lock
);
540 switch (req
->status
) {
542 VDBG(cdev
, "Notification %02x sent\n",
543 event
->bNotificationType
);
547 ncm
->notify_state
= NCM_NOTIFY_NONE
;
550 DBG(cdev
, "event %02x --> %d\n",
551 event
->bNotificationType
, req
->status
);
554 ncm
->notify_req
= req
;
556 spin_unlock(&ncm
->lock
);
559 static void ncm_ep0out_complete(struct usb_ep
*ep
, struct usb_request
*req
)
561 /* now for SET_NTB_INPUT_SIZE only */
563 struct usb_function
*f
= req
->context
;
564 struct f_ncm
*ncm
= func_to_ncm(f
);
565 struct usb_composite_dev
*cdev
= ep
->driver_data
;
568 if (req
->status
|| req
->actual
!= req
->length
) {
569 DBG(cdev
, "Bad control-OUT transfer\n");
573 in_size
= get_unaligned_le32(req
->buf
);
574 if (in_size
< USB_CDC_NCM_NTB_MIN_IN_SIZE
||
575 in_size
> le32_to_cpu(ntb_parameters
.dwNtbInMaxSize
)) {
576 DBG(cdev
, "Got wrong INPUT SIZE (%d) from host\n", in_size
);
580 ncm
->port
.fixed_in_len
= in_size
;
581 VDBG(cdev
, "Set NTB INPUT SIZE %d\n", in_size
);
589 static int ncm_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
591 struct f_ncm
*ncm
= func_to_ncm(f
);
592 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
593 struct usb_request
*req
= cdev
->req
;
594 int value
= -EOPNOTSUPP
;
595 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
596 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
597 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
600 * composite driver infrastructure handles everything except
601 * CDC class messages; interface activation uses set_alt().
603 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
604 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
605 | USB_CDC_SET_ETHERNET_PACKET_FILTER
:
607 * see 6.2.30: no data, wIndex = interface,
608 * wValue = packet filter bitmap
610 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
612 DBG(cdev
, "packet filter %02x\n", w_value
);
614 * REVISIT locking of cdc_filter. This assumes the UDC
615 * driver won't have a concurrent packet TX irq running on
616 * another CPU; or that if it does, this write is atomic...
618 ncm
->port
.cdc_filter
= w_value
;
623 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
624 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
625 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
626 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
627 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
628 * case USB_CDC_GET_ETHERNET_STATISTIC:
631 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
632 | USB_CDC_GET_NTB_PARAMETERS
:
634 if (w_length
== 0 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
636 value
= w_length
> sizeof ntb_parameters
?
637 sizeof ntb_parameters
: w_length
;
638 memcpy(req
->buf
, &ntb_parameters
, value
);
639 VDBG(cdev
, "Host asked NTB parameters\n");
642 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
643 | USB_CDC_GET_NTB_INPUT_SIZE
:
645 if (w_length
< 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
647 put_unaligned_le32(ncm
->port
.fixed_in_len
, req
->buf
);
649 VDBG(cdev
, "Host asked INPUT SIZE, sending %d\n",
650 ncm
->port
.fixed_in_len
);
653 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
654 | USB_CDC_SET_NTB_INPUT_SIZE
:
656 if (w_length
!= 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
658 req
->complete
= ncm_ep0out_complete
;
659 req
->length
= w_length
;
666 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
667 | USB_CDC_GET_NTB_FORMAT
:
671 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
673 format
= (ncm
->parser_opts
== &ndp16_opts
) ? 0x0000 : 0x0001;
674 put_unaligned_le16(format
, req
->buf
);
676 VDBG(cdev
, "Host asked NTB FORMAT, sending %d\n", format
);
680 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
681 | USB_CDC_SET_NTB_FORMAT
:
683 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
687 ncm
->parser_opts
= &ndp16_opts
;
688 DBG(cdev
, "NCM16 selected\n");
691 ncm
->parser_opts
= &ndp32_opts
;
692 DBG(cdev
, "NCM32 selected\n");
700 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
701 | USB_CDC_GET_CRC_MODE
:
705 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
707 is_crc
= ncm
->is_crc
? 0x0001 : 0x0000;
708 put_unaligned_le16(is_crc
, req
->buf
);
710 VDBG(cdev
, "Host asked CRC MODE, sending %d\n", is_crc
);
714 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
715 | USB_CDC_SET_CRC_MODE
:
719 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
724 ndp_hdr_crc
= NCM_NDP_HDR_NOCRC
;
725 DBG(cdev
, "non-CRC mode selected\n");
729 ndp_hdr_crc
= NCM_NDP_HDR_CRC
;
730 DBG(cdev
, "CRC mode selected\n");
735 ncm
->parser_opts
->ndp_sign
&= ~NCM_NDP_HDR_CRC_MASK
;
736 ncm
->parser_opts
->ndp_sign
|= ndp_hdr_crc
;
741 /* and disabled in ncm descriptor: */
742 /* case USB_CDC_GET_NET_ADDRESS: */
743 /* case USB_CDC_SET_NET_ADDRESS: */
744 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
745 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
749 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
750 ctrl
->bRequestType
, ctrl
->bRequest
,
751 w_value
, w_index
, w_length
);
754 /* respond with data transfer or status phase? */
756 DBG(cdev
, "ncm req%02x.%02x v%04x i%04x l%d\n",
757 ctrl
->bRequestType
, ctrl
->bRequest
,
758 w_value
, w_index
, w_length
);
761 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
763 ERROR(cdev
, "ncm req %02x.%02x response err %d\n",
764 ctrl
->bRequestType
, ctrl
->bRequest
,
768 /* device either stalls (value < 0) or reports success */
773 static int ncm_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
775 struct f_ncm
*ncm
= func_to_ncm(f
);
776 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
778 /* Control interface has only altsetting 0 */
779 if (intf
== ncm
->ctrl_id
) {
783 if (ncm
->notify
->driver_data
) {
784 DBG(cdev
, "reset ncm control %d\n", intf
);
785 usb_ep_disable(ncm
->notify
);
788 if (!(ncm
->notify
->desc
)) {
789 DBG(cdev
, "init ncm ctrl %d\n", intf
);
790 if (config_ep_by_speed(cdev
->gadget
, f
, ncm
->notify
))
793 usb_ep_enable(ncm
->notify
);
794 ncm
->notify
->driver_data
= ncm
;
796 /* Data interface has two altsettings, 0 and 1 */
797 } else if (intf
== ncm
->data_id
) {
801 if (ncm
->port
.in_ep
->driver_data
) {
802 DBG(cdev
, "reset ncm\n");
803 gether_disconnect(&ncm
->port
);
804 ncm_reset_values(ncm
);
808 * CDC Network only sends data in non-default altsettings.
809 * Changing altsettings resets filters, statistics, etc.
812 struct net_device
*net
;
814 if (!ncm
->port
.in_ep
->desc
||
815 !ncm
->port
.out_ep
->desc
) {
816 DBG(cdev
, "init ncm\n");
817 if (config_ep_by_speed(cdev
->gadget
, f
,
819 config_ep_by_speed(cdev
->gadget
, f
,
821 ncm
->port
.in_ep
->desc
= NULL
;
822 ncm
->port
.out_ep
->desc
= NULL
;
828 /* Enable zlps by default for NCM conformance;
829 * override for musb_hdrc (avoids txdma ovhead)
831 ncm
->port
.is_zlp_ok
= !(
832 gadget_is_musbhdrc(cdev
->gadget
)
834 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
835 DBG(cdev
, "activate ncm\n");
836 net
= gether_connect(&ncm
->port
);
841 spin_lock(&ncm
->lock
);
843 spin_unlock(&ncm
->lock
);
853 * Because the data interface supports multiple altsettings,
854 * this NCM function *MUST* implement a get_alt() method.
856 static int ncm_get_alt(struct usb_function
*f
, unsigned intf
)
858 struct f_ncm
*ncm
= func_to_ncm(f
);
860 if (intf
== ncm
->ctrl_id
)
862 return ncm
->port
.in_ep
->driver_data
? 1 : 0;
865 static struct sk_buff
*ncm_wrap_ntb(struct gether
*port
,
868 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
869 struct sk_buff
*skb2
;
872 int div
= ntb_parameters
.wNdpInDivisor
;
873 int rem
= ntb_parameters
.wNdpInPayloadRemainder
;
875 int ndp_align
= ntb_parameters
.wNdpInAlignment
;
877 unsigned max_size
= ncm
->port
.fixed_in_len
;
878 struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
879 unsigned crc_len
= ncm
->is_crc
? sizeof(uint32_t) : 0;
881 ncb_len
+= opts
->nth_size
;
882 ndp_pad
= ALIGN(ncb_len
, ndp_align
) - ncb_len
;
884 ncb_len
+= opts
->ndp_size
;
885 ncb_len
+= 2 * 2 * opts
->dgram_item_len
; /* Datagram entry */
886 ncb_len
+= 2 * 2 * opts
->dgram_item_len
; /* Zero datagram entry */
887 pad
= ALIGN(ncb_len
, div
) + rem
- ncb_len
;
890 if (ncb_len
+ skb
->len
+ crc_len
> max_size
) {
891 dev_kfree_skb_any(skb
);
895 skb2
= skb_copy_expand(skb
, ncb_len
,
896 max_size
- skb
->len
- ncb_len
- crc_len
,
898 dev_kfree_skb_any(skb
);
904 tmp
= (void *) skb_push(skb
, ncb_len
);
905 memset(tmp
, 0, ncb_len
);
907 put_unaligned_le32(opts
->nth_sign
, tmp
); /* dwSignature */
910 put_unaligned_le16(opts
->nth_size
, tmp
++);
911 tmp
++; /* skip wSequence */
912 put_ncm(&tmp
, opts
->block_length
, skb
->len
); /* (d)wBlockLength */
914 /* the first pointer is right after the NTH + align */
915 put_ncm(&tmp
, opts
->fp_index
, opts
->nth_size
+ ndp_pad
);
917 tmp
= (void *)tmp
+ ndp_pad
;
920 put_unaligned_le32(opts
->ndp_sign
, tmp
); /* dwSignature */
923 put_unaligned_le16(ncb_len
- opts
->nth_size
- pad
, tmp
++);
925 tmp
+= opts
->reserved1
;
926 tmp
+= opts
->next_fp_index
; /* skip reserved (d)wNextFpIndex */
927 tmp
+= opts
->reserved2
;
935 put_unaligned_le32(crc
, skb
->data
+ skb
->len
);
936 skb_put(skb
, crc_len
);
939 /* (d)wDatagramIndex[0] */
940 put_ncm(&tmp
, opts
->dgram_item_len
, ncb_len
);
941 /* (d)wDatagramLength[0] */
942 put_ncm(&tmp
, opts
->dgram_item_len
, skb
->len
- ncb_len
);
943 /* (d)wDatagramIndex[1] and (d)wDatagramLength[1] already zeroed */
945 if (skb
->len
> MAX_TX_NONFIXED
)
946 memset(skb_put(skb
, max_size
- skb
->len
),
947 0, max_size
- skb
->len
);
952 static int ncm_unwrap_ntb(struct gether
*port
,
954 struct sk_buff_head
*list
)
956 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
957 __le16
*tmp
= (void *) skb
->data
;
958 unsigned index
, index2
;
959 unsigned dg_len
, dg_len2
;
961 struct sk_buff
*skb2
;
963 unsigned max_size
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
964 struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
965 unsigned crc_len
= ncm
->is_crc
? sizeof(uint32_t) : 0;
969 if (get_unaligned_le32(tmp
) != opts
->nth_sign
) {
970 INFO(port
->func
.config
->cdev
, "Wrong NTH SIGN, skblen %d\n",
972 print_hex_dump(KERN_INFO
, "HEAD:", DUMP_PREFIX_ADDRESS
, 32, 1,
973 skb
->data
, 32, false);
979 if (get_unaligned_le16(tmp
++) != opts
->nth_size
) {
980 INFO(port
->func
.config
->cdev
, "Wrong NTB headersize\n");
983 tmp
++; /* skip wSequence */
985 /* (d)wBlockLength */
986 if (get_ncm(&tmp
, opts
->block_length
) > max_size
) {
987 INFO(port
->func
.config
->cdev
, "OUT size exceeded\n");
991 index
= get_ncm(&tmp
, opts
->fp_index
);
993 if (((index
% 4) != 0) && (index
< opts
->nth_size
)) {
994 INFO(port
->func
.config
->cdev
, "Bad index: %x\n",
999 /* walk through NDP */
1000 tmp
= ((void *)skb
->data
) + index
;
1001 if (get_unaligned_le32(tmp
) != opts
->ndp_sign
) {
1002 INFO(port
->func
.config
->cdev
, "Wrong NDP SIGN\n");
1007 ndp_len
= get_unaligned_le16(tmp
++);
1011 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1012 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1014 if ((ndp_len
< opts
->ndp_size
+ 2 * 2 * (opts
->dgram_item_len
* 2))
1015 || (ndp_len
% opts
->ndplen_align
!= 0)) {
1016 INFO(port
->func
.config
->cdev
, "Bad NDP length: %x\n", ndp_len
);
1019 tmp
+= opts
->reserved1
;
1020 tmp
+= opts
->next_fp_index
; /* skip reserved (d)wNextFpIndex */
1021 tmp
+= opts
->reserved2
;
1023 ndp_len
-= opts
->ndp_size
;
1024 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1025 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1031 if (dg_len
< 14 + crc_len
) { /* ethernet header + crc */
1032 INFO(port
->func
.config
->cdev
, "Bad dgram length: %x\n",
1039 crc
= get_unaligned_le32(skb
->data
+
1040 index
+ dg_len
- crc_len
);
1041 crc2
= ~crc32_le(~0,
1045 INFO(port
->func
.config
->cdev
, "Bad CRC\n");
1050 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1051 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1053 if (index2
== 0 || dg_len2
== 0) {
1056 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1061 if (!skb_pull(skb2
, index
)) {
1066 skb_trim(skb2
, dg_len
- crc_len
);
1067 skb_queue_tail(list
, skb2
);
1069 ndp_len
-= 2 * (opts
->dgram_item_len
* 2);
1073 if (index2
== 0 || dg_len2
== 0)
1075 } while (ndp_len
> 2 * (opts
->dgram_item_len
* 2)); /* zero entry */
1077 VDBG(port
->func
.config
->cdev
,
1078 "Parsed NTB with %d frames\n", dgram_counter
);
1081 skb_queue_purge(list
);
1082 dev_kfree_skb_any(skb
);
1086 static void ncm_disable(struct usb_function
*f
)
1088 struct f_ncm
*ncm
= func_to_ncm(f
);
1089 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
1091 DBG(cdev
, "ncm deactivated\n");
1093 if (ncm
->port
.in_ep
->driver_data
)
1094 gether_disconnect(&ncm
->port
);
1096 if (ncm
->notify
->driver_data
) {
1097 usb_ep_disable(ncm
->notify
);
1098 ncm
->notify
->driver_data
= NULL
;
1099 ncm
->notify
->desc
= NULL
;
1103 /*-------------------------------------------------------------------------*/
1106 * Callbacks let us notify the host about connect/disconnect when the
1107 * net device is opened or closed.
1109 * For testing, note that link states on this side include both opened
1110 * and closed variants of:
1112 * - disconnected/unconfigured
1113 * - configured but inactive (data alt 0)
1114 * - configured and active (data alt 1)
1116 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1117 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1118 * imply the host is actually polling the notification endpoint, and
1119 * likewise that "active" doesn't imply it's actually using the data
1120 * endpoints for traffic.
1123 static void ncm_open(struct gether
*geth
)
1125 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1127 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1129 spin_lock(&ncm
->lock
);
1130 ncm
->is_open
= true;
1132 spin_unlock(&ncm
->lock
);
1135 static void ncm_close(struct gether
*geth
)
1137 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1139 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1141 spin_lock(&ncm
->lock
);
1142 ncm
->is_open
= false;
1144 spin_unlock(&ncm
->lock
);
1147 /*-------------------------------------------------------------------------*/
1149 /* ethernet function driver setup/binding */
1152 ncm_bind(struct usb_configuration
*c
, struct usb_function
*f
)
1154 struct usb_composite_dev
*cdev
= c
->cdev
;
1155 struct f_ncm
*ncm
= func_to_ncm(f
);
1159 /* allocate instance-specific interface IDs */
1160 status
= usb_interface_id(c
, f
);
1163 ncm
->ctrl_id
= status
;
1164 ncm_iad_desc
.bFirstInterface
= status
;
1166 ncm_control_intf
.bInterfaceNumber
= status
;
1167 ncm_union_desc
.bMasterInterface0
= status
;
1169 status
= usb_interface_id(c
, f
);
1172 ncm
->data_id
= status
;
1174 ncm_data_nop_intf
.bInterfaceNumber
= status
;
1175 ncm_data_intf
.bInterfaceNumber
= status
;
1176 ncm_union_desc
.bSlaveInterface0
= status
;
1180 /* allocate instance-specific endpoints */
1181 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_in_desc
);
1184 ncm
->port
.in_ep
= ep
;
1185 ep
->driver_data
= cdev
; /* claim */
1187 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_out_desc
);
1190 ncm
->port
.out_ep
= ep
;
1191 ep
->driver_data
= cdev
; /* claim */
1193 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_notify_desc
);
1197 ep
->driver_data
= cdev
; /* claim */
1201 /* allocate notification request and buffer */
1202 ncm
->notify_req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
1203 if (!ncm
->notify_req
)
1205 ncm
->notify_req
->buf
= kmalloc(NCM_STATUS_BYTECOUNT
, GFP_KERNEL
);
1206 if (!ncm
->notify_req
->buf
)
1208 ncm
->notify_req
->context
= ncm
;
1209 ncm
->notify_req
->complete
= ncm_notify_complete
;
1211 /* copy descriptors, and track endpoint copies */
1212 f
->descriptors
= usb_copy_descriptors(ncm_fs_function
);
1213 if (!f
->descriptors
)
1217 * support all relevant hardware speeds... we expect that when
1218 * hardware is dual speed, all bulk-capable endpoints work at
1221 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
1222 hs_ncm_in_desc
.bEndpointAddress
=
1223 fs_ncm_in_desc
.bEndpointAddress
;
1224 hs_ncm_out_desc
.bEndpointAddress
=
1225 fs_ncm_out_desc
.bEndpointAddress
;
1226 hs_ncm_notify_desc
.bEndpointAddress
=
1227 fs_ncm_notify_desc
.bEndpointAddress
;
1229 /* copy descriptors, and track endpoint copies */
1230 f
->hs_descriptors
= usb_copy_descriptors(ncm_hs_function
);
1231 if (!f
->hs_descriptors
)
1236 * NOTE: all that is done without knowing or caring about
1237 * the network link ... which is unavailable to this code
1238 * until we're activated via set_alt().
1241 ncm
->port
.open
= ncm_open
;
1242 ncm
->port
.close
= ncm_close
;
1244 DBG(cdev
, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1245 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
1246 ncm
->port
.in_ep
->name
, ncm
->port
.out_ep
->name
,
1252 usb_free_descriptors(f
->descriptors
);
1254 if (ncm
->notify_req
) {
1255 kfree(ncm
->notify_req
->buf
);
1256 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1259 /* we might as well release our claims on endpoints */
1261 ncm
->notify
->driver_data
= NULL
;
1262 if (ncm
->port
.out_ep
->desc
)
1263 ncm
->port
.out_ep
->driver_data
= NULL
;
1264 if (ncm
->port
.in_ep
->desc
)
1265 ncm
->port
.in_ep
->driver_data
= NULL
;
1267 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
1273 ncm_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1275 struct f_ncm
*ncm
= func_to_ncm(f
);
1277 DBG(c
->cdev
, "ncm unbind\n");
1279 if (gadget_is_dualspeed(c
->cdev
->gadget
))
1280 usb_free_descriptors(f
->hs_descriptors
);
1281 usb_free_descriptors(f
->descriptors
);
1283 kfree(ncm
->notify_req
->buf
);
1284 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1286 ncm_string_defs
[1].s
= NULL
;
1291 * ncm_bind_config - add CDC Network link to a configuration
1292 * @c: the configuration to support the network link
1293 * @ethaddr: a buffer in which the ethernet address of the host side
1294 * side of the link was recorded
1295 * Context: single threaded during gadget setup
1297 * Returns zero on success, else negative errno.
1299 * Caller must have called @gether_setup(). Caller is also responsible
1300 * for calling @gether_cleanup() before module unload.
1302 int __init
ncm_bind_config(struct usb_configuration
*c
, u8 ethaddr
[ETH_ALEN
])
1307 if (!can_support_ecm(c
->cdev
->gadget
) || !ethaddr
)
1310 /* maybe allocate device-global string IDs */
1311 if (ncm_string_defs
[0].id
== 0) {
1313 /* control interface label */
1314 status
= usb_string_id(c
->cdev
);
1317 ncm_string_defs
[STRING_CTRL_IDX
].id
= status
;
1318 ncm_control_intf
.iInterface
= status
;
1320 /* data interface label */
1321 status
= usb_string_id(c
->cdev
);
1324 ncm_string_defs
[STRING_DATA_IDX
].id
= status
;
1325 ncm_data_nop_intf
.iInterface
= status
;
1326 ncm_data_intf
.iInterface
= status
;
1329 status
= usb_string_id(c
->cdev
);
1332 ncm_string_defs
[STRING_MAC_IDX
].id
= status
;
1333 ecm_desc
.iMACAddress
= status
;
1336 status
= usb_string_id(c
->cdev
);
1339 ncm_string_defs
[STRING_IAD_IDX
].id
= status
;
1340 ncm_iad_desc
.iFunction
= status
;
1343 /* allocate and initialize one new instance */
1344 ncm
= kzalloc(sizeof *ncm
, GFP_KERNEL
);
1348 /* export host's Ethernet address in CDC format */
1349 snprintf(ncm
->ethaddr
, sizeof ncm
->ethaddr
,
1350 "%02X%02X%02X%02X%02X%02X",
1351 ethaddr
[0], ethaddr
[1], ethaddr
[2],
1352 ethaddr
[3], ethaddr
[4], ethaddr
[5]);
1353 ncm_string_defs
[1].s
= ncm
->ethaddr
;
1355 spin_lock_init(&ncm
->lock
);
1356 ncm_reset_values(ncm
);
1357 ncm
->port
.is_fixed
= true;
1359 ncm
->port
.func
.name
= "cdc_network";
1360 ncm
->port
.func
.strings
= ncm_strings
;
1361 /* descriptors are per-instance copies */
1362 ncm
->port
.func
.bind
= ncm_bind
;
1363 ncm
->port
.func
.unbind
= ncm_unbind
;
1364 ncm
->port
.func
.set_alt
= ncm_set_alt
;
1365 ncm
->port
.func
.get_alt
= ncm_get_alt
;
1366 ncm
->port
.func
.setup
= ncm_setup
;
1367 ncm
->port
.func
.disable
= ncm_disable
;
1369 ncm
->port
.wrap
= ncm_wrap_ntb
;
1370 ncm
->port
.unwrap
= ncm_unwrap_ntb
;
1372 status
= usb_add_function(c
, &ncm
->port
.func
);
1374 ncm_string_defs
[1].s
= NULL
;