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/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/etherdevice.h>
23 #include <linux/crc32.h>
25 #include <linux/usb/cdc.h>
28 #include "u_ether_configfs.h"
32 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
33 * NCM is intended to be used with high-speed network attachments.
35 * Note that NCM requires the use of "alternate settings" for its data
36 * interface. This means that the set_alt() method has real work to do,
37 * and also means that a get_alt() method is required.
40 /* to trigger crc/non-crc ndp signature */
42 #define NCM_NDP_HDR_CRC_MASK 0x01000000
43 #define NCM_NDP_HDR_CRC 0x01000000
44 #define NCM_NDP_HDR_NOCRC 0x00000000
46 enum ncm_notify_state
{
47 NCM_NOTIFY_NONE
, /* don't notify */
48 NCM_NOTIFY_CONNECT
, /* issue CONNECT next */
49 NCM_NOTIFY_SPEED
, /* issue SPEED_CHANGE next */
58 struct usb_ep
*notify
;
59 struct usb_request
*notify_req
;
63 const struct ndp_parser_opts
*parser_opts
;
68 * for notification, it is accessed from both
69 * callback and ethernet open/close
73 struct net_device
*netdev
;
75 /* For multi-frame NDP TX */
76 struct sk_buff
*skb_tx_data
;
77 struct sk_buff
*skb_tx_ndp
;
80 struct tasklet_struct tx_tasklet
;
81 struct hrtimer task_timer
;
86 static inline struct f_ncm
*func_to_ncm(struct usb_function
*f
)
88 return container_of(f
, struct f_ncm
, port
.func
);
91 /* peak (theoretical) bulk transfer rate in bits-per-second */
92 static inline unsigned ncm_bitrate(struct usb_gadget
*g
)
94 if (gadget_is_superspeed(g
) && g
->speed
== USB_SPEED_SUPER
)
95 return 13 * 1024 * 8 * 1000 * 8;
96 else 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 16384
111 #define NTB_OUT_SIZE 16384
113 /* Allocation for storing the NDP, 32 should suffice for a
114 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
115 * be transmitted in a single 16kB skb, though when sending full size
116 * packets this limit will be plenty.
117 * Smaller packets are not likely to be trying to maximize the
118 * throughput and will be mstly sending smaller infrequent frames.
120 #define TX_MAX_NUM_DPE 32
122 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
123 #define TX_TIMEOUT_NSECS 300000
125 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
126 USB_CDC_NCM_NTB32_SUPPORTED)
128 static struct usb_cdc_ncm_ntb_parameters ntb_parameters
= {
129 .wLength
= cpu_to_le16(sizeof(ntb_parameters
)),
130 .bmNtbFormatsSupported
= cpu_to_le16(FORMATS_SUPPORTED
),
131 .dwNtbInMaxSize
= cpu_to_le32(NTB_DEFAULT_IN_SIZE
),
132 .wNdpInDivisor
= cpu_to_le16(4),
133 .wNdpInPayloadRemainder
= cpu_to_le16(0),
134 .wNdpInAlignment
= cpu_to_le16(4),
136 .dwNtbOutMaxSize
= cpu_to_le32(NTB_OUT_SIZE
),
137 .wNdpOutDivisor
= cpu_to_le16(4),
138 .wNdpOutPayloadRemainder
= cpu_to_le16(0),
139 .wNdpOutAlignment
= cpu_to_le16(4),
143 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
144 * packet, to simplify cancellation; and a big transfer interval, to
145 * waste less bandwidth.
148 #define NCM_STATUS_INTERVAL_MS 32
149 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
151 static struct usb_interface_assoc_descriptor ncm_iad_desc
= {
152 .bLength
= sizeof ncm_iad_desc
,
153 .bDescriptorType
= USB_DT_INTERFACE_ASSOCIATION
,
155 /* .bFirstInterface = DYNAMIC, */
156 .bInterfaceCount
= 2, /* control + data */
157 .bFunctionClass
= USB_CLASS_COMM
,
158 .bFunctionSubClass
= USB_CDC_SUBCLASS_NCM
,
159 .bFunctionProtocol
= USB_CDC_PROTO_NONE
,
160 /* .iFunction = DYNAMIC */
163 /* interface descriptor: */
165 static struct usb_interface_descriptor ncm_control_intf
= {
166 .bLength
= sizeof ncm_control_intf
,
167 .bDescriptorType
= USB_DT_INTERFACE
,
169 /* .bInterfaceNumber = DYNAMIC */
171 .bInterfaceClass
= USB_CLASS_COMM
,
172 .bInterfaceSubClass
= USB_CDC_SUBCLASS_NCM
,
173 .bInterfaceProtocol
= USB_CDC_PROTO_NONE
,
174 /* .iInterface = DYNAMIC */
177 static struct usb_cdc_header_desc ncm_header_desc
= {
178 .bLength
= sizeof ncm_header_desc
,
179 .bDescriptorType
= USB_DT_CS_INTERFACE
,
180 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
182 .bcdCDC
= cpu_to_le16(0x0110),
185 static struct usb_cdc_union_desc ncm_union_desc
= {
186 .bLength
= sizeof(ncm_union_desc
),
187 .bDescriptorType
= USB_DT_CS_INTERFACE
,
188 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
189 /* .bMasterInterface0 = DYNAMIC */
190 /* .bSlaveInterface0 = DYNAMIC */
193 static struct usb_cdc_ether_desc ecm_desc
= {
194 .bLength
= sizeof ecm_desc
,
195 .bDescriptorType
= USB_DT_CS_INTERFACE
,
196 .bDescriptorSubType
= USB_CDC_ETHERNET_TYPE
,
198 /* this descriptor actually adds value, surprise! */
199 /* .iMACAddress = DYNAMIC */
200 .bmEthernetStatistics
= cpu_to_le32(0), /* no statistics */
201 .wMaxSegmentSize
= cpu_to_le16(ETH_FRAME_LEN
),
202 .wNumberMCFilters
= cpu_to_le16(0),
203 .bNumberPowerFilters
= 0,
206 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
208 static struct usb_cdc_ncm_desc ncm_desc
= {
209 .bLength
= sizeof ncm_desc
,
210 .bDescriptorType
= USB_DT_CS_INTERFACE
,
211 .bDescriptorSubType
= USB_CDC_NCM_TYPE
,
213 .bcdNcmVersion
= cpu_to_le16(0x0100),
214 /* can process SetEthernetPacketFilter */
215 .bmNetworkCapabilities
= NCAPS
,
218 /* the default data interface has no endpoints ... */
220 static struct usb_interface_descriptor ncm_data_nop_intf
= {
221 .bLength
= sizeof ncm_data_nop_intf
,
222 .bDescriptorType
= USB_DT_INTERFACE
,
224 .bInterfaceNumber
= 1,
225 .bAlternateSetting
= 0,
227 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
228 .bInterfaceSubClass
= 0,
229 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
230 /* .iInterface = DYNAMIC */
233 /* ... but the "real" data interface has two bulk endpoints */
235 static struct usb_interface_descriptor ncm_data_intf
= {
236 .bLength
= sizeof ncm_data_intf
,
237 .bDescriptorType
= USB_DT_INTERFACE
,
239 .bInterfaceNumber
= 1,
240 .bAlternateSetting
= 1,
242 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
243 .bInterfaceSubClass
= 0,
244 .bInterfaceProtocol
= USB_CDC_NCM_PROTO_NTB
,
245 /* .iInterface = DYNAMIC */
248 /* full speed support: */
250 static struct usb_endpoint_descriptor fs_ncm_notify_desc
= {
251 .bLength
= USB_DT_ENDPOINT_SIZE
,
252 .bDescriptorType
= USB_DT_ENDPOINT
,
254 .bEndpointAddress
= USB_DIR_IN
,
255 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
256 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
257 .bInterval
= NCM_STATUS_INTERVAL_MS
,
260 static struct usb_endpoint_descriptor fs_ncm_in_desc
= {
261 .bLength
= USB_DT_ENDPOINT_SIZE
,
262 .bDescriptorType
= USB_DT_ENDPOINT
,
264 .bEndpointAddress
= USB_DIR_IN
,
265 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
268 static struct usb_endpoint_descriptor fs_ncm_out_desc
= {
269 .bLength
= USB_DT_ENDPOINT_SIZE
,
270 .bDescriptorType
= USB_DT_ENDPOINT
,
272 .bEndpointAddress
= USB_DIR_OUT
,
273 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
276 static struct usb_descriptor_header
*ncm_fs_function
[] = {
277 (struct usb_descriptor_header
*) &ncm_iad_desc
,
278 /* CDC NCM control descriptors */
279 (struct usb_descriptor_header
*) &ncm_control_intf
,
280 (struct usb_descriptor_header
*) &ncm_header_desc
,
281 (struct usb_descriptor_header
*) &ncm_union_desc
,
282 (struct usb_descriptor_header
*) &ecm_desc
,
283 (struct usb_descriptor_header
*) &ncm_desc
,
284 (struct usb_descriptor_header
*) &fs_ncm_notify_desc
,
285 /* data interface, altsettings 0 and 1 */
286 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
287 (struct usb_descriptor_header
*) &ncm_data_intf
,
288 (struct usb_descriptor_header
*) &fs_ncm_in_desc
,
289 (struct usb_descriptor_header
*) &fs_ncm_out_desc
,
293 /* high speed support: */
295 static struct usb_endpoint_descriptor hs_ncm_notify_desc
= {
296 .bLength
= USB_DT_ENDPOINT_SIZE
,
297 .bDescriptorType
= USB_DT_ENDPOINT
,
299 .bEndpointAddress
= USB_DIR_IN
,
300 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
301 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
302 .bInterval
= USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS
),
304 static struct usb_endpoint_descriptor hs_ncm_in_desc
= {
305 .bLength
= USB_DT_ENDPOINT_SIZE
,
306 .bDescriptorType
= USB_DT_ENDPOINT
,
308 .bEndpointAddress
= USB_DIR_IN
,
309 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
310 .wMaxPacketSize
= cpu_to_le16(512),
313 static struct usb_endpoint_descriptor hs_ncm_out_desc
= {
314 .bLength
= USB_DT_ENDPOINT_SIZE
,
315 .bDescriptorType
= USB_DT_ENDPOINT
,
317 .bEndpointAddress
= USB_DIR_OUT
,
318 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
319 .wMaxPacketSize
= cpu_to_le16(512),
322 static struct usb_descriptor_header
*ncm_hs_function
[] = {
323 (struct usb_descriptor_header
*) &ncm_iad_desc
,
324 /* CDC NCM control descriptors */
325 (struct usb_descriptor_header
*) &ncm_control_intf
,
326 (struct usb_descriptor_header
*) &ncm_header_desc
,
327 (struct usb_descriptor_header
*) &ncm_union_desc
,
328 (struct usb_descriptor_header
*) &ecm_desc
,
329 (struct usb_descriptor_header
*) &ncm_desc
,
330 (struct usb_descriptor_header
*) &hs_ncm_notify_desc
,
331 /* data interface, altsettings 0 and 1 */
332 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
333 (struct usb_descriptor_header
*) &ncm_data_intf
,
334 (struct usb_descriptor_header
*) &hs_ncm_in_desc
,
335 (struct usb_descriptor_header
*) &hs_ncm_out_desc
,
340 /* super speed support: */
342 static struct usb_endpoint_descriptor ss_ncm_notify_desc
= {
343 .bLength
= USB_DT_ENDPOINT_SIZE
,
344 .bDescriptorType
= USB_DT_ENDPOINT
,
346 .bEndpointAddress
= USB_DIR_IN
,
347 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
348 .wMaxPacketSize
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
349 .bInterval
= USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS
)
352 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc
= {
353 .bLength
= sizeof(ss_ncm_notify_comp_desc
),
354 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
356 /* the following 3 values can be tweaked if necessary */
357 /* .bMaxBurst = 0, */
358 /* .bmAttributes = 0, */
359 .wBytesPerInterval
= cpu_to_le16(NCM_STATUS_BYTECOUNT
),
362 static struct usb_endpoint_descriptor ss_ncm_in_desc
= {
363 .bLength
= USB_DT_ENDPOINT_SIZE
,
364 .bDescriptorType
= USB_DT_ENDPOINT
,
366 .bEndpointAddress
= USB_DIR_IN
,
367 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
368 .wMaxPacketSize
= cpu_to_le16(1024),
371 static struct usb_endpoint_descriptor ss_ncm_out_desc
= {
372 .bLength
= USB_DT_ENDPOINT_SIZE
,
373 .bDescriptorType
= USB_DT_ENDPOINT
,
375 .bEndpointAddress
= USB_DIR_OUT
,
376 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
377 .wMaxPacketSize
= cpu_to_le16(1024),
380 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc
= {
381 .bLength
= sizeof(ss_ncm_bulk_comp_desc
),
382 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
384 /* the following 2 values can be tweaked if necessary */
385 /* .bMaxBurst = 0, */
386 /* .bmAttributes = 0, */
389 static struct usb_descriptor_header
*ncm_ss_function
[] = {
390 (struct usb_descriptor_header
*) &ncm_iad_desc
,
391 /* CDC NCM control descriptors */
392 (struct usb_descriptor_header
*) &ncm_control_intf
,
393 (struct usb_descriptor_header
*) &ncm_header_desc
,
394 (struct usb_descriptor_header
*) &ncm_union_desc
,
395 (struct usb_descriptor_header
*) &ecm_desc
,
396 (struct usb_descriptor_header
*) &ncm_desc
,
397 (struct usb_descriptor_header
*) &ss_ncm_notify_desc
,
398 (struct usb_descriptor_header
*) &ss_ncm_notify_comp_desc
,
399 /* data interface, altsettings 0 and 1 */
400 (struct usb_descriptor_header
*) &ncm_data_nop_intf
,
401 (struct usb_descriptor_header
*) &ncm_data_intf
,
402 (struct usb_descriptor_header
*) &ss_ncm_in_desc
,
403 (struct usb_descriptor_header
*) &ss_ncm_bulk_comp_desc
,
404 (struct usb_descriptor_header
*) &ss_ncm_out_desc
,
405 (struct usb_descriptor_header
*) &ss_ncm_bulk_comp_desc
,
409 /* string descriptors: */
411 #define STRING_CTRL_IDX 0
412 #define STRING_MAC_IDX 1
413 #define STRING_DATA_IDX 2
414 #define STRING_IAD_IDX 3
416 static struct usb_string ncm_string_defs
[] = {
417 [STRING_CTRL_IDX
].s
= "CDC Network Control Model (NCM)",
418 [STRING_MAC_IDX
].s
= "",
419 [STRING_DATA_IDX
].s
= "CDC Network Data",
420 [STRING_IAD_IDX
].s
= "CDC NCM",
421 { } /* end of list */
424 static struct usb_gadget_strings ncm_string_table
= {
425 .language
= 0x0409, /* en-us */
426 .strings
= ncm_string_defs
,
429 static struct usb_gadget_strings
*ncm_strings
[] = {
435 * Here are options for NCM Datagram Pointer table (NDP) parser.
436 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
437 * in NDP16 offsets and sizes fields are 1 16bit word wide,
438 * in NDP32 -- 2 16bit words wide. Also signatures are different.
439 * To make the parser code the same, put the differences in the structure,
440 * and switch pointers to the structures when the format is changed.
443 struct ndp_parser_opts
{
449 unsigned ndplen_align
;
450 /* sizes in u16 units */
451 unsigned dgram_item_len
; /* index or length */
452 unsigned block_length
;
456 unsigned next_ndp_index
;
459 #define INIT_NDP16_OPTS { \
460 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
461 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
462 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
463 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
464 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
466 .dgram_item_len = 1, \
471 .next_ndp_index = 1, \
475 #define INIT_NDP32_OPTS { \
476 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
477 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
478 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
479 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
480 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
482 .dgram_item_len = 2, \
487 .next_ndp_index = 2, \
490 static const struct ndp_parser_opts ndp16_opts
= INIT_NDP16_OPTS
;
491 static const struct ndp_parser_opts ndp32_opts
= INIT_NDP32_OPTS
;
493 static inline void put_ncm(__le16
**p
, unsigned size
, unsigned val
)
497 put_unaligned_le16((u16
)val
, *p
);
500 put_unaligned_le32((u32
)val
, *p
);
510 static inline unsigned get_ncm(__le16
**p
, unsigned size
)
516 tmp
= get_unaligned_le16(*p
);
519 tmp
= get_unaligned_le32(*p
);
529 /*-------------------------------------------------------------------------*/
531 static inline void ncm_reset_values(struct f_ncm
*ncm
)
533 ncm
->parser_opts
= &ndp16_opts
;
535 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
537 /* doesn't make sense for ncm, fixed size used */
538 ncm
->port
.header_len
= 0;
540 ncm
->port
.fixed_out_len
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
541 ncm
->port
.fixed_in_len
= NTB_DEFAULT_IN_SIZE
;
545 * Context: ncm->lock held
547 static void ncm_do_notify(struct f_ncm
*ncm
)
549 struct usb_request
*req
= ncm
->notify_req
;
550 struct usb_cdc_notification
*event
;
551 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
555 /* notification already in flight? */
560 switch (ncm
->notify_state
) {
561 case NCM_NOTIFY_NONE
:
564 case NCM_NOTIFY_CONNECT
:
565 event
->bNotificationType
= USB_CDC_NOTIFY_NETWORK_CONNECTION
;
567 event
->wValue
= cpu_to_le16(1);
569 event
->wValue
= cpu_to_le16(0);
571 req
->length
= sizeof *event
;
573 DBG(cdev
, "notify connect %s\n",
574 ncm
->is_open
? "true" : "false");
575 ncm
->notify_state
= NCM_NOTIFY_NONE
;
578 case NCM_NOTIFY_SPEED
:
579 event
->bNotificationType
= USB_CDC_NOTIFY_SPEED_CHANGE
;
580 event
->wValue
= cpu_to_le16(0);
581 event
->wLength
= cpu_to_le16(8);
582 req
->length
= NCM_STATUS_BYTECOUNT
;
584 /* SPEED_CHANGE data is up/down speeds in bits/sec */
585 data
= req
->buf
+ sizeof *event
;
586 data
[0] = cpu_to_le32(ncm_bitrate(cdev
->gadget
));
589 DBG(cdev
, "notify speed %d\n", ncm_bitrate(cdev
->gadget
));
590 ncm
->notify_state
= NCM_NOTIFY_CONNECT
;
593 event
->bmRequestType
= 0xA1;
594 event
->wIndex
= cpu_to_le16(ncm
->ctrl_id
);
596 ncm
->notify_req
= NULL
;
598 * In double buffering if there is a space in FIFO,
599 * completion callback can be called right after the call,
602 spin_unlock(&ncm
->lock
);
603 status
= usb_ep_queue(ncm
->notify
, req
, GFP_ATOMIC
);
604 spin_lock(&ncm
->lock
);
606 ncm
->notify_req
= req
;
607 DBG(cdev
, "notify --> %d\n", status
);
612 * Context: ncm->lock held
614 static void ncm_notify(struct f_ncm
*ncm
)
617 * NOTE on most versions of Linux, host side cdc-ethernet
618 * won't listen for notifications until its netdevice opens.
619 * The first notification then sits in the FIFO for a long
620 * time, and the second one is queued.
622 * If ncm_notify() is called before the second (CONNECT)
623 * notification is sent, then it will reset to send the SPEED
624 * notificaion again (and again, and again), but it's not a problem
626 ncm
->notify_state
= NCM_NOTIFY_SPEED
;
630 static void ncm_notify_complete(struct usb_ep
*ep
, struct usb_request
*req
)
632 struct f_ncm
*ncm
= req
->context
;
633 struct usb_composite_dev
*cdev
= ncm
->port
.func
.config
->cdev
;
634 struct usb_cdc_notification
*event
= req
->buf
;
636 spin_lock(&ncm
->lock
);
637 switch (req
->status
) {
639 VDBG(cdev
, "Notification %02x sent\n",
640 event
->bNotificationType
);
644 ncm
->notify_state
= NCM_NOTIFY_NONE
;
647 DBG(cdev
, "event %02x --> %d\n",
648 event
->bNotificationType
, req
->status
);
651 ncm
->notify_req
= req
;
653 spin_unlock(&ncm
->lock
);
656 static void ncm_ep0out_complete(struct usb_ep
*ep
, struct usb_request
*req
)
658 /* now for SET_NTB_INPUT_SIZE only */
660 struct usb_function
*f
= req
->context
;
661 struct f_ncm
*ncm
= func_to_ncm(f
);
662 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
665 if (req
->status
|| req
->actual
!= req
->length
) {
666 DBG(cdev
, "Bad control-OUT transfer\n");
670 in_size
= get_unaligned_le32(req
->buf
);
671 if (in_size
< USB_CDC_NCM_NTB_MIN_IN_SIZE
||
672 in_size
> le32_to_cpu(ntb_parameters
.dwNtbInMaxSize
)) {
673 DBG(cdev
, "Got wrong INPUT SIZE (%d) from host\n", in_size
);
677 ncm
->port
.fixed_in_len
= in_size
;
678 VDBG(cdev
, "Set NTB INPUT SIZE %d\n", in_size
);
686 static int ncm_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
688 struct f_ncm
*ncm
= func_to_ncm(f
);
689 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
690 struct usb_request
*req
= cdev
->req
;
691 int value
= -EOPNOTSUPP
;
692 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
693 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
694 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
697 * composite driver infrastructure handles everything except
698 * CDC class messages; interface activation uses set_alt().
700 switch ((ctrl
->bRequestType
<< 8) | ctrl
->bRequest
) {
701 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
702 | USB_CDC_SET_ETHERNET_PACKET_FILTER
:
704 * see 6.2.30: no data, wIndex = interface,
705 * wValue = packet filter bitmap
707 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
709 DBG(cdev
, "packet filter %02x\n", w_value
);
711 * REVISIT locking of cdc_filter. This assumes the UDC
712 * driver won't have a concurrent packet TX irq running on
713 * another CPU; or that if it does, this write is atomic...
715 ncm
->port
.cdc_filter
= w_value
;
720 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
721 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
722 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
723 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
724 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
725 * case USB_CDC_GET_ETHERNET_STATISTIC:
728 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
729 | USB_CDC_GET_NTB_PARAMETERS
:
731 if (w_length
== 0 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
733 value
= w_length
> sizeof ntb_parameters
?
734 sizeof ntb_parameters
: w_length
;
735 memcpy(req
->buf
, &ntb_parameters
, value
);
736 VDBG(cdev
, "Host asked NTB parameters\n");
739 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
740 | USB_CDC_GET_NTB_INPUT_SIZE
:
742 if (w_length
< 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
744 put_unaligned_le32(ncm
->port
.fixed_in_len
, req
->buf
);
746 VDBG(cdev
, "Host asked INPUT SIZE, sending %d\n",
747 ncm
->port
.fixed_in_len
);
750 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
751 | USB_CDC_SET_NTB_INPUT_SIZE
:
753 if (w_length
!= 4 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
755 req
->complete
= ncm_ep0out_complete
;
756 req
->length
= w_length
;
763 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
764 | USB_CDC_GET_NTB_FORMAT
:
768 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
770 format
= (ncm
->parser_opts
== &ndp16_opts
) ? 0x0000 : 0x0001;
771 put_unaligned_le16(format
, req
->buf
);
773 VDBG(cdev
, "Host asked NTB FORMAT, sending %d\n", format
);
777 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
778 | USB_CDC_SET_NTB_FORMAT
:
780 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
784 ncm
->parser_opts
= &ndp16_opts
;
785 DBG(cdev
, "NCM16 selected\n");
788 ncm
->parser_opts
= &ndp32_opts
;
789 DBG(cdev
, "NCM32 selected\n");
797 case ((USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
798 | USB_CDC_GET_CRC_MODE
:
802 if (w_length
< 2 || w_value
!= 0 || w_index
!= ncm
->ctrl_id
)
804 is_crc
= ncm
->is_crc
? 0x0001 : 0x0000;
805 put_unaligned_le16(is_crc
, req
->buf
);
807 VDBG(cdev
, "Host asked CRC MODE, sending %d\n", is_crc
);
811 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
) << 8)
812 | USB_CDC_SET_CRC_MODE
:
816 if (w_length
!= 0 || w_index
!= ncm
->ctrl_id
)
821 ndp_hdr_crc
= NCM_NDP_HDR_NOCRC
;
822 DBG(cdev
, "non-CRC mode selected\n");
826 ndp_hdr_crc
= NCM_NDP_HDR_CRC
;
827 DBG(cdev
, "CRC mode selected\n");
832 ncm
->ndp_sign
= ncm
->parser_opts
->ndp_sign
| ndp_hdr_crc
;
837 /* and disabled in ncm descriptor: */
838 /* case USB_CDC_GET_NET_ADDRESS: */
839 /* case USB_CDC_SET_NET_ADDRESS: */
840 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
841 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
845 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
846 ctrl
->bRequestType
, ctrl
->bRequest
,
847 w_value
, w_index
, w_length
);
850 /* respond with data transfer or status phase? */
852 DBG(cdev
, "ncm req%02x.%02x v%04x i%04x l%d\n",
853 ctrl
->bRequestType
, ctrl
->bRequest
,
854 w_value
, w_index
, w_length
);
857 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
859 ERROR(cdev
, "ncm req %02x.%02x response err %d\n",
860 ctrl
->bRequestType
, ctrl
->bRequest
,
864 /* device either stalls (value < 0) or reports success */
869 static int ncm_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
871 struct f_ncm
*ncm
= func_to_ncm(f
);
872 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
874 /* Control interface has only altsetting 0 */
875 if (intf
== ncm
->ctrl_id
) {
879 DBG(cdev
, "reset ncm control %d\n", intf
);
880 usb_ep_disable(ncm
->notify
);
882 if (!(ncm
->notify
->desc
)) {
883 DBG(cdev
, "init ncm ctrl %d\n", intf
);
884 if (config_ep_by_speed(cdev
->gadget
, f
, ncm
->notify
))
887 usb_ep_enable(ncm
->notify
);
889 /* Data interface has two altsettings, 0 and 1 */
890 } else if (intf
== ncm
->data_id
) {
894 if (ncm
->port
.in_ep
->enabled
) {
895 DBG(cdev
, "reset ncm\n");
896 ncm
->timer_stopping
= true;
898 gether_disconnect(&ncm
->port
);
899 ncm_reset_values(ncm
);
903 * CDC Network only sends data in non-default altsettings.
904 * Changing altsettings resets filters, statistics, etc.
907 struct net_device
*net
;
909 if (!ncm
->port
.in_ep
->desc
||
910 !ncm
->port
.out_ep
->desc
) {
911 DBG(cdev
, "init ncm\n");
912 if (config_ep_by_speed(cdev
->gadget
, f
,
914 config_ep_by_speed(cdev
->gadget
, f
,
916 ncm
->port
.in_ep
->desc
= NULL
;
917 ncm
->port
.out_ep
->desc
= NULL
;
923 /* Enable zlps by default for NCM conformance;
924 * override for musb_hdrc (avoids txdma ovhead)
926 ncm
->port
.is_zlp_ok
=
927 gadget_is_zlp_supported(cdev
->gadget
);
928 ncm
->port
.cdc_filter
= DEFAULT_FILTER
;
929 DBG(cdev
, "activate ncm\n");
930 net
= gether_connect(&ncm
->port
);
934 ncm
->timer_stopping
= false;
937 spin_lock(&ncm
->lock
);
939 spin_unlock(&ncm
->lock
);
949 * Because the data interface supports multiple altsettings,
950 * this NCM function *MUST* implement a get_alt() method.
952 static int ncm_get_alt(struct usb_function
*f
, unsigned intf
)
954 struct f_ncm
*ncm
= func_to_ncm(f
);
956 if (intf
== ncm
->ctrl_id
)
958 return ncm
->port
.in_ep
->enabled
? 1 : 0;
961 static struct sk_buff
*package_for_tx(struct f_ncm
*ncm
)
964 struct sk_buff
*skb2
= NULL
;
969 const struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
970 const int ndp_align
= le16_to_cpu(ntb_parameters
.wNdpInAlignment
);
971 const int dgram_idx_len
= 2 * 2 * opts
->dgram_item_len
;
974 hrtimer_try_to_cancel(&ncm
->task_timer
);
976 ndp_pad
= ALIGN(ncm
->skb_tx_data
->len
, ndp_align
) -
977 ncm
->skb_tx_data
->len
;
978 ndp_index
= ncm
->skb_tx_data
->len
+ ndp_pad
;
979 new_len
= ndp_index
+ dgram_idx_len
+ ncm
->skb_tx_ndp
->len
;
981 /* Set the final BlockLength and wNdpIndex */
982 ntb_iter
= (void *) ncm
->skb_tx_data
->data
;
983 /* Increment pointer to BlockLength */
984 ntb_iter
+= 2 + 1 + 1;
985 put_ncm(&ntb_iter
, opts
->block_length
, new_len
);
986 put_ncm(&ntb_iter
, opts
->ndp_index
, ndp_index
);
988 /* Set the final NDP wLength */
989 new_len
= opts
->ndp_size
+
990 (ncm
->ndp_dgram_count
* dgram_idx_len
);
991 ncm
->ndp_dgram_count
= 0;
992 /* Increment from start to wLength */
993 ntb_iter
= (void *) ncm
->skb_tx_ndp
->data
;
995 put_unaligned_le16(new_len
, ntb_iter
);
998 swap(skb2
, ncm
->skb_tx_data
);
999 if (ncm
->skb_tx_data
) {
1000 dev_consume_skb_any(ncm
->skb_tx_data
);
1001 ncm
->skb_tx_data
= NULL
;
1004 /* Insert NDP alignment. */
1005 skb_put_zero(skb2
, ndp_pad
);
1007 /* Copy NTB across. */
1008 skb_put_data(skb2
, ncm
->skb_tx_ndp
->data
, ncm
->skb_tx_ndp
->len
);
1009 dev_consume_skb_any(ncm
->skb_tx_ndp
);
1010 ncm
->skb_tx_ndp
= NULL
;
1012 /* Insert zero'd datagram. */
1013 skb_put_zero(skb2
, dgram_idx_len
);
1018 static struct sk_buff
*ncm_wrap_ntb(struct gether
*port
,
1019 struct sk_buff
*skb
)
1021 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
1022 struct sk_buff
*skb2
= NULL
;
1028 unsigned max_size
= ncm
->port
.fixed_in_len
;
1029 const struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
1030 const int ndp_align
= le16_to_cpu(ntb_parameters
.wNdpInAlignment
);
1031 const int div
= le16_to_cpu(ntb_parameters
.wNdpInDivisor
);
1032 const int rem
= le16_to_cpu(ntb_parameters
.wNdpInPayloadRemainder
);
1033 const int dgram_idx_len
= 2 * 2 * opts
->dgram_item_len
;
1035 if (!skb
&& !ncm
->skb_tx_data
)
1039 /* Add the CRC if required up front */
1047 crc_pos
= skb_put(skb
, sizeof(uint32_t));
1048 put_unaligned_le32(crc
, crc_pos
);
1051 /* If the new skb is too big for the current NCM NTB then
1052 * set the current stored skb to be sent now and clear it
1053 * ready for new data.
1054 * NOTE: Assume maximum align for speed of calculation.
1056 if (ncm
->skb_tx_data
1057 && (ncm
->ndp_dgram_count
>= TX_MAX_NUM_DPE
1058 || (ncm
->skb_tx_data
->len
+
1059 div
+ rem
+ skb
->len
+
1060 ncm
->skb_tx_ndp
->len
+ ndp_align
+ (2 * dgram_idx_len
))
1062 skb2
= package_for_tx(ncm
);
1067 if (!ncm
->skb_tx_data
) {
1068 ncb_len
= opts
->nth_size
;
1069 dgram_pad
= ALIGN(ncb_len
, div
) + rem
- ncb_len
;
1070 ncb_len
+= dgram_pad
;
1072 /* Create a new skb for the NTH and datagrams. */
1073 ncm
->skb_tx_data
= alloc_skb(max_size
, GFP_ATOMIC
);
1074 if (!ncm
->skb_tx_data
)
1077 ncm
->skb_tx_data
->dev
= ncm
->netdev
;
1078 ntb_data
= skb_put_zero(ncm
->skb_tx_data
, ncb_len
);
1080 put_unaligned_le32(opts
->nth_sign
, ntb_data
);
1083 put_unaligned_le16(opts
->nth_size
, ntb_data
++);
1085 /* Allocate an skb for storing the NDP,
1086 * TX_MAX_NUM_DPE should easily suffice for a
1089 ncm
->skb_tx_ndp
= alloc_skb((int)(opts
->ndp_size
1093 if (!ncm
->skb_tx_ndp
)
1096 ncm
->skb_tx_ndp
->dev
= ncm
->netdev
;
1097 ntb_ndp
= skb_put(ncm
->skb_tx_ndp
, opts
->ndp_size
);
1098 memset(ntb_ndp
, 0, ncb_len
);
1100 put_unaligned_le32(ncm
->ndp_sign
, ntb_ndp
);
1103 /* There is always a zeroed entry */
1104 ncm
->ndp_dgram_count
= 1;
1106 /* Note: we skip opts->next_ndp_index */
1109 /* Delay the timer. */
1110 hrtimer_start(&ncm
->task_timer
, TX_TIMEOUT_NSECS
,
1113 /* Add the datagram position entries */
1114 ntb_ndp
= skb_put_zero(ncm
->skb_tx_ndp
, dgram_idx_len
);
1116 ncb_len
= ncm
->skb_tx_data
->len
;
1117 dgram_pad
= ALIGN(ncb_len
, div
) + rem
- ncb_len
;
1118 ncb_len
+= dgram_pad
;
1120 /* (d)wDatagramIndex */
1121 put_ncm(&ntb_ndp
, opts
->dgram_item_len
, ncb_len
);
1122 /* (d)wDatagramLength */
1123 put_ncm(&ntb_ndp
, opts
->dgram_item_len
, skb
->len
);
1124 ncm
->ndp_dgram_count
++;
1126 /* Add the new data to the skb */
1127 skb_put_zero(ncm
->skb_tx_data
, dgram_pad
);
1128 skb_put_data(ncm
->skb_tx_data
, skb
->data
, skb
->len
);
1129 dev_consume_skb_any(skb
);
1132 } else if (ncm
->skb_tx_data
&& ncm
->timer_force_tx
) {
1133 /* If the tx was requested because of a timeout then send */
1134 skb2
= package_for_tx(ncm
);
1142 ncm
->netdev
->stats
.tx_dropped
++;
1145 dev_kfree_skb_any(skb
);
1146 if (ncm
->skb_tx_data
)
1147 dev_kfree_skb_any(ncm
->skb_tx_data
);
1148 if (ncm
->skb_tx_ndp
)
1149 dev_kfree_skb_any(ncm
->skb_tx_ndp
);
1155 * This transmits the NTB if there are frames waiting.
1157 static void ncm_tx_tasklet(unsigned long data
)
1159 struct f_ncm
*ncm
= (void *)data
;
1161 if (ncm
->timer_stopping
)
1164 /* Only send if data is available. */
1165 if (ncm
->skb_tx_data
) {
1166 ncm
->timer_force_tx
= true;
1168 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1169 * XXX is not sane. The gadget layer should be redesigned so
1170 * XXX that the dev->wrap() invocations to build SKBs is transparent
1171 * XXX and performed in some way outside of the ndo_start_xmit
1174 ncm
->netdev
->netdev_ops
->ndo_start_xmit(NULL
, ncm
->netdev
);
1176 ncm
->timer_force_tx
= false;
1181 * The transmit should only be run if no skb data has been sent
1182 * for a certain duration.
1184 static enum hrtimer_restart
ncm_tx_timeout(struct hrtimer
*data
)
1186 struct f_ncm
*ncm
= container_of(data
, struct f_ncm
, task_timer
);
1187 tasklet_schedule(&ncm
->tx_tasklet
);
1188 return HRTIMER_NORESTART
;
1191 static int ncm_unwrap_ntb(struct gether
*port
,
1192 struct sk_buff
*skb
,
1193 struct sk_buff_head
*list
)
1195 struct f_ncm
*ncm
= func_to_ncm(&port
->func
);
1196 __le16
*tmp
= (void *) skb
->data
;
1197 unsigned index
, index2
;
1199 unsigned dg_len
, dg_len2
;
1201 struct sk_buff
*skb2
;
1203 unsigned max_size
= le32_to_cpu(ntb_parameters
.dwNtbOutMaxSize
);
1204 const struct ndp_parser_opts
*opts
= ncm
->parser_opts
;
1205 unsigned crc_len
= ncm
->is_crc
? sizeof(uint32_t) : 0;
1209 if (get_unaligned_le32(tmp
) != opts
->nth_sign
) {
1210 INFO(port
->func
.config
->cdev
, "Wrong NTH SIGN, skblen %d\n",
1212 print_hex_dump(KERN_INFO
, "HEAD:", DUMP_PREFIX_ADDRESS
, 32, 1,
1213 skb
->data
, 32, false);
1219 if (get_unaligned_le16(tmp
++) != opts
->nth_size
) {
1220 INFO(port
->func
.config
->cdev
, "Wrong NTB headersize\n");
1223 tmp
++; /* skip wSequence */
1225 /* (d)wBlockLength */
1226 if (get_ncm(&tmp
, opts
->block_length
) > max_size
) {
1227 INFO(port
->func
.config
->cdev
, "OUT size exceeded\n");
1231 ndp_index
= get_ncm(&tmp
, opts
->ndp_index
);
1233 /* Run through all the NDP's in the NTB */
1236 if (((ndp_index
% 4) != 0) &&
1237 (ndp_index
< opts
->nth_size
)) {
1238 INFO(port
->func
.config
->cdev
, "Bad index: %#X\n",
1243 /* walk through NDP */
1244 tmp
= (void *)(skb
->data
+ ndp_index
);
1245 if (get_unaligned_le32(tmp
) != ncm
->ndp_sign
) {
1246 INFO(port
->func
.config
->cdev
, "Wrong NDP SIGN\n");
1251 ndp_len
= get_unaligned_le16(tmp
++);
1255 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1256 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1257 * Each entry is a dgram index and a dgram length.
1259 if ((ndp_len
< opts
->ndp_size
1260 + 2 * 2 * (opts
->dgram_item_len
* 2))
1261 || (ndp_len
% opts
->ndplen_align
!= 0)) {
1262 INFO(port
->func
.config
->cdev
, "Bad NDP length: %#X\n",
1266 tmp
+= opts
->reserved1
;
1267 /* Check for another NDP (d)wNextNdpIndex */
1268 ndp_index
= get_ncm(&tmp
, opts
->next_ndp_index
);
1269 tmp
+= opts
->reserved2
;
1271 ndp_len
-= opts
->ndp_size
;
1272 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1273 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1279 if (dg_len
< 14 + crc_len
) { /* ethernet hdr + crc */
1280 INFO(port
->func
.config
->cdev
,
1281 "Bad dgram length: %#X\n", dg_len
);
1287 crc
= get_unaligned_le32(skb
->data
+
1290 crc2
= ~crc32_le(~0,
1294 INFO(port
->func
.config
->cdev
,
1300 index2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1301 dg_len2
= get_ncm(&tmp
, opts
->dgram_item_len
);
1304 * Copy the data into a new skb.
1305 * This ensures the truesize is correct
1307 skb2
= netdev_alloc_skb_ip_align(ncm
->netdev
,
1311 skb_put_data(skb2
, skb
->data
+ index
,
1314 skb_queue_tail(list
, skb2
);
1316 ndp_len
-= 2 * (opts
->dgram_item_len
* 2);
1320 if (index2
== 0 || dg_len2
== 0)
1322 } while (ndp_len
> 2 * (opts
->dgram_item_len
* 2));
1323 } while (ndp_index
);
1325 dev_consume_skb_any(skb
);
1327 VDBG(port
->func
.config
->cdev
,
1328 "Parsed NTB with %d frames\n", dgram_counter
);
1331 skb_queue_purge(list
);
1332 dev_kfree_skb_any(skb
);
1336 static void ncm_disable(struct usb_function
*f
)
1338 struct f_ncm
*ncm
= func_to_ncm(f
);
1339 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
1341 DBG(cdev
, "ncm deactivated\n");
1343 if (ncm
->port
.in_ep
->enabled
) {
1344 ncm
->timer_stopping
= true;
1346 gether_disconnect(&ncm
->port
);
1349 if (ncm
->notify
->enabled
) {
1350 usb_ep_disable(ncm
->notify
);
1351 ncm
->notify
->desc
= NULL
;
1355 /*-------------------------------------------------------------------------*/
1358 * Callbacks let us notify the host about connect/disconnect when the
1359 * net device is opened or closed.
1361 * For testing, note that link states on this side include both opened
1362 * and closed variants of:
1364 * - disconnected/unconfigured
1365 * - configured but inactive (data alt 0)
1366 * - configured and active (data alt 1)
1368 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1369 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1370 * imply the host is actually polling the notification endpoint, and
1371 * likewise that "active" doesn't imply it's actually using the data
1372 * endpoints for traffic.
1375 static void ncm_open(struct gether
*geth
)
1377 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1379 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1381 spin_lock(&ncm
->lock
);
1382 ncm
->is_open
= true;
1384 spin_unlock(&ncm
->lock
);
1387 static void ncm_close(struct gether
*geth
)
1389 struct f_ncm
*ncm
= func_to_ncm(&geth
->func
);
1391 DBG(ncm
->port
.func
.config
->cdev
, "%s\n", __func__
);
1393 spin_lock(&ncm
->lock
);
1394 ncm
->is_open
= false;
1396 spin_unlock(&ncm
->lock
);
1399 /*-------------------------------------------------------------------------*/
1401 /* ethernet function driver setup/binding */
1403 static int ncm_bind(struct usb_configuration
*c
, struct usb_function
*f
)
1405 struct usb_composite_dev
*cdev
= c
->cdev
;
1406 struct f_ncm
*ncm
= func_to_ncm(f
);
1407 struct usb_string
*us
;
1410 struct f_ncm_opts
*ncm_opts
;
1412 if (!can_support_ecm(cdev
->gadget
))
1415 ncm_opts
= container_of(f
->fi
, struct f_ncm_opts
, func_inst
);
1417 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1418 * configurations are bound in sequence with list_for_each_entry,
1419 * in each configuration its functions are bound in sequence
1420 * with list_for_each_entry, so we assume no race condition
1421 * with regard to ncm_opts->bound access
1423 if (!ncm_opts
->bound
) {
1424 mutex_lock(&ncm_opts
->lock
);
1425 gether_set_gadget(ncm_opts
->net
, cdev
->gadget
);
1426 status
= gether_register_netdev(ncm_opts
->net
);
1427 mutex_unlock(&ncm_opts
->lock
);
1430 ncm_opts
->bound
= true;
1432 us
= usb_gstrings_attach(cdev
, ncm_strings
,
1433 ARRAY_SIZE(ncm_string_defs
));
1436 ncm_control_intf
.iInterface
= us
[STRING_CTRL_IDX
].id
;
1437 ncm_data_nop_intf
.iInterface
= us
[STRING_DATA_IDX
].id
;
1438 ncm_data_intf
.iInterface
= us
[STRING_DATA_IDX
].id
;
1439 ecm_desc
.iMACAddress
= us
[STRING_MAC_IDX
].id
;
1440 ncm_iad_desc
.iFunction
= us
[STRING_IAD_IDX
].id
;
1442 /* allocate instance-specific interface IDs */
1443 status
= usb_interface_id(c
, f
);
1446 ncm
->ctrl_id
= status
;
1447 ncm_iad_desc
.bFirstInterface
= status
;
1449 ncm_control_intf
.bInterfaceNumber
= status
;
1450 ncm_union_desc
.bMasterInterface0
= status
;
1452 status
= usb_interface_id(c
, f
);
1455 ncm
->data_id
= status
;
1457 ncm_data_nop_intf
.bInterfaceNumber
= status
;
1458 ncm_data_intf
.bInterfaceNumber
= status
;
1459 ncm_union_desc
.bSlaveInterface0
= status
;
1463 /* allocate instance-specific endpoints */
1464 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_in_desc
);
1467 ncm
->port
.in_ep
= ep
;
1469 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_out_desc
);
1472 ncm
->port
.out_ep
= ep
;
1474 ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ncm_notify_desc
);
1481 /* allocate notification request and buffer */
1482 ncm
->notify_req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
1483 if (!ncm
->notify_req
)
1485 ncm
->notify_req
->buf
= kmalloc(NCM_STATUS_BYTECOUNT
, GFP_KERNEL
);
1486 if (!ncm
->notify_req
->buf
)
1488 ncm
->notify_req
->context
= ncm
;
1489 ncm
->notify_req
->complete
= ncm_notify_complete
;
1492 * support all relevant hardware speeds... we expect that when
1493 * hardware is dual speed, all bulk-capable endpoints work at
1496 hs_ncm_in_desc
.bEndpointAddress
= fs_ncm_in_desc
.bEndpointAddress
;
1497 hs_ncm_out_desc
.bEndpointAddress
= fs_ncm_out_desc
.bEndpointAddress
;
1498 hs_ncm_notify_desc
.bEndpointAddress
=
1499 fs_ncm_notify_desc
.bEndpointAddress
;
1501 ss_ncm_in_desc
.bEndpointAddress
= fs_ncm_in_desc
.bEndpointAddress
;
1502 ss_ncm_out_desc
.bEndpointAddress
= fs_ncm_out_desc
.bEndpointAddress
;
1503 ss_ncm_notify_desc
.bEndpointAddress
=
1504 fs_ncm_notify_desc
.bEndpointAddress
;
1506 status
= usb_assign_descriptors(f
, ncm_fs_function
, ncm_hs_function
,
1507 ncm_ss_function
, NULL
);
1512 * NOTE: all that is done without knowing or caring about
1513 * the network link ... which is unavailable to this code
1514 * until we're activated via set_alt().
1517 ncm
->port
.open
= ncm_open
;
1518 ncm
->port
.close
= ncm_close
;
1520 tasklet_init(&ncm
->tx_tasklet
, ncm_tx_tasklet
, (unsigned long) ncm
);
1521 hrtimer_init(&ncm
->task_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1522 ncm
->task_timer
.function
= ncm_tx_timeout
;
1524 DBG(cdev
, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1525 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
1526 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
1527 ncm
->port
.in_ep
->name
, ncm
->port
.out_ep
->name
,
1532 if (ncm
->notify_req
) {
1533 kfree(ncm
->notify_req
->buf
);
1534 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1537 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
1542 static inline struct f_ncm_opts
*to_f_ncm_opts(struct config_item
*item
)
1544 return container_of(to_config_group(item
), struct f_ncm_opts
,
1548 /* f_ncm_item_ops */
1549 USB_ETHERNET_CONFIGFS_ITEM(ncm
);
1551 /* f_ncm_opts_dev_addr */
1552 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm
);
1554 /* f_ncm_opts_host_addr */
1555 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm
);
1557 /* f_ncm_opts_qmult */
1558 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm
);
1560 /* f_ncm_opts_ifname */
1561 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm
);
1563 static struct configfs_attribute
*ncm_attrs
[] = {
1564 &ncm_opts_attr_dev_addr
,
1565 &ncm_opts_attr_host_addr
,
1566 &ncm_opts_attr_qmult
,
1567 &ncm_opts_attr_ifname
,
1571 static struct config_item_type ncm_func_type
= {
1572 .ct_item_ops
= &ncm_item_ops
,
1573 .ct_attrs
= ncm_attrs
,
1574 .ct_owner
= THIS_MODULE
,
1577 static void ncm_free_inst(struct usb_function_instance
*f
)
1579 struct f_ncm_opts
*opts
;
1581 opts
= container_of(f
, struct f_ncm_opts
, func_inst
);
1583 gether_cleanup(netdev_priv(opts
->net
));
1585 free_netdev(opts
->net
);
1589 static struct usb_function_instance
*ncm_alloc_inst(void)
1591 struct f_ncm_opts
*opts
;
1593 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1595 return ERR_PTR(-ENOMEM
);
1596 mutex_init(&opts
->lock
);
1597 opts
->func_inst
.free_func_inst
= ncm_free_inst
;
1598 opts
->net
= gether_setup_default();
1599 if (IS_ERR(opts
->net
)) {
1600 struct net_device
*net
= opts
->net
;
1602 return ERR_CAST(net
);
1605 config_group_init_type_name(&opts
->func_inst
.group
, "", &ncm_func_type
);
1607 return &opts
->func_inst
;
1610 static void ncm_free(struct usb_function
*f
)
1613 struct f_ncm_opts
*opts
;
1615 ncm
= func_to_ncm(f
);
1616 opts
= container_of(f
->fi
, struct f_ncm_opts
, func_inst
);
1618 mutex_lock(&opts
->lock
);
1620 mutex_unlock(&opts
->lock
);
1623 static void ncm_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
1625 struct f_ncm
*ncm
= func_to_ncm(f
);
1627 DBG(c
->cdev
, "ncm unbind\n");
1629 hrtimer_cancel(&ncm
->task_timer
);
1630 tasklet_kill(&ncm
->tx_tasklet
);
1632 ncm_string_defs
[0].id
= 0;
1633 usb_free_all_descriptors(f
);
1635 kfree(ncm
->notify_req
->buf
);
1636 usb_ep_free_request(ncm
->notify
, ncm
->notify_req
);
1639 static struct usb_function
*ncm_alloc(struct usb_function_instance
*fi
)
1642 struct f_ncm_opts
*opts
;
1645 /* allocate and initialize one new instance */
1646 ncm
= kzalloc(sizeof(*ncm
), GFP_KERNEL
);
1648 return ERR_PTR(-ENOMEM
);
1650 opts
= container_of(fi
, struct f_ncm_opts
, func_inst
);
1651 mutex_lock(&opts
->lock
);
1654 /* export host's Ethernet address in CDC format */
1655 status
= gether_get_host_addr_cdc(opts
->net
, ncm
->ethaddr
,
1656 sizeof(ncm
->ethaddr
));
1657 if (status
< 12) { /* strlen("01234567890a") */
1659 mutex_unlock(&opts
->lock
);
1660 return ERR_PTR(-EINVAL
);
1662 ncm_string_defs
[STRING_MAC_IDX
].s
= ncm
->ethaddr
;
1664 spin_lock_init(&ncm
->lock
);
1665 ncm_reset_values(ncm
);
1666 ncm
->port
.ioport
= netdev_priv(opts
->net
);
1667 mutex_unlock(&opts
->lock
);
1668 ncm
->port
.is_fixed
= true;
1669 ncm
->port
.supports_multi_frame
= true;
1671 ncm
->port
.func
.name
= "cdc_network";
1672 /* descriptors are per-instance copies */
1673 ncm
->port
.func
.bind
= ncm_bind
;
1674 ncm
->port
.func
.unbind
= ncm_unbind
;
1675 ncm
->port
.func
.set_alt
= ncm_set_alt
;
1676 ncm
->port
.func
.get_alt
= ncm_get_alt
;
1677 ncm
->port
.func
.setup
= ncm_setup
;
1678 ncm
->port
.func
.disable
= ncm_disable
;
1679 ncm
->port
.func
.free_func
= ncm_free
;
1681 ncm
->port
.wrap
= ncm_wrap_ntb
;
1682 ncm
->port
.unwrap
= ncm_unwrap_ntb
;
1684 return &ncm
->port
.func
;
1687 DECLARE_USB_FUNCTION_INIT(ncm
, ncm_alloc_inst
, ncm_alloc
);
1688 MODULE_LICENSE("GPL");
1689 MODULE_AUTHOR("Yauheni Kaliuta");