1 // SPDX-License-Identifier: GPL-2.0+
3 * f_ncm.c -- USB CDC Network (NCM) link function driver
5 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
8 * The driver borrows from f_ecm.c which is:
10 * Copyright (C) 2003-2005,2008 David Brownell
11 * Copyright (C) 2008 Nokia Corporation
14 #include <linux/kernel.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/etherdevice.h>
19 #include <linux/crc32.h>
21 #include <linux/usb/cdc.h>
24 #include "u_ether_configfs.h"
28 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
29 * NCM is intended to be used with high-speed network attachments.
31 * Note that NCM requires the use of "alternate settings" for its data
32 * interface. This means that the set_alt() method has real work to do,
33 * and also means that a get_alt() method is required.
36 /* to trigger crc/non-crc ndp signature */
38 #define NCM_NDP_HDR_CRC_MASK 0x01000000
39 #define NCM_NDP_HDR_CRC 0x01000000
40 #define NCM_NDP_HDR_NOCRC 0x00000000
42 enum ncm_notify_state {
43 NCM_NOTIFY_NONE, /* don't notify */
44 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
45 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
54 struct usb_ep *notify;
55 struct usb_request *notify_req;
57 atomic_t notify_count;
60 const struct ndp_parser_opts *parser_opts;
65 * for notification, it is accessed from both
66 * callback and ethernet open/close
70 struct net_device *netdev;
72 /* For multi-frame NDP TX */
73 struct sk_buff *skb_tx_data;
74 struct sk_buff *skb_tx_ndp;
77 struct hrtimer task_timer;
81 static inline struct f_ncm *func_to_ncm(struct usb_function *f)
83 return container_of(f, struct f_ncm, port.func);
86 /* peak (theoretical) bulk transfer rate in bits-per-second */
87 static inline unsigned ncm_bitrate(struct usb_gadget *g)
89 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER_PLUS)
91 else if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
93 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
94 return 13 * 512 * 8 * 1000 * 8;
96 return 19 * 64 * 1 * 1000 * 8;
99 /*-------------------------------------------------------------------------*/
102 * We cannot group frames so use just the minimal size which ok to put
103 * one max-size ethernet frame.
104 * If the host can group frames, allow it to do that, 16K is selected,
105 * because it's used by default by the current linux host driver
107 #define NTB_DEFAULT_IN_SIZE 16384
108 #define NTB_OUT_SIZE 16384
110 /* Allocation for storing the NDP, 32 should suffice for a
111 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
112 * be transmitted in a single 16kB skb, though when sending full size
113 * packets this limit will be plenty.
114 * Smaller packets are not likely to be trying to maximize the
115 * throughput and will be mstly sending smaller infrequent frames.
117 #define TX_MAX_NUM_DPE 32
119 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
120 #define TX_TIMEOUT_NSECS 300000
122 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
123 USB_CDC_NCM_NTB32_SUPPORTED)
125 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
126 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
127 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
128 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
129 .wNdpInDivisor = cpu_to_le16(4),
130 .wNdpInPayloadRemainder = cpu_to_le16(0),
131 .wNdpInAlignment = cpu_to_le16(4),
133 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
134 .wNdpOutDivisor = cpu_to_le16(4),
135 .wNdpOutPayloadRemainder = cpu_to_le16(0),
136 .wNdpOutAlignment = cpu_to_le16(4),
140 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
141 * packet, to simplify cancellation; and a big transfer interval, to
142 * waste less bandwidth.
145 #define NCM_STATUS_INTERVAL_MS 32
146 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
148 static struct usb_interface_assoc_descriptor ncm_iad_desc = {
149 .bLength = sizeof ncm_iad_desc,
150 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
152 /* .bFirstInterface = DYNAMIC, */
153 .bInterfaceCount = 2, /* control + data */
154 .bFunctionClass = USB_CLASS_COMM,
155 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
156 .bFunctionProtocol = USB_CDC_PROTO_NONE,
157 /* .iFunction = DYNAMIC */
160 /* interface descriptor: */
162 static struct usb_interface_descriptor ncm_control_intf = {
163 .bLength = sizeof ncm_control_intf,
164 .bDescriptorType = USB_DT_INTERFACE,
166 /* .bInterfaceNumber = DYNAMIC */
168 .bInterfaceClass = USB_CLASS_COMM,
169 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
170 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
171 /* .iInterface = DYNAMIC */
174 static struct usb_cdc_header_desc ncm_header_desc = {
175 .bLength = sizeof ncm_header_desc,
176 .bDescriptorType = USB_DT_CS_INTERFACE,
177 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
179 .bcdCDC = cpu_to_le16(0x0110),
182 static struct usb_cdc_union_desc ncm_union_desc = {
183 .bLength = sizeof(ncm_union_desc),
184 .bDescriptorType = USB_DT_CS_INTERFACE,
185 .bDescriptorSubType = USB_CDC_UNION_TYPE,
186 /* .bMasterInterface0 = DYNAMIC */
187 /* .bSlaveInterface0 = DYNAMIC */
190 static struct usb_cdc_ether_desc ecm_desc = {
191 .bLength = sizeof ecm_desc,
192 .bDescriptorType = USB_DT_CS_INTERFACE,
193 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
195 /* this descriptor actually adds value, surprise! */
196 /* .iMACAddress = DYNAMIC */
197 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
198 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
199 .wNumberMCFilters = cpu_to_le16(0),
200 .bNumberPowerFilters = 0,
203 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
205 static struct usb_cdc_ncm_desc ncm_desc = {
206 .bLength = sizeof ncm_desc,
207 .bDescriptorType = USB_DT_CS_INTERFACE,
208 .bDescriptorSubType = USB_CDC_NCM_TYPE,
210 .bcdNcmVersion = cpu_to_le16(0x0100),
211 /* can process SetEthernetPacketFilter */
212 .bmNetworkCapabilities = NCAPS,
215 /* the default data interface has no endpoints ... */
217 static struct usb_interface_descriptor ncm_data_nop_intf = {
218 .bLength = sizeof ncm_data_nop_intf,
219 .bDescriptorType = USB_DT_INTERFACE,
221 .bInterfaceNumber = 1,
222 .bAlternateSetting = 0,
224 .bInterfaceClass = USB_CLASS_CDC_DATA,
225 .bInterfaceSubClass = 0,
226 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
227 /* .iInterface = DYNAMIC */
230 /* ... but the "real" data interface has two bulk endpoints */
232 static struct usb_interface_descriptor ncm_data_intf = {
233 .bLength = sizeof ncm_data_intf,
234 .bDescriptorType = USB_DT_INTERFACE,
236 .bInterfaceNumber = 1,
237 .bAlternateSetting = 1,
239 .bInterfaceClass = USB_CLASS_CDC_DATA,
240 .bInterfaceSubClass = 0,
241 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
242 /* .iInterface = DYNAMIC */
245 /* full speed support: */
247 static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
248 .bLength = USB_DT_ENDPOINT_SIZE,
249 .bDescriptorType = USB_DT_ENDPOINT,
251 .bEndpointAddress = USB_DIR_IN,
252 .bmAttributes = USB_ENDPOINT_XFER_INT,
253 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
254 .bInterval = NCM_STATUS_INTERVAL_MS,
257 static struct usb_endpoint_descriptor fs_ncm_in_desc = {
258 .bLength = USB_DT_ENDPOINT_SIZE,
259 .bDescriptorType = USB_DT_ENDPOINT,
261 .bEndpointAddress = USB_DIR_IN,
262 .bmAttributes = USB_ENDPOINT_XFER_BULK,
265 static struct usb_endpoint_descriptor fs_ncm_out_desc = {
266 .bLength = USB_DT_ENDPOINT_SIZE,
267 .bDescriptorType = USB_DT_ENDPOINT,
269 .bEndpointAddress = USB_DIR_OUT,
270 .bmAttributes = USB_ENDPOINT_XFER_BULK,
273 static struct usb_descriptor_header *ncm_fs_function[] = {
274 (struct usb_descriptor_header *) &ncm_iad_desc,
275 /* CDC NCM control descriptors */
276 (struct usb_descriptor_header *) &ncm_control_intf,
277 (struct usb_descriptor_header *) &ncm_header_desc,
278 (struct usb_descriptor_header *) &ncm_union_desc,
279 (struct usb_descriptor_header *) &ecm_desc,
280 (struct usb_descriptor_header *) &ncm_desc,
281 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
282 /* data interface, altsettings 0 and 1 */
283 (struct usb_descriptor_header *) &ncm_data_nop_intf,
284 (struct usb_descriptor_header *) &ncm_data_intf,
285 (struct usb_descriptor_header *) &fs_ncm_in_desc,
286 (struct usb_descriptor_header *) &fs_ncm_out_desc,
290 /* high speed support: */
292 static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
293 .bLength = USB_DT_ENDPOINT_SIZE,
294 .bDescriptorType = USB_DT_ENDPOINT,
296 .bEndpointAddress = USB_DIR_IN,
297 .bmAttributes = USB_ENDPOINT_XFER_INT,
298 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
299 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
301 static struct usb_endpoint_descriptor hs_ncm_in_desc = {
302 .bLength = USB_DT_ENDPOINT_SIZE,
303 .bDescriptorType = USB_DT_ENDPOINT,
305 .bEndpointAddress = USB_DIR_IN,
306 .bmAttributes = USB_ENDPOINT_XFER_BULK,
307 .wMaxPacketSize = cpu_to_le16(512),
310 static struct usb_endpoint_descriptor hs_ncm_out_desc = {
311 .bLength = USB_DT_ENDPOINT_SIZE,
312 .bDescriptorType = USB_DT_ENDPOINT,
314 .bEndpointAddress = USB_DIR_OUT,
315 .bmAttributes = USB_ENDPOINT_XFER_BULK,
316 .wMaxPacketSize = cpu_to_le16(512),
319 static struct usb_descriptor_header *ncm_hs_function[] = {
320 (struct usb_descriptor_header *) &ncm_iad_desc,
321 /* CDC NCM control descriptors */
322 (struct usb_descriptor_header *) &ncm_control_intf,
323 (struct usb_descriptor_header *) &ncm_header_desc,
324 (struct usb_descriptor_header *) &ncm_union_desc,
325 (struct usb_descriptor_header *) &ecm_desc,
326 (struct usb_descriptor_header *) &ncm_desc,
327 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
328 /* data interface, altsettings 0 and 1 */
329 (struct usb_descriptor_header *) &ncm_data_nop_intf,
330 (struct usb_descriptor_header *) &ncm_data_intf,
331 (struct usb_descriptor_header *) &hs_ncm_in_desc,
332 (struct usb_descriptor_header *) &hs_ncm_out_desc,
337 /* super speed support: */
339 static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
340 .bLength = USB_DT_ENDPOINT_SIZE,
341 .bDescriptorType = USB_DT_ENDPOINT,
343 .bEndpointAddress = USB_DIR_IN,
344 .bmAttributes = USB_ENDPOINT_XFER_INT,
345 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
346 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
349 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
350 .bLength = sizeof(ss_ncm_notify_comp_desc),
351 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
353 /* the following 3 values can be tweaked if necessary */
354 /* .bMaxBurst = 0, */
355 /* .bmAttributes = 0, */
356 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
359 static struct usb_endpoint_descriptor ss_ncm_in_desc = {
360 .bLength = USB_DT_ENDPOINT_SIZE,
361 .bDescriptorType = USB_DT_ENDPOINT,
363 .bEndpointAddress = USB_DIR_IN,
364 .bmAttributes = USB_ENDPOINT_XFER_BULK,
365 .wMaxPacketSize = cpu_to_le16(1024),
368 static struct usb_endpoint_descriptor ss_ncm_out_desc = {
369 .bLength = USB_DT_ENDPOINT_SIZE,
370 .bDescriptorType = USB_DT_ENDPOINT,
372 .bEndpointAddress = USB_DIR_OUT,
373 .bmAttributes = USB_ENDPOINT_XFER_BULK,
374 .wMaxPacketSize = cpu_to_le16(1024),
377 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
378 .bLength = sizeof(ss_ncm_bulk_comp_desc),
379 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
381 /* the following 2 values can be tweaked if necessary */
382 /* .bMaxBurst = 0, */
383 /* .bmAttributes = 0, */
386 static struct usb_descriptor_header *ncm_ss_function[] = {
387 (struct usb_descriptor_header *) &ncm_iad_desc,
388 /* CDC NCM control descriptors */
389 (struct usb_descriptor_header *) &ncm_control_intf,
390 (struct usb_descriptor_header *) &ncm_header_desc,
391 (struct usb_descriptor_header *) &ncm_union_desc,
392 (struct usb_descriptor_header *) &ecm_desc,
393 (struct usb_descriptor_header *) &ncm_desc,
394 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
395 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
396 /* data interface, altsettings 0 and 1 */
397 (struct usb_descriptor_header *) &ncm_data_nop_intf,
398 (struct usb_descriptor_header *) &ncm_data_intf,
399 (struct usb_descriptor_header *) &ss_ncm_in_desc,
400 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
401 (struct usb_descriptor_header *) &ss_ncm_out_desc,
402 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
406 /* string descriptors: */
408 #define STRING_CTRL_IDX 0
409 #define STRING_MAC_IDX 1
410 #define STRING_DATA_IDX 2
411 #define STRING_IAD_IDX 3
413 static struct usb_string ncm_string_defs[] = {
414 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
415 [STRING_MAC_IDX].s = "",
416 [STRING_DATA_IDX].s = "CDC Network Data",
417 [STRING_IAD_IDX].s = "CDC NCM",
418 { } /* end of list */
421 static struct usb_gadget_strings ncm_string_table = {
422 .language = 0x0409, /* en-us */
423 .strings = ncm_string_defs,
426 static struct usb_gadget_strings *ncm_strings[] = {
432 * Here are options for NCM Datagram Pointer table (NDP) parser.
433 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
434 * in NDP16 offsets and sizes fields are 1 16bit word wide,
435 * in NDP32 -- 2 16bit words wide. Also signatures are different.
436 * To make the parser code the same, put the differences in the structure,
437 * and switch pointers to the structures when the format is changed.
440 struct ndp_parser_opts {
446 unsigned ndplen_align;
447 /* sizes in u16 units */
448 unsigned dgram_item_len; /* index or length */
449 unsigned block_length;
453 unsigned next_ndp_index;
456 #define INIT_NDP16_OPTS { \
457 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
458 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
459 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
460 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
461 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
463 .dgram_item_len = 1, \
468 .next_ndp_index = 1, \
472 #define INIT_NDP32_OPTS { \
473 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
474 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
475 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
476 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
477 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
479 .dgram_item_len = 2, \
484 .next_ndp_index = 2, \
487 static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
488 static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
490 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
494 put_unaligned_le16((u16)val, *p);
497 put_unaligned_le32((u32)val, *p);
507 static inline unsigned get_ncm(__le16 **p, unsigned size)
513 tmp = get_unaligned_le16(*p);
516 tmp = get_unaligned_le32(*p);
526 /*-------------------------------------------------------------------------*/
528 static inline void ncm_reset_values(struct f_ncm *ncm)
530 ncm->parser_opts = &ndp16_opts;
532 ncm->port.cdc_filter = DEFAULT_FILTER;
534 /* doesn't make sense for ncm, fixed size used */
535 ncm->port.header_len = 0;
537 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
538 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
542 * Context: ncm->lock held
544 static void ncm_do_notify(struct f_ncm *ncm)
546 struct usb_request *req = ncm->notify_req;
547 struct usb_cdc_notification *event;
548 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
552 /* notification already in flight? */
553 if (atomic_read(&ncm->notify_count))
557 switch (ncm->notify_state) {
558 case NCM_NOTIFY_NONE:
561 case NCM_NOTIFY_CONNECT:
562 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
564 event->wValue = cpu_to_le16(1);
566 event->wValue = cpu_to_le16(0);
568 req->length = sizeof *event;
570 DBG(cdev, "notify connect %s\n",
571 ncm->is_open ? "true" : "false");
572 ncm->notify_state = NCM_NOTIFY_NONE;
575 case NCM_NOTIFY_SPEED:
576 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
577 event->wValue = cpu_to_le16(0);
578 event->wLength = cpu_to_le16(8);
579 req->length = NCM_STATUS_BYTECOUNT;
581 /* SPEED_CHANGE data is up/down speeds in bits/sec */
582 data = req->buf + sizeof *event;
583 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
586 DBG(cdev, "notify speed %u\n", ncm_bitrate(cdev->gadget));
587 ncm->notify_state = NCM_NOTIFY_CONNECT;
590 event->bmRequestType = 0xA1;
591 event->wIndex = cpu_to_le16(ncm->ctrl_id);
593 atomic_inc(&ncm->notify_count);
596 * In double buffering if there is a space in FIFO,
597 * completion callback can be called right after the call,
600 spin_unlock(&ncm->lock);
601 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
602 spin_lock(&ncm->lock);
604 atomic_dec(&ncm->notify_count);
605 DBG(cdev, "notify --> %d\n", status);
610 * Context: ncm->lock held
612 static void ncm_notify(struct f_ncm *ncm)
615 * NOTE on most versions of Linux, host side cdc-ethernet
616 * won't listen for notifications until its netdevice opens.
617 * The first notification then sits in the FIFO for a long
618 * time, and the second one is queued.
620 * If ncm_notify() is called before the second (CONNECT)
621 * notification is sent, then it will reset to send the SPEED
622 * notificaion again (and again, and again), but it's not a problem
624 ncm->notify_state = NCM_NOTIFY_SPEED;
628 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
630 struct f_ncm *ncm = req->context;
631 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
632 struct usb_cdc_notification *event = req->buf;
634 spin_lock(&ncm->lock);
635 switch (req->status) {
637 VDBG(cdev, "Notification %02x sent\n",
638 event->bNotificationType);
639 atomic_dec(&ncm->notify_count);
643 atomic_set(&ncm->notify_count, 0);
644 ncm->notify_state = NCM_NOTIFY_NONE;
647 DBG(cdev, "event %02x --> %d\n",
648 event->bNotificationType, req->status);
649 atomic_dec(&ncm->notify_count);
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 */
1108 /* Start the timer. */
1109 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS,
1110 HRTIMER_MODE_REL_SOFT);
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 * The transmit should only be run if no skb data has been sent
1156 * for a certain duration.
1158 static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1160 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1162 /* Only send if data is available. */
1163 if (!ncm->timer_stopping && ncm->skb_tx_data) {
1164 ncm->timer_force_tx = true;
1166 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1167 * XXX is not sane. The gadget layer should be redesigned so
1168 * XXX that the dev->wrap() invocations to build SKBs is transparent
1169 * XXX and performed in some way outside of the ndo_start_xmit
1172 ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
1174 ncm->timer_force_tx = false;
1176 return HRTIMER_NORESTART;
1179 static int ncm_unwrap_ntb(struct gether *port,
1180 struct sk_buff *skb,
1181 struct sk_buff_head *list)
1183 struct f_ncm *ncm = func_to_ncm(&port->func);
1184 __le16 *tmp = (void *) skb->data;
1185 unsigned index, index2;
1187 unsigned dg_len, dg_len2;
1190 struct sk_buff *skb2;
1192 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1193 unsigned frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
1194 const struct ndp_parser_opts *opts = ncm->parser_opts;
1195 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1199 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1200 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1202 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1203 skb->data, 32, false);
1209 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1210 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1213 tmp++; /* skip wSequence */
1215 block_len = get_ncm(&tmp, opts->block_length);
1216 /* (d)wBlockLength */
1217 if (block_len > ntb_max) {
1218 INFO(port->func.config->cdev, "OUT size exceeded\n");
1222 ndp_index = get_ncm(&tmp, opts->ndp_index);
1224 /* Run through all the NDP's in the NTB */
1230 if (((ndp_index % 4) != 0) ||
1231 (ndp_index < opts->nth_size) ||
1232 (ndp_index > (block_len -
1234 INFO(port->func.config->cdev, "Bad index: %#X\n",
1243 tmp = (void *)(skb->data + ndp_index);
1244 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1245 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1250 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);
1278 /* wDatagramIndex[0] */
1279 if ((index < opts->nth_size) ||
1280 (index > block_len - opts->dpe_size)) {
1281 INFO(port->func.config->cdev,
1282 "Bad index: %#X\n", index);
1288 * wDatagramLength[0]
1289 * ethernet hdr + crc or larger than max frame size
1291 if ((dg_len < 14 + crc_len) ||
1292 (dg_len > frame_max)) {
1293 INFO(port->func.config->cdev,
1294 "Bad dgram length: %#X\n", dg_len);
1300 crc = get_unaligned_le32(skb->data +
1303 crc2 = ~crc32_le(~0,
1307 INFO(port->func.config->cdev,
1313 index2 = get_ncm(&tmp, opts->dgram_item_len);
1314 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1316 /* wDatagramIndex[1] */
1317 if (index2 > block_len - opts->dpe_size) {
1318 INFO(port->func.config->cdev,
1319 "Bad index: %#X\n", index2);
1324 * Copy the data into a new skb.
1325 * This ensures the truesize is correct
1327 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1331 skb_put_data(skb2, skb->data + index,
1334 skb_queue_tail(list, skb2);
1336 ndp_len -= 2 * (opts->dgram_item_len * 2);
1339 if (index2 == 0 || dg_len2 == 0)
1341 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1342 } while (ndp_index);
1344 dev_consume_skb_any(skb);
1346 VDBG(port->func.config->cdev,
1347 "Parsed NTB with %d frames\n", dgram_counter);
1350 skb_queue_purge(list);
1351 dev_kfree_skb_any(skb);
1355 static void ncm_disable(struct usb_function *f)
1357 struct f_ncm *ncm = func_to_ncm(f);
1358 struct usb_composite_dev *cdev = f->config->cdev;
1360 DBG(cdev, "ncm deactivated\n");
1362 if (ncm->port.in_ep->enabled) {
1363 ncm->timer_stopping = true;
1365 gether_disconnect(&ncm->port);
1368 if (ncm->notify->enabled) {
1369 usb_ep_disable(ncm->notify);
1370 ncm->notify->desc = NULL;
1374 /*-------------------------------------------------------------------------*/
1377 * Callbacks let us notify the host about connect/disconnect when the
1378 * net device is opened or closed.
1380 * For testing, note that link states on this side include both opened
1381 * and closed variants of:
1383 * - disconnected/unconfigured
1384 * - configured but inactive (data alt 0)
1385 * - configured and active (data alt 1)
1387 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1388 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1389 * imply the host is actually polling the notification endpoint, and
1390 * likewise that "active" doesn't imply it's actually using the data
1391 * endpoints for traffic.
1394 static void ncm_open(struct gether *geth)
1396 struct f_ncm *ncm = func_to_ncm(&geth->func);
1398 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1400 spin_lock(&ncm->lock);
1401 ncm->is_open = true;
1403 spin_unlock(&ncm->lock);
1406 static void ncm_close(struct gether *geth)
1408 struct f_ncm *ncm = func_to_ncm(&geth->func);
1410 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1412 spin_lock(&ncm->lock);
1413 ncm->is_open = false;
1415 spin_unlock(&ncm->lock);
1418 /*-------------------------------------------------------------------------*/
1420 /* ethernet function driver setup/binding */
1422 static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1424 struct usb_composite_dev *cdev = c->cdev;
1425 struct f_ncm *ncm = func_to_ncm(f);
1426 struct usb_string *us;
1429 struct f_ncm_opts *ncm_opts;
1431 if (!can_support_ecm(cdev->gadget))
1434 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1436 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1437 * configurations are bound in sequence with list_for_each_entry,
1438 * in each configuration its functions are bound in sequence
1439 * with list_for_each_entry, so we assume no race condition
1440 * with regard to ncm_opts->bound access
1442 if (!ncm_opts->bound) {
1443 mutex_lock(&ncm_opts->lock);
1444 gether_set_gadget(ncm_opts->net, cdev->gadget);
1445 status = gether_register_netdev(ncm_opts->net);
1446 mutex_unlock(&ncm_opts->lock);
1449 ncm_opts->bound = true;
1451 us = usb_gstrings_attach(cdev, ncm_strings,
1452 ARRAY_SIZE(ncm_string_defs));
1455 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1456 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1457 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1458 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1459 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1461 /* allocate instance-specific interface IDs */
1462 status = usb_interface_id(c, f);
1465 ncm->ctrl_id = status;
1466 ncm_iad_desc.bFirstInterface = status;
1468 ncm_control_intf.bInterfaceNumber = status;
1469 ncm_union_desc.bMasterInterface0 = status;
1471 status = usb_interface_id(c, f);
1474 ncm->data_id = status;
1476 ncm_data_nop_intf.bInterfaceNumber = status;
1477 ncm_data_intf.bInterfaceNumber = status;
1478 ncm_union_desc.bSlaveInterface0 = status;
1482 /* allocate instance-specific endpoints */
1483 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1486 ncm->port.in_ep = ep;
1488 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1491 ncm->port.out_ep = ep;
1493 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1500 /* allocate notification request and buffer */
1501 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1502 if (!ncm->notify_req)
1504 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1505 if (!ncm->notify_req->buf)
1507 ncm->notify_req->context = ncm;
1508 ncm->notify_req->complete = ncm_notify_complete;
1511 * support all relevant hardware speeds... we expect that when
1512 * hardware is dual speed, all bulk-capable endpoints work at
1515 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1516 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1517 hs_ncm_notify_desc.bEndpointAddress =
1518 fs_ncm_notify_desc.bEndpointAddress;
1520 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1521 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1522 ss_ncm_notify_desc.bEndpointAddress =
1523 fs_ncm_notify_desc.bEndpointAddress;
1525 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1526 ncm_ss_function, ncm_ss_function);
1531 * NOTE: all that is done without knowing or caring about
1532 * the network link ... which is unavailable to this code
1533 * until we're activated via set_alt().
1536 ncm->port.open = ncm_open;
1537 ncm->port.close = ncm_close;
1539 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1540 ncm->task_timer.function = ncm_tx_timeout;
1542 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1543 gadget_is_superspeed(c->cdev->gadget) ? "super" :
1544 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1545 ncm->port.in_ep->name, ncm->port.out_ep->name,
1550 if (ncm->notify_req) {
1551 kfree(ncm->notify_req->buf);
1552 usb_ep_free_request(ncm->notify, ncm->notify_req);
1555 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1560 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1562 return container_of(to_config_group(item), struct f_ncm_opts,
1566 /* f_ncm_item_ops */
1567 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1569 /* f_ncm_opts_dev_addr */
1570 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1572 /* f_ncm_opts_host_addr */
1573 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1575 /* f_ncm_opts_qmult */
1576 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1578 /* f_ncm_opts_ifname */
1579 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1581 static struct configfs_attribute *ncm_attrs[] = {
1582 &ncm_opts_attr_dev_addr,
1583 &ncm_opts_attr_host_addr,
1584 &ncm_opts_attr_qmult,
1585 &ncm_opts_attr_ifname,
1589 static const struct config_item_type ncm_func_type = {
1590 .ct_item_ops = &ncm_item_ops,
1591 .ct_attrs = ncm_attrs,
1592 .ct_owner = THIS_MODULE,
1595 static void ncm_free_inst(struct usb_function_instance *f)
1597 struct f_ncm_opts *opts;
1599 opts = container_of(f, struct f_ncm_opts, func_inst);
1601 gether_cleanup(netdev_priv(opts->net));
1603 free_netdev(opts->net);
1607 static struct usb_function_instance *ncm_alloc_inst(void)
1609 struct f_ncm_opts *opts;
1611 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1613 return ERR_PTR(-ENOMEM);
1614 mutex_init(&opts->lock);
1615 opts->func_inst.free_func_inst = ncm_free_inst;
1616 opts->net = gether_setup_default();
1617 if (IS_ERR(opts->net)) {
1618 struct net_device *net = opts->net;
1620 return ERR_CAST(net);
1623 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1625 return &opts->func_inst;
1628 static void ncm_free(struct usb_function *f)
1631 struct f_ncm_opts *opts;
1633 ncm = func_to_ncm(f);
1634 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1636 mutex_lock(&opts->lock);
1638 mutex_unlock(&opts->lock);
1641 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1643 struct f_ncm *ncm = func_to_ncm(f);
1645 DBG(c->cdev, "ncm unbind\n");
1647 hrtimer_cancel(&ncm->task_timer);
1649 ncm_string_defs[0].id = 0;
1650 usb_free_all_descriptors(f);
1652 if (atomic_read(&ncm->notify_count)) {
1653 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1654 atomic_set(&ncm->notify_count, 0);
1657 kfree(ncm->notify_req->buf);
1658 usb_ep_free_request(ncm->notify, ncm->notify_req);
1661 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1664 struct f_ncm_opts *opts;
1667 /* allocate and initialize one new instance */
1668 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1670 return ERR_PTR(-ENOMEM);
1672 opts = container_of(fi, struct f_ncm_opts, func_inst);
1673 mutex_lock(&opts->lock);
1676 /* export host's Ethernet address in CDC format */
1677 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1678 sizeof(ncm->ethaddr));
1679 if (status < 12) { /* strlen("01234567890a") */
1681 mutex_unlock(&opts->lock);
1682 return ERR_PTR(-EINVAL);
1684 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1686 spin_lock_init(&ncm->lock);
1687 ncm_reset_values(ncm);
1688 ncm->port.ioport = netdev_priv(opts->net);
1689 mutex_unlock(&opts->lock);
1690 ncm->port.is_fixed = true;
1691 ncm->port.supports_multi_frame = true;
1693 ncm->port.func.name = "cdc_network";
1694 /* descriptors are per-instance copies */
1695 ncm->port.func.bind = ncm_bind;
1696 ncm->port.func.unbind = ncm_unbind;
1697 ncm->port.func.set_alt = ncm_set_alt;
1698 ncm->port.func.get_alt = ncm_get_alt;
1699 ncm->port.func.setup = ncm_setup;
1700 ncm->port.func.disable = ncm_disable;
1701 ncm->port.func.free_func = ncm_free;
1703 ncm->port.wrap = ncm_wrap_ntb;
1704 ncm->port.unwrap = ncm_unwrap_ntb;
1706 return &ncm->port.func;
1709 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1710 MODULE_LICENSE("GPL");
1711 MODULE_AUTHOR("Yauheni Kaliuta");