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.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/kernel.h>
28 #include <linux/device.h>
29 #include <linux/etherdevice.h>
30 #include <linux/crc32.h>
32 #include <linux/usb/cdc.h>
37 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
38 * NCM is intended to be used with high-speed network attachments.
40 * Note that NCM requires the use of "alternate settings" for its data
41 * interface. This means that the set_alt() method has real work to do,
42 * and also means that a get_alt() method is required.
45 /* to trigger crc/non-crc ndp signature */
47 #define NCM_NDP_HDR_CRC_MASK 0x01000000
48 #define NCM_NDP_HDR_CRC 0x01000000
49 #define NCM_NDP_HDR_NOCRC 0x00000000
52 struct usb_endpoint_descriptor
*in
;
53 struct usb_endpoint_descriptor
*out
;
54 struct usb_endpoint_descriptor
*notify
;
57 enum ncm_notify_state
{
58 NCM_NOTIFY_NONE
, /* don't notify */
59 NCM_NOTIFY_CONNECT
, /* issue CONNECT next */
60 NCM_NOTIFY_SPEED
, /* issue SPEED_CHANGE next */
69 struct ncm_ep_descs fs
;
70 struct ncm_ep_descs hs
;
72 struct usb_ep
*notify
;
73 struct usb_endpoint_descriptor
*notify_desc
;
74 struct usb_request
*notify_req
;
78 struct ndp_parser_opts
*parser_opts
;
82 * for notification, it is accessed from both
83 * callback and ethernet open/close
88 static inline struct f_ncm
*func_to_ncm(struct usb_function
*f
)
90 return container_of(f
, struct f_ncm
, port
.func
);
93 /* peak (theoretical) bulk transfer rate in bits-per-second */
94 static inline unsigned ncm_bitrate(struct usb_gadget
*g
)
96 if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
97 return 13 * 512 * 8 * 1000 * 8;
99 return 19 * 64 * 1 * 1000 * 8;
102 /*-------------------------------------------------------------------------*/
105 * We cannot group frames so use just the minimal size which ok to put
106 * one max-size ethernet frame.
107 * If the host can group frames, allow it to do that, 16K is selected,
108 * because it's used by default by the current linux host driver
110 #define NTB_DEFAULT_IN_SIZE USB_CDC_NCM_NTB_MIN_IN_SIZE
111 #define NTB_OUT_SIZE 16384
114 * skbs of size less than that will not be alligned
115 * to NCM's dwNtbInMaxSize to save bus bandwidth
118 #define MAX_TX_NONFIXED (512 * 3)
120 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
121 USB_CDC_NCM_NTB32_SUPPORTED)
123 static struct usb_cdc_ncm_ntb_parameters ntb_parameters
= {
124 .wLength
= sizeof ntb_parameters
,
125 .bmNtbFormatsSupported
= cpu_to_le16(FORMATS_SUPPORTED
),
126 .dwNtbInMaxSize
= cpu_to_le32(NTB_DEFAULT_IN_SIZE
),
127 .wNdpInDivisor
= cpu_to_le16(4),
128 .wNdpInPayloadRemainder
= cpu_to_le16(0),
129 .wNdpInAlignment
= cpu_to_le16(4),
131 .dwNtbOutMaxSize
= cpu_to_le32(NTB_OUT_SIZE
),
132 .wNdpOutDivisor
= cpu_to_le16(4),
133 .wNdpOutPayloadRemainder
= cpu_to_le16(0),
134 .wNdpOutAlignment
= cpu_to_le16(4),
138 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
139 * packet, to simplify cancellation; and a big transfer interval, to
140 * waste less bandwidth.
143 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
144 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
146 static struct usb_interface_assoc_descriptor ncm_iad_desc __initdata
= {
147 .bLength
= sizeof ncm_iad_desc
,
148 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
150 /* .bFirstInterface = DYNAMIC, */
151 .bInterfaceCount
= 2, /* control + data */
152 .bFunctionClass
= USB_CLASS_COMM
,
153 .bFunctionSubClass
= USB_CDC_SUBCLASS_NCM
,
154 .bFunctionProtocol
= USB_CDC_PROTO_NONE
,
155 /* .iFunction = DYNAMIC */
158 /* interface descriptor: */
160 static struct usb_interface_descriptor ncm_control_intf __initdata
= {
161 .bLength
= sizeof ncm_control_intf
,
162 .bDescriptorType
= USB_DT_INTERFACE
,
164 /* .bInterfaceNumber = DYNAMIC */
166 .bInterfaceClass
= USB_CLASS_COMM
,
167 .bInterfaceSubClass
= USB_CDC_SUBCLASS_NCM
,
168 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
169 /* .iInterface = DYNAMIC */
172 static struct usb_cdc_header_desc ncm_header_desc __initdata
= {
173 .bLength
= sizeof ncm_header_desc
,
174 .bDescriptorType
= USB_DT_CS_INTERFACE
,
175 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
177 .bcdCDC
= cpu_to_le16(0x0110),
180 static struct usb_cdc_union_desc ncm_union_desc __initdata
= {
181 .bLength
= sizeof(ncm_union_desc
),
182 .bDescriptorType
= USB_DT_CS_INTERFACE
,
183 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
184 /* .bMasterInterface0 = DYNAMIC */
185 /* .bSlaveInterface0 = DYNAMIC */
188 static struct usb_cdc_ether_desc ecm_desc __initdata
= {
189 .bLength
= sizeof ecm_desc
,
190 .bDescriptorType
= USB_DT_CS_INTERFACE
,
191 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
193 /* this descriptor actually adds value, surprise! */
194 /* .iMACAddress = DYNAMIC */
195 .bmEthernetStatistics
= cpu_to_le32(0), /* no statistics */
196 .wMaxSegmentSize
= cpu_to_le16(ETH_FRAME_LEN
),
197 .wNumberMCFilters
= cpu_to_le16(0),
198 .bNumberPowerFilters
= 0,
201 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
203 static struct usb_cdc_ncm_desc ncm_desc __initdata
= {
204 .bLength
= sizeof ncm_desc
,
205 .bDescriptorType
= USB_DT_CS_INTERFACE
,
206 .bDescriptorSubType
= USB_CDC_NCM_TYPE
,
208 .bcdNcmVersion
= cpu_to_le16(0x0100),
209 /* can process SetEthernetPacketFilter */
210 .bmNetworkCapabilities
= NCAPS
,
213 /* the default data interface has no endpoints ... */
215 static struct usb_interface_descriptor ncm_data_nop_intf __initdata
= {
216 .bLength
= sizeof ncm_data_nop_intf
,
217 .bDescriptorType
= USB_DT_INTERFACE
,
219 .bInterfaceNumber
= 1,
220 .bAlternateSetting
= 0,
222 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
223 .bInterfaceSubClass
= 0,
224 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
225 /* .iInterface = DYNAMIC */
228 /* ... but the "real" data interface has two bulk endpoints */
230 static struct usb_interface_descriptor ncm_data_intf __initdata
= {
231 .bLength
= sizeof ncm_data_intf
,
232 .bDescriptorType
= USB_DT_INTERFACE
,
234 .bInterfaceNumber
= 1,
235 .bAlternateSetting
= 1,
237 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
238 .bInterfaceSubClass
= 0,
239 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
240 /* .iInterface = DYNAMIC */
243 /* full speed support: */
245 static struct usb_endpoint_descriptor fs_ncm_notify_desc __initdata
= {
246 .bLength
= USB_DT_ENDPOINT_SIZE
,
247 .bDescriptorType
= USB_DT_ENDPOINT
,
249 .bEndpointAddress
= USB_DIR_IN
,
250 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
251 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
252 .bInterval
= 1 << LOG2_STATUS_INTERVAL_MSEC
,
255 static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata
= {
256 .bLength
= USB_DT_ENDPOINT_SIZE
,
257 .bDescriptorType
= USB_DT_ENDPOINT
,
259 .bEndpointAddress
= USB_DIR_IN
,
260 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
263 static struct usb_endpoint_descriptor fs_ncm_out_desc __initdata
= {
264 .bLength
= USB_DT_ENDPOINT_SIZE
,
265 .bDescriptorType
= USB_DT_ENDPOINT
,
267 .bEndpointAddress
= USB_DIR_OUT
,
268 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
271 static struct usb_descriptor_header
*ncm_fs_function
[] __initdata
= {
272 (struct usb_descriptor_header
*) &ncm_iad_desc
,
273 /* CDC NCM control descriptors */
274 (struct usb_descriptor_header
*) &ncm_control_intf
,
275 (struct usb_descriptor_header
*) &ncm_header_desc
,
276 (struct usb_descriptor_header
*) &ncm_union_desc
,
277 (struct usb_descriptor_header
*) &ecm_desc
,
278 (struct usb_descriptor_header
*) &ncm_desc
,
279 (struct usb_descriptor_header
*) &fs_ncm_notify_desc
,
280 /* data interface, altsettings 0 and 1 */
281 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
282 (struct usb_descriptor_header
*) &ncm_data_intf
,
283 (struct usb_descriptor_header
*) &fs_ncm_in_desc
,
284 (struct usb_descriptor_header
*) &fs_ncm_out_desc
,
288 /* high speed support: */
290 static struct usb_endpoint_descriptor hs_ncm_notify_desc __initdata
= {
291 .bLength
= USB_DT_ENDPOINT_SIZE
,
292 .bDescriptorType
= USB_DT_ENDPOINT
,
294 .bEndpointAddress
= USB_DIR_IN
,
295 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
296 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
297 .bInterval
= LOG2_STATUS_INTERVAL_MSEC
+ 4,
299 static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata
= {
300 .bLength
= USB_DT_ENDPOINT_SIZE
,
301 .bDescriptorType
= USB_DT_ENDPOINT
,
303 .bEndpointAddress
= USB_DIR_IN
,
304 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
305 .wMaxPacketSize
= cpu_to_le16(512),
308 static struct usb_endpoint_descriptor hs_ncm_out_desc __initdata
= {
309 .bLength
= USB_DT_ENDPOINT_SIZE
,
310 .bDescriptorType
= USB_DT_ENDPOINT
,
312 .bEndpointAddress
= USB_DIR_OUT
,
313 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
314 .wMaxPacketSize
= cpu_to_le16(512),
317 static struct usb_descriptor_header
*ncm_hs_function
[] __initdata
= {
318 (struct usb_descriptor_header
*) &ncm_iad_desc
,
319 /* CDC NCM control descriptors */
320 (struct usb_descriptor_header
*) &ncm_control_intf
,
321 (struct usb_descriptor_header
*) &ncm_header_desc
,
322 (struct usb_descriptor_header
*) &ncm_union_desc
,
323 (struct usb_descriptor_header
*) &ecm_desc
,
324 (struct usb_descriptor_header
*) &ncm_desc
,
325 (struct usb_descriptor_header
*) &hs_ncm_notify_desc
,
326 /* data interface, altsettings 0 and 1 */
327 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
328 (struct usb_descriptor_header
*) &ncm_data_intf
,
329 (struct usb_descriptor_header
*) &hs_ncm_in_desc
,
330 (struct usb_descriptor_header
*) &hs_ncm_out_desc
,
334 /* string descriptors: */
336 #define STRING_CTRL_IDX 0
337 #define STRING_MAC_IDX 1
338 #define STRING_DATA_IDX 2
339 #define STRING_IAD_IDX 3
341 static struct usb_string ncm_string_defs
[] = {
342 [STRING_CTRL_IDX
].s
= "CDC Network Control Model (NCM)",
343 [STRING_MAC_IDX
].s
= NULL
/* DYNAMIC */,
344 [STRING_DATA_IDX
].s
= "CDC Network Data",
345 [STRING_IAD_IDX
].s
= "CDC NCM",
346 { } /* end of list */
349 static struct usb_gadget_strings ncm_string_table
= {
350 .language
= 0x0409, /* en-us */
351 .strings
= ncm_string_defs
,
354 static struct usb_gadget_strings
*ncm_strings
[] = {
360 * Here are options for NCM Datagram Pointer table (NDP) parser.
361 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
362 * in NDP16 offsets and sizes fields are 1 16bit word wide,
363 * in NDP32 -- 2 16bit words wide. Also signatures are different.
364 * To make the parser code the same, put the differences in the structure,
365 * and switch pointers to the structures when the format is changed.
368 struct ndp_parser_opts
{
373 unsigned ndplen_align
;
374 /* sizes in u16 units */
375 unsigned dgram_item_len
; /* index or length */
376 unsigned block_length
;
380 unsigned next_fp_index
;
383 #define INIT_NDP16_OPTS { \
384 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
385 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
386 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
387 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
389 .dgram_item_len = 1, \
394 .next_fp_index = 1, \
398 #define INIT_NDP32_OPTS { \
399 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
400 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
401 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
402 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
404 .dgram_item_len = 2, \
409 .next_fp_index = 2, \
412 static struct ndp_parser_opts ndp16_opts
= INIT_NDP16_OPTS
;
413 static struct ndp_parser_opts ndp32_opts
= INIT_NDP32_OPTS
;
415 static inline void put_ncm(__le16
**p
, unsigned size
, unsigned val
)
419 put_unaligned_le16((u16
)val
, *p
);
422 put_unaligned_le32((u32
)val
, *p
);
432 static inline unsigned get_ncm(__le16
**p
, unsigned size
)
438 tmp
= get_unaligned_le16(*p
);
441 tmp
= get_unaligned_le32(*p
);
451 /*-------------------------------------------------------------------------*/
453 static inline void ncm_reset_values(struct f_ncm
*ncm
)
455 ncm
->parser_opts
= &ndp16_opts
;
457 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
459 /* doesn't make sense for ncm, fixed size used */
460 ncm
->port
.header_len
= 0;
462 ncm
->port
.fixed_out_len
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
463 ncm
->port
.fixed_in_len
= NTB_DEFAULT_IN_SIZE
;
467 * Context: ncm->lock held
469 static void ncm_do_notify(struct f_ncm
*ncm
)
471 struct usb_request
*req
= ncm
->notify_req
;
472 struct usb_cdc_notification
*event
;
473 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
477 /* notification already in flight? */
482 switch (ncm
->notify_state
) {
483 case NCM_NOTIFY_NONE
:
486 case NCM_NOTIFY_CONNECT
:
487 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
489 event
->wValue
= cpu_to_le16(1);
491 event
->wValue
= cpu_to_le16(0);
493 req
->length
= sizeof *event
;
495 DBG(cdev
, "notify connect %s\n",
496 ncm
->is_open
? "true" : "false");
497 ncm
->notify_state
= NCM_NOTIFY_NONE
;
500 case NCM_NOTIFY_SPEED
:
501 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
502 event
->wValue
= cpu_to_le16(0);
503 event
->wLength
= cpu_to_le16(8);
504 req
->length
= NCM_STATUS_BYTECOUNT
;
506 /* SPEED_CHANGE data is up/down speeds in bits/sec */
507 data
= req
->buf
+ sizeof *event
;
508 data
[0] = cpu_to_le32(ncm_bitrate(cdev
->gadget
));
511 DBG(cdev
, "notify speed %d\n", ncm_bitrate(cdev
->gadget
));
512 ncm
->notify_state
= NCM_NOTIFY_CONNECT
;
515 event
->bmRequestType
= 0xA1;
516 event
->wIndex
= cpu_to_le16(ncm
->ctrl_id
);
518 ncm
->notify_req
= NULL
;
520 * In double buffering if there is a space in FIFO,
521 * completion callback can be called right after the call,
524 spin_unlock(&ncm
->lock
);
525 status
= usb_ep_queue(ncm
->notify
, req
, GFP_ATOMIC
);
526 spin_lock(&ncm
->lock
);
528 ncm
->notify_req
= req
;
529 DBG(cdev
, "notify --> %d\n", status
);
534 * Context: ncm->lock held
536 static void ncm_notify(struct f_ncm
*ncm
)
539 * NOTE on most versions of Linux, host side cdc-ethernet
540 * won't listen for notifications until its netdevice opens.
541 * The first notification then sits in the FIFO for a long
542 * time, and the second one is queued.
544 * If ncm_notify() is called before the second (CONNECT)
545 * notification is sent, then it will reset to send the SPEED
546 * notificaion again (and again, and again), but it's not a problem
548 ncm
->notify_state
= NCM_NOTIFY_SPEED
;
552 static void ncm_notify_complete(struct usb_ep
*ep
, struct usb_request
*req
)
554 struct f_ncm
*ncm
= req
->context
;
555 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
556 struct usb_cdc_notification
*event
= req
->buf
;
558 spin_lock(&ncm
->lock
);
559 switch (req
->status
) {
561 VDBG(cdev
, "Notification %02x sent\n",
562 event
->bNotificationType
);
566 ncm
->notify_state
= NCM_NOTIFY_NONE
;
569 DBG(cdev
, "event %02x --> %d\n",
570 event
->bNotificationType
, req
->status
);
573 ncm
->notify_req
= req
;
575 spin_unlock(&ncm
->lock
);
578 static void ncm_ep0out_complete(struct usb_ep
*ep
, struct usb_request
*req
)
580 /* now for SET_NTB_INPUT_SIZE only */
582 struct usb_function
*f
= req
->context
;
583 struct f_ncm
*ncm
= func_to_ncm(f
);
584 struct usb_composite_dev
*cdev
= ep
->driver_data
;
587 if (req
->status
|| req
->actual
!= req
->length
) {
588 DBG(cdev
, "Bad control-OUT transfer\n");
592 in_size
= get_unaligned_le32(req
->buf
);
593 if (in_size
< USB_CDC_NCM_NTB_MIN_IN_SIZE
||
594 in_size
> le32_to_cpu(ntb_parameters
.dwNtbInMaxSize
)) {
595 DBG(cdev
, "Got wrong INPUT SIZE (%d) from host\n", in_size
);
599 ncm
->port
.fixed_in_len
= in_size
;
600 VDBG(cdev
, "Set NTB INPUT SIZE %d\n", in_size
);
608 static int ncm_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
610 struct f_ncm
*ncm
= func_to_ncm(f
);
611 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
612 struct usb_request
*req
= cdev
->req
;
613 int value
= -EOPNOTSUPP
;
614 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
615 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
616 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
619 * composite driver infrastructure handles everything except
620 * CDC class messages; interface activation uses set_alt().
622 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
623 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
624 | USB_CDC_SET_ETHERNET_PACKET_FILTER
:
626 * see 6.2.30: no data, wIndex = interface,
627 * wValue = packet filter bitmap
629 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
631 DBG(cdev
, "packet filter %02x\n", w_value
);
633 * REVISIT locking of cdc_filter. This assumes the UDC
634 * driver won't have a concurrent packet TX irq running on
635 * another CPU; or that if it does, this write is atomic...
637 ncm
->port
.cdc_filter
= w_value
;
642 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
643 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
644 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
645 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
646 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
647 * case USB_CDC_GET_ETHERNET_STATISTIC:
650 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
651 | USB_CDC_GET_NTB_PARAMETERS
:
653 if (w_length
== 0 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
655 value
= w_length
> sizeof ntb_parameters
?
656 sizeof ntb_parameters
: w_length
;
657 memcpy(req
->buf
, &ntb_parameters
, value
);
658 VDBG(cdev
, "Host asked NTB parameters\n");
661 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
662 | USB_CDC_GET_NTB_INPUT_SIZE
:
664 if (w_length
< 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
666 put_unaligned_le32(ncm
->port
.fixed_in_len
, req
->buf
);
668 VDBG(cdev
, "Host asked INPUT SIZE, sending %d\n",
669 ncm
->port
.fixed_in_len
);
672 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
673 | USB_CDC_SET_NTB_INPUT_SIZE
:
675 if (w_length
!= 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
677 req
->complete
= ncm_ep0out_complete
;
678 req
->length
= w_length
;
685 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
686 | USB_CDC_GET_NTB_FORMAT
:
690 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
692 format
= (ncm
->parser_opts
== &ndp16_opts
) ? 0x0000 : 0x0001;
693 put_unaligned_le16(format
, req
->buf
);
695 VDBG(cdev
, "Host asked NTB FORMAT, sending %d\n", format
);
699 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
700 | USB_CDC_SET_NTB_FORMAT
:
702 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
706 ncm
->parser_opts
= &ndp16_opts
;
707 DBG(cdev
, "NCM16 selected\n");
710 ncm
->parser_opts
= &ndp32_opts
;
711 DBG(cdev
, "NCM32 selected\n");
719 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
720 | USB_CDC_GET_CRC_MODE
:
724 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
726 is_crc
= ncm
->is_crc
? 0x0001 : 0x0000;
727 put_unaligned_le16(is_crc
, req
->buf
);
729 VDBG(cdev
, "Host asked CRC MODE, sending %d\n", is_crc
);
733 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
734 | USB_CDC_SET_CRC_MODE
:
738 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
743 ndp_hdr_crc
= NCM_NDP_HDR_NOCRC
;
744 DBG(cdev
, "non-CRC mode selected\n");
748 ndp_hdr_crc
= NCM_NDP_HDR_CRC
;
749 DBG(cdev
, "CRC mode selected\n");
754 ncm
->parser_opts
->ndp_sign
&= ~NCM_NDP_HDR_CRC_MASK
;
755 ncm
->parser_opts
->ndp_sign
|= ndp_hdr_crc
;
760 /* and disabled in ncm descriptor: */
761 /* case USB_CDC_GET_NET_ADDRESS: */
762 /* case USB_CDC_SET_NET_ADDRESS: */
763 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
764 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
768 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
769 ctrl
->bRequestType
, ctrl
->bRequest
,
770 w_value
, w_index
, w_length
);
773 /* respond with data transfer or status phase? */
775 DBG(cdev
, "ncm req%02x.%02x v%04x i%04x l%d\n",
776 ctrl
->bRequestType
, ctrl
->bRequest
,
777 w_value
, w_index
, w_length
);
780 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
782 ERROR(cdev
, "ncm req %02x.%02x response err %d\n",
783 ctrl
->bRequestType
, ctrl
->bRequest
,
787 /* device either stalls (value < 0) or reports success */
792 static int ncm_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
794 struct f_ncm
*ncm
= func_to_ncm(f
);
795 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
797 /* Control interface has only altsetting 0 */
798 if (intf
== ncm
->ctrl_id
) {
802 if (ncm
->notify
->driver_data
) {
803 DBG(cdev
, "reset ncm control %d\n", intf
);
804 usb_ep_disable(ncm
->notify
);
806 DBG(cdev
, "init ncm ctrl %d\n", intf
);
807 ncm
->notify_desc
= ep_choose(cdev
->gadget
,
811 usb_ep_enable(ncm
->notify
, ncm
->notify_desc
);
812 ncm
->notify
->driver_data
= ncm
;
814 /* Data interface has two altsettings, 0 and 1 */
815 } else if (intf
== ncm
->data_id
) {
819 if (ncm
->port
.in_ep
->driver_data
) {
820 DBG(cdev
, "reset ncm\n");
821 gether_disconnect(&ncm
->port
);
822 ncm_reset_values(ncm
);
826 * CDC Network only sends data in non-default altsettings.
827 * Changing altsettings resets filters, statistics, etc.
830 struct net_device
*net
;
833 DBG(cdev
, "init ncm\n");
834 ncm
->port
.in
= ep_choose(cdev
->gadget
,
837 ncm
->port
.out
= ep_choose(cdev
->gadget
,
843 /* Enable zlps by default for NCM conformance;
844 * override for musb_hdrc (avoids txdma ovhead)
846 ncm
->port
.is_zlp_ok
= !(
847 gadget_is_musbhdrc(cdev
->gadget
)
849 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
850 DBG(cdev
, "activate ncm\n");
851 net
= gether_connect(&ncm
->port
);
856 spin_lock(&ncm
->lock
);
858 spin_unlock(&ncm
->lock
);
868 * Because the data interface supports multiple altsettings,
869 * this NCM function *MUST* implement a get_alt() method.
871 static int ncm_get_alt(struct usb_function
*f
, unsigned intf
)
873 struct f_ncm
*ncm
= func_to_ncm(f
);
875 if (intf
== ncm
->ctrl_id
)
877 return ncm
->port
.in_ep
->driver_data
? 1 : 0;
880 static struct sk_buff
*ncm_wrap_ntb(struct gether
*port
,
883 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
884 struct sk_buff
*skb2
;
887 int div
= ntb_parameters
.wNdpInDivisor
;
888 int rem
= ntb_parameters
.wNdpInPayloadRemainder
;
890 int ndp_align
= ntb_parameters
.wNdpInAlignment
;
892 unsigned max_size
= ncm
->port
.fixed_in_len
;
893 struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
894 unsigned crc_len
= ncm
->is_crc
? sizeof(uint32_t) : 0;
896 ncb_len
+= opts
->nth_size
;
897 ndp_pad
= ALIGN(ncb_len
, ndp_align
) - ncb_len
;
899 ncb_len
+= opts
->ndp_size
;
900 ncb_len
+= 2 * 2 * opts
->dgram_item_len
; /* Datagram entry */
901 ncb_len
+= 2 * 2 * opts
->dgram_item_len
; /* Zero datagram entry */
902 pad
= ALIGN(ncb_len
, div
) + rem
- ncb_len
;
905 if (ncb_len
+ skb
->len
+ crc_len
> max_size
) {
906 dev_kfree_skb_any(skb
);
910 skb2
= skb_copy_expand(skb
, ncb_len
,
911 max_size
- skb
->len
- ncb_len
- crc_len
,
913 dev_kfree_skb_any(skb
);
919 tmp
= (void *) skb_push(skb
, ncb_len
);
920 memset(tmp
, 0, ncb_len
);
922 put_unaligned_le32(opts
->nth_sign
, tmp
); /* dwSignature */
925 put_unaligned_le16(opts
->nth_size
, tmp
++);
926 tmp
++; /* skip wSequence */
927 put_ncm(&tmp
, opts
->block_length
, skb
->len
); /* (d)wBlockLength */
929 /* the first pointer is right after the NTH + align */
930 put_ncm(&tmp
, opts
->fp_index
, opts
->nth_size
+ ndp_pad
);
932 tmp
= (void *)tmp
+ ndp_pad
;
935 put_unaligned_le32(opts
->ndp_sign
, tmp
); /* dwSignature */
938 put_unaligned_le16(ncb_len
- opts
->nth_size
- pad
, tmp
++);
940 tmp
+= opts
->reserved1
;
941 tmp
+= opts
->next_fp_index
; /* skip reserved (d)wNextFpIndex */
942 tmp
+= opts
->reserved2
;
950 put_unaligned_le32(crc
, skb
->data
+ skb
->len
);
951 skb_put(skb
, crc_len
);
954 /* (d)wDatagramIndex[0] */
955 put_ncm(&tmp
, opts
->dgram_item_len
, ncb_len
);
956 /* (d)wDatagramLength[0] */
957 put_ncm(&tmp
, opts
->dgram_item_len
, skb
->len
- ncb_len
);
958 /* (d)wDatagramIndex[1] and (d)wDatagramLength[1] already zeroed */
960 if (skb
->len
> MAX_TX_NONFIXED
)
961 memset(skb_put(skb
, max_size
- skb
->len
),
962 0, max_size
- skb
->len
);
967 static int ncm_unwrap_ntb(struct gether
*port
,
969 struct sk_buff_head
*list
)
971 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
972 __le16
*tmp
= (void *) skb
->data
;
973 unsigned index
, index2
;
974 unsigned dg_len
, dg_len2
;
976 struct sk_buff
*skb2
;
978 unsigned max_size
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
979 struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
980 unsigned crc_len
= ncm
->is_crc
? sizeof(uint32_t) : 0;
984 if (get_unaligned_le32(tmp
) != opts
->nth_sign
) {
985 INFO(port
->func
.config
->cdev
, "Wrong NTH SIGN, skblen %d\n",
987 print_hex_dump(KERN_INFO
, "HEAD:", DUMP_PREFIX_ADDRESS
, 32, 1,
988 skb
->data
, 32, false);
994 if (get_unaligned_le16(tmp
++) != opts
->nth_size
) {
995 INFO(port
->func
.config
->cdev
, "Wrong NTB headersize\n");
998 tmp
++; /* skip wSequence */
1000 /* (d)wBlockLength */
1001 if (get_ncm(&tmp
, opts
->block_length
) > max_size
) {
1002 INFO(port
->func
.config
->cdev
, "OUT size exceeded\n");
1006 index
= get_ncm(&tmp
, opts
->fp_index
);
1008 if (((index
% 4) != 0) && (index
< opts
->nth_size
)) {
1009 INFO(port
->func
.config
->cdev
, "Bad index: %x\n",
1014 /* walk through NDP */
1015 tmp
= ((void *)skb
->data
) + index
;
1016 if (get_unaligned_le32(tmp
) != opts
->ndp_sign
) {
1017 INFO(port
->func
.config
->cdev
, "Wrong NDP SIGN\n");
1022 ndp_len
= get_unaligned_le16(tmp
++);
1026 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1027 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1029 if ((ndp_len
< opts
->ndp_size
+ 2 * 2 * (opts
->dgram_item_len
* 2))
1030 || (ndp_len
% opts
->ndplen_align
!= 0)) {
1031 INFO(port
->func
.config
->cdev
, "Bad NDP length: %x\n", ndp_len
);
1034 tmp
+= opts
->reserved1
;
1035 tmp
+= opts
->next_fp_index
; /* skip reserved (d)wNextFpIndex */
1036 tmp
+= opts
->reserved2
;
1038 ndp_len
-= opts
->ndp_size
;
1039 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1040 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1046 if (dg_len
< 14 + crc_len
) { /* ethernet header + crc */
1047 INFO(port
->func
.config
->cdev
, "Bad dgram length: %x\n",
1054 crc
= get_unaligned_le32(skb
->data
+
1055 index
+ dg_len
- crc_len
);
1056 crc2
= ~crc32_le(~0,
1060 INFO(port
->func
.config
->cdev
, "Bad CRC\n");
1065 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1066 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1068 if (index2
== 0 || dg_len2
== 0) {
1071 skb2
= skb_clone(skb
, GFP_ATOMIC
);
1076 if (!skb_pull(skb2
, index
)) {
1081 skb_trim(skb2
, dg_len
- crc_len
);
1082 skb_queue_tail(list
, skb2
);
1084 ndp_len
-= 2 * (opts
->dgram_item_len
* 2);
1088 if (index2
== 0 || dg_len2
== 0)
1090 } while (ndp_len
> 2 * (opts
->dgram_item_len
* 2)); /* zero entry */
1092 VDBG(port
->func
.config
->cdev
,
1093 "Parsed NTB with %d frames\n", dgram_counter
);
1096 skb_queue_purge(list
);
1097 dev_kfree_skb_any(skb
);
1101 static void ncm_disable(struct usb_function
*f
)
1103 struct f_ncm
*ncm
= func_to_ncm(f
);
1104 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
1106 DBG(cdev
, "ncm deactivated\n");
1108 if (ncm
->port
.in_ep
->driver_data
)
1109 gether_disconnect(&ncm
->port
);
1111 if (ncm
->notify
->driver_data
) {
1112 usb_ep_disable(ncm
->notify
);
1113 ncm
->notify
->driver_data
= NULL
;
1114 ncm
->notify_desc
= NULL
;
1118 /*-------------------------------------------------------------------------*/
1121 * Callbacks let us notify the host about connect/disconnect when the
1122 * net device is opened or closed.
1124 * For testing, note that link states on this side include both opened
1125 * and closed variants of:
1127 * - disconnected/unconfigured
1128 * - configured but inactive (data alt 0)
1129 * - configured and active (data alt 1)
1131 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1132 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1133 * imply the host is actually polling the notification endpoint, and
1134 * likewise that "active" doesn't imply it's actually using the data
1135 * endpoints for traffic.
1138 static void ncm_open(struct gether
*geth
)
1140 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1142 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1144 spin_lock(&ncm
->lock
);
1145 ncm
->is_open
= true;
1147 spin_unlock(&ncm
->lock
);
1150 static void ncm_close(struct gether
*geth
)
1152 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1154 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1156 spin_lock(&ncm
->lock
);
1157 ncm
->is_open
= false;
1159 spin_unlock(&ncm
->lock
);
1162 /*-------------------------------------------------------------------------*/
1164 /* ethernet function driver setup/binding */
1167 ncm_bind(struct usb_configuration
*c
, struct usb_function
*f
)
1169 struct usb_composite_dev
*cdev
= c
->cdev
;
1170 struct f_ncm
*ncm
= func_to_ncm(f
);
1174 /* allocate instance-specific interface IDs */
1175 status
= usb_interface_id(c
, f
);
1178 ncm
->ctrl_id
= status
;
1179 ncm_iad_desc
.bFirstInterface
= status
;
1181 ncm_control_intf
.bInterfaceNumber
= status
;
1182 ncm_union_desc
.bMasterInterface0
= status
;
1184 status
= usb_interface_id(c
, f
);
1187 ncm
->data_id
= status
;
1189 ncm_data_nop_intf
.bInterfaceNumber
= status
;
1190 ncm_data_intf
.bInterfaceNumber
= status
;
1191 ncm_union_desc
.bSlaveInterface0
= status
;
1195 /* allocate instance-specific endpoints */
1196 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_in_desc
);
1199 ncm
->port
.in_ep
= ep
;
1200 ep
->driver_data
= cdev
; /* claim */
1202 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_out_desc
);
1205 ncm
->port
.out_ep
= ep
;
1206 ep
->driver_data
= cdev
; /* claim */
1208 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_notify_desc
);
1212 ep
->driver_data
= cdev
; /* claim */
1216 /* allocate notification request and buffer */
1217 ncm
->notify_req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
1218 if (!ncm
->notify_req
)
1220 ncm
->notify_req
->buf
= kmalloc(NCM_STATUS_BYTECOUNT
, GFP_KERNEL
);
1221 if (!ncm
->notify_req
->buf
)
1223 ncm
->notify_req
->context
= ncm
;
1224 ncm
->notify_req
->complete
= ncm_notify_complete
;
1226 /* copy descriptors, and track endpoint copies */
1227 f
->descriptors
= usb_copy_descriptors(ncm_fs_function
);
1228 if (!f
->descriptors
)
1231 ncm
->fs
.in
= usb_find_endpoint(ncm_fs_function
,
1232 f
->descriptors
, &fs_ncm_in_desc
);
1233 ncm
->fs
.out
= usb_find_endpoint(ncm_fs_function
,
1234 f
->descriptors
, &fs_ncm_out_desc
);
1235 ncm
->fs
.notify
= usb_find_endpoint(ncm_fs_function
,
1236 f
->descriptors
, &fs_ncm_notify_desc
);
1239 * support all relevant hardware speeds... we expect that when
1240 * hardware is dual speed, all bulk-capable endpoints work at
1243 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
1244 hs_ncm_in_desc
.bEndpointAddress
=
1245 fs_ncm_in_desc
.bEndpointAddress
;
1246 hs_ncm_out_desc
.bEndpointAddress
=
1247 fs_ncm_out_desc
.bEndpointAddress
;
1248 hs_ncm_notify_desc
.bEndpointAddress
=
1249 fs_ncm_notify_desc
.bEndpointAddress
;
1251 /* copy descriptors, and track endpoint copies */
1252 f
->hs_descriptors
= usb_copy_descriptors(ncm_hs_function
);
1253 if (!f
->hs_descriptors
)
1256 ncm
->hs
.in
= usb_find_endpoint(ncm_hs_function
,
1257 f
->hs_descriptors
, &hs_ncm_in_desc
);
1258 ncm
->hs
.out
= usb_find_endpoint(ncm_hs_function
,
1259 f
->hs_descriptors
, &hs_ncm_out_desc
);
1260 ncm
->hs
.notify
= usb_find_endpoint(ncm_hs_function
,
1261 f
->hs_descriptors
, &hs_ncm_notify_desc
);
1265 * NOTE: all that is done without knowing or caring about
1266 * the network link ... which is unavailable to this code
1267 * until we're activated via set_alt().
1270 ncm
->port
.open
= ncm_open
;
1271 ncm
->port
.close
= ncm_close
;
1273 DBG(cdev
, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1274 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
1275 ncm
->port
.in_ep
->name
, ncm
->port
.out_ep
->name
,
1281 usb_free_descriptors(f
->descriptors
);
1283 if (ncm
->notify_req
) {
1284 kfree(ncm
->notify_req
->buf
);
1285 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1288 /* we might as well release our claims on endpoints */
1290 ncm
->notify
->driver_data
= NULL
;
1292 ncm
->port
.out_ep
->driver_data
= NULL
;
1294 ncm
->port
.in_ep
->driver_data
= NULL
;
1296 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
1302 ncm_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1304 struct f_ncm
*ncm
= func_to_ncm(f
);
1306 DBG(c
->cdev
, "ncm unbind\n");
1308 if (gadget_is_dualspeed(c
->cdev
->gadget
))
1309 usb_free_descriptors(f
->hs_descriptors
);
1310 usb_free_descriptors(f
->descriptors
);
1312 kfree(ncm
->notify_req
->buf
);
1313 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1315 ncm_string_defs
[1].s
= NULL
;
1320 * ncm_bind_config - add CDC Network link to a configuration
1321 * @c: the configuration to support the network link
1322 * @ethaddr: a buffer in which the ethernet address of the host side
1323 * side of the link was recorded
1324 * Context: single threaded during gadget setup
1326 * Returns zero on success, else negative errno.
1328 * Caller must have called @gether_setup(). Caller is also responsible
1329 * for calling @gether_cleanup() before module unload.
1331 int __init
ncm_bind_config(struct usb_configuration
*c
, u8 ethaddr
[ETH_ALEN
])
1336 if (!can_support_ecm(c
->cdev
->gadget
) || !ethaddr
)
1339 /* maybe allocate device-global string IDs */
1340 if (ncm_string_defs
[0].id
== 0) {
1342 /* control interface label */
1343 status
= usb_string_id(c
->cdev
);
1346 ncm_string_defs
[STRING_CTRL_IDX
].id
= status
;
1347 ncm_control_intf
.iInterface
= status
;
1349 /* data interface label */
1350 status
= usb_string_id(c
->cdev
);
1353 ncm_string_defs
[STRING_DATA_IDX
].id
= status
;
1354 ncm_data_nop_intf
.iInterface
= status
;
1355 ncm_data_intf
.iInterface
= status
;
1358 status
= usb_string_id(c
->cdev
);
1361 ncm_string_defs
[STRING_MAC_IDX
].id
= status
;
1362 ecm_desc
.iMACAddress
= status
;
1365 status
= usb_string_id(c
->cdev
);
1368 ncm_string_defs
[STRING_IAD_IDX
].id
= status
;
1369 ncm_iad_desc
.iFunction
= status
;
1372 /* allocate and initialize one new instance */
1373 ncm
= kzalloc(sizeof *ncm
, GFP_KERNEL
);
1377 /* export host's Ethernet address in CDC format */
1378 snprintf(ncm
->ethaddr
, sizeof ncm
->ethaddr
,
1379 "%02X%02X%02X%02X%02X%02X",
1380 ethaddr
[0], ethaddr
[1], ethaddr
[2],
1381 ethaddr
[3], ethaddr
[4], ethaddr
[5]);
1382 ncm_string_defs
[1].s
= ncm
->ethaddr
;
1384 spin_lock_init(&ncm
->lock
);
1385 ncm_reset_values(ncm
);
1386 ncm
->port
.is_fixed
= true;
1388 ncm
->port
.func
.name
= "cdc_network";
1389 ncm
->port
.func
.strings
= ncm_strings
;
1390 /* descriptors are per-instance copies */
1391 ncm
->port
.func
.bind
= ncm_bind
;
1392 ncm
->port
.func
.unbind
= ncm_unbind
;
1393 ncm
->port
.func
.set_alt
= ncm_set_alt
;
1394 ncm
->port
.func
.get_alt
= ncm_get_alt
;
1395 ncm
->port
.func
.setup
= ncm_setup
;
1396 ncm
->port
.func
.disable
= ncm_disable
;
1398 ncm
->port
.wrap
= ncm_wrap_ntb
;
1399 ncm
->port
.unwrap
= ncm_unwrap_ntb
;
1401 status
= usb_add_function(c
, &ncm
->port
.func
);
1403 ncm_string_defs
[1].s
= NULL
;