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"
29 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
30 * NCM is intended to be used with high-speed network attachments.
32 * Note that NCM requires the use of "alternate settings" for its data
33 * interface. This means that the set_alt() method has real work to do,
34 * and also means that a get_alt() method is required.
37 /* to trigger crc/non-crc ndp signature */
39 #define NCM_NDP_HDR_CRC 0x01000000
41 enum ncm_notify_state {
42 NCM_NOTIFY_NONE, /* don't notify */
43 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
44 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
53 struct usb_ep *notify;
54 struct usb_request *notify_req;
56 atomic_t notify_count;
59 const struct ndp_parser_opts *parser_opts;
64 * for notification, it is accessed from both
65 * callback and ethernet open/close
69 struct net_device *netdev;
71 /* For multi-frame NDP TX */
72 struct sk_buff *skb_tx_data;
73 struct sk_buff *skb_tx_ndp;
75 struct hrtimer task_timer;
78 static inline struct f_ncm *func_to_ncm(struct usb_function *f)
80 return container_of(f, struct f_ncm, port.func);
83 /*-------------------------------------------------------------------------*/
86 * We cannot group frames so use just the minimal size which ok to put
87 * one max-size ethernet frame.
88 * If the host can group frames, allow it to do that, 16K is selected,
89 * because it's used by default by the current linux host driver
91 #define NTB_DEFAULT_IN_SIZE 16384
92 #define NTB_OUT_SIZE 16384
94 /* Allocation for storing the NDP, 32 should suffice for a
95 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
96 * be transmitted in a single 16kB skb, though when sending full size
97 * packets this limit will be plenty.
98 * Smaller packets are not likely to be trying to maximize the
99 * throughput and will be mstly sending smaller infrequent frames.
101 #define TX_MAX_NUM_DPE 32
103 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
104 #define TX_TIMEOUT_NSECS 300000
107 * Although max mtu as dictated by u_ether is 15412 bytes, setting
108 * max_segment_size to 15426 would not be efficient. If user chooses segment
109 * size to be (>= 8192), then we can't aggregate more than one buffer in each
110 * NTB (assuming each packet coming from network layer is >= 8192 bytes) as ep
111 * maxpacket limit is 16384. So let max_segment_size be limited to 8000 to allow
112 * at least 2 packets to be aggregated reducing wastage of NTB buffer space
114 #define MAX_DATAGRAM_SIZE 8000
116 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
117 USB_CDC_NCM_NTB32_SUPPORTED)
119 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
120 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
121 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
122 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
123 .wNdpInDivisor = cpu_to_le16(4),
124 .wNdpInPayloadRemainder = cpu_to_le16(0),
125 .wNdpInAlignment = cpu_to_le16(4),
127 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
128 .wNdpOutDivisor = cpu_to_le16(4),
129 .wNdpOutPayloadRemainder = cpu_to_le16(0),
130 .wNdpOutAlignment = cpu_to_le16(4),
134 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
135 * packet, to simplify cancellation; and a big transfer interval, to
136 * waste less bandwidth.
139 #define NCM_STATUS_INTERVAL_MS 32
140 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
142 static struct usb_interface_assoc_descriptor ncm_iad_desc = {
143 .bLength = sizeof ncm_iad_desc,
144 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
146 /* .bFirstInterface = DYNAMIC, */
147 .bInterfaceCount = 2, /* control + data */
148 .bFunctionClass = USB_CLASS_COMM,
149 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
150 .bFunctionProtocol = USB_CDC_PROTO_NONE,
151 /* .iFunction = DYNAMIC */
154 /* interface descriptor: */
156 static struct usb_interface_descriptor ncm_control_intf = {
157 .bLength = sizeof ncm_control_intf,
158 .bDescriptorType = USB_DT_INTERFACE,
160 /* .bInterfaceNumber = DYNAMIC */
162 .bInterfaceClass = USB_CLASS_COMM,
163 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
164 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
165 /* .iInterface = DYNAMIC */
168 static struct usb_cdc_header_desc ncm_header_desc = {
169 .bLength = sizeof ncm_header_desc,
170 .bDescriptorType = USB_DT_CS_INTERFACE,
171 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
173 .bcdCDC = cpu_to_le16(0x0110),
176 static struct usb_cdc_union_desc ncm_union_desc = {
177 .bLength = sizeof(ncm_union_desc),
178 .bDescriptorType = USB_DT_CS_INTERFACE,
179 .bDescriptorSubType = USB_CDC_UNION_TYPE,
180 /* .bMasterInterface0 = DYNAMIC */
181 /* .bSlaveInterface0 = DYNAMIC */
184 static struct usb_cdc_ether_desc ecm_desc = {
185 .bLength = sizeof ecm_desc,
186 .bDescriptorType = USB_DT_CS_INTERFACE,
187 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
189 /* this descriptor actually adds value, surprise! */
190 /* .iMACAddress = DYNAMIC */
191 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
192 .wNumberMCFilters = cpu_to_le16(0),
193 .bNumberPowerFilters = 0,
196 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
198 static struct usb_cdc_ncm_desc ncm_desc = {
199 .bLength = sizeof ncm_desc,
200 .bDescriptorType = USB_DT_CS_INTERFACE,
201 .bDescriptorSubType = USB_CDC_NCM_TYPE,
203 .bcdNcmVersion = cpu_to_le16(0x0100),
204 /* can process SetEthernetPacketFilter */
205 .bmNetworkCapabilities = NCAPS,
208 /* the default data interface has no endpoints ... */
210 static struct usb_interface_descriptor ncm_data_nop_intf = {
211 .bLength = sizeof ncm_data_nop_intf,
212 .bDescriptorType = USB_DT_INTERFACE,
214 .bInterfaceNumber = 1,
215 .bAlternateSetting = 0,
217 .bInterfaceClass = USB_CLASS_CDC_DATA,
218 .bInterfaceSubClass = 0,
219 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
220 /* .iInterface = DYNAMIC */
223 /* ... but the "real" data interface has two bulk endpoints */
225 static struct usb_interface_descriptor ncm_data_intf = {
226 .bLength = sizeof ncm_data_intf,
227 .bDescriptorType = USB_DT_INTERFACE,
229 .bInterfaceNumber = 1,
230 .bAlternateSetting = 1,
232 .bInterfaceClass = USB_CLASS_CDC_DATA,
233 .bInterfaceSubClass = 0,
234 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
235 /* .iInterface = DYNAMIC */
238 /* full speed support: */
240 static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
244 .bEndpointAddress = USB_DIR_IN,
245 .bmAttributes = USB_ENDPOINT_XFER_INT,
246 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
247 .bInterval = NCM_STATUS_INTERVAL_MS,
250 static struct usb_endpoint_descriptor fs_ncm_in_desc = {
251 .bLength = USB_DT_ENDPOINT_SIZE,
252 .bDescriptorType = USB_DT_ENDPOINT,
254 .bEndpointAddress = USB_DIR_IN,
255 .bmAttributes = USB_ENDPOINT_XFER_BULK,
258 static struct usb_endpoint_descriptor fs_ncm_out_desc = {
259 .bLength = USB_DT_ENDPOINT_SIZE,
260 .bDescriptorType = USB_DT_ENDPOINT,
262 .bEndpointAddress = USB_DIR_OUT,
263 .bmAttributes = USB_ENDPOINT_XFER_BULK,
266 static struct usb_descriptor_header *ncm_fs_function[] = {
267 (struct usb_descriptor_header *) &ncm_iad_desc,
268 /* CDC NCM control descriptors */
269 (struct usb_descriptor_header *) &ncm_control_intf,
270 (struct usb_descriptor_header *) &ncm_header_desc,
271 (struct usb_descriptor_header *) &ncm_union_desc,
272 (struct usb_descriptor_header *) &ecm_desc,
273 (struct usb_descriptor_header *) &ncm_desc,
274 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
275 /* data interface, altsettings 0 and 1 */
276 (struct usb_descriptor_header *) &ncm_data_nop_intf,
277 (struct usb_descriptor_header *) &ncm_data_intf,
278 (struct usb_descriptor_header *) &fs_ncm_in_desc,
279 (struct usb_descriptor_header *) &fs_ncm_out_desc,
283 /* high speed support: */
285 static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
286 .bLength = USB_DT_ENDPOINT_SIZE,
287 .bDescriptorType = USB_DT_ENDPOINT,
289 .bEndpointAddress = USB_DIR_IN,
290 .bmAttributes = USB_ENDPOINT_XFER_INT,
291 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
292 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
294 static struct usb_endpoint_descriptor hs_ncm_in_desc = {
295 .bLength = USB_DT_ENDPOINT_SIZE,
296 .bDescriptorType = USB_DT_ENDPOINT,
298 .bEndpointAddress = USB_DIR_IN,
299 .bmAttributes = USB_ENDPOINT_XFER_BULK,
300 .wMaxPacketSize = cpu_to_le16(512),
303 static struct usb_endpoint_descriptor hs_ncm_out_desc = {
304 .bLength = USB_DT_ENDPOINT_SIZE,
305 .bDescriptorType = USB_DT_ENDPOINT,
307 .bEndpointAddress = USB_DIR_OUT,
308 .bmAttributes = USB_ENDPOINT_XFER_BULK,
309 .wMaxPacketSize = cpu_to_le16(512),
312 static struct usb_descriptor_header *ncm_hs_function[] = {
313 (struct usb_descriptor_header *) &ncm_iad_desc,
314 /* CDC NCM control descriptors */
315 (struct usb_descriptor_header *) &ncm_control_intf,
316 (struct usb_descriptor_header *) &ncm_header_desc,
317 (struct usb_descriptor_header *) &ncm_union_desc,
318 (struct usb_descriptor_header *) &ecm_desc,
319 (struct usb_descriptor_header *) &ncm_desc,
320 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
321 /* data interface, altsettings 0 and 1 */
322 (struct usb_descriptor_header *) &ncm_data_nop_intf,
323 (struct usb_descriptor_header *) &ncm_data_intf,
324 (struct usb_descriptor_header *) &hs_ncm_in_desc,
325 (struct usb_descriptor_header *) &hs_ncm_out_desc,
330 /* super speed support: */
332 static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
333 .bLength = USB_DT_ENDPOINT_SIZE,
334 .bDescriptorType = USB_DT_ENDPOINT,
336 .bEndpointAddress = USB_DIR_IN,
337 .bmAttributes = USB_ENDPOINT_XFER_INT,
338 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
339 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
342 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
343 .bLength = sizeof(ss_ncm_notify_comp_desc),
344 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
346 /* the following 3 values can be tweaked if necessary */
347 /* .bMaxBurst = 0, */
348 /* .bmAttributes = 0, */
349 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
352 static struct usb_endpoint_descriptor ss_ncm_in_desc = {
353 .bLength = USB_DT_ENDPOINT_SIZE,
354 .bDescriptorType = USB_DT_ENDPOINT,
356 .bEndpointAddress = USB_DIR_IN,
357 .bmAttributes = USB_ENDPOINT_XFER_BULK,
358 .wMaxPacketSize = cpu_to_le16(1024),
361 static struct usb_endpoint_descriptor ss_ncm_out_desc = {
362 .bLength = USB_DT_ENDPOINT_SIZE,
363 .bDescriptorType = USB_DT_ENDPOINT,
365 .bEndpointAddress = USB_DIR_OUT,
366 .bmAttributes = USB_ENDPOINT_XFER_BULK,
367 .wMaxPacketSize = cpu_to_le16(1024),
370 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
371 .bLength = sizeof(ss_ncm_bulk_comp_desc),
372 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
374 /* the following 2 values can be tweaked if necessary */
376 /* .bmAttributes = 0, */
379 static struct usb_descriptor_header *ncm_ss_function[] = {
380 (struct usb_descriptor_header *) &ncm_iad_desc,
381 /* CDC NCM control descriptors */
382 (struct usb_descriptor_header *) &ncm_control_intf,
383 (struct usb_descriptor_header *) &ncm_header_desc,
384 (struct usb_descriptor_header *) &ncm_union_desc,
385 (struct usb_descriptor_header *) &ecm_desc,
386 (struct usb_descriptor_header *) &ncm_desc,
387 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
388 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
389 /* data interface, altsettings 0 and 1 */
390 (struct usb_descriptor_header *) &ncm_data_nop_intf,
391 (struct usb_descriptor_header *) &ncm_data_intf,
392 (struct usb_descriptor_header *) &ss_ncm_in_desc,
393 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
394 (struct usb_descriptor_header *) &ss_ncm_out_desc,
395 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
399 /* string descriptors: */
401 #define STRING_CTRL_IDX 0
402 #define STRING_MAC_IDX 1
403 #define STRING_DATA_IDX 2
404 #define STRING_IAD_IDX 3
406 static struct usb_string ncm_string_defs[] = {
407 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
408 [STRING_MAC_IDX].s = "",
409 [STRING_DATA_IDX].s = "CDC Network Data",
410 [STRING_IAD_IDX].s = "CDC NCM",
411 { } /* end of list */
414 static struct usb_gadget_strings ncm_string_table = {
415 .language = 0x0409, /* en-us */
416 .strings = ncm_string_defs,
419 static struct usb_gadget_strings *ncm_strings[] = {
425 * Here are options for NCM Datagram Pointer table (NDP) parser.
426 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
427 * in NDP16 offsets and sizes fields are 1 16bit word wide,
428 * in NDP32 -- 2 16bit words wide. Also signatures are different.
429 * To make the parser code the same, put the differences in the structure,
430 * and switch pointers to the structures when the format is changed.
433 struct ndp_parser_opts {
439 unsigned ndplen_align;
440 /* sizes in u16 units */
441 unsigned dgram_item_len; /* index or length */
442 unsigned block_length;
446 unsigned next_ndp_index;
449 static const struct ndp_parser_opts ndp16_opts = {
450 .nth_sign = USB_CDC_NCM_NTH16_SIGN,
451 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN,
452 .nth_size = sizeof(struct usb_cdc_ncm_nth16),
453 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16),
454 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16),
464 static const struct ndp_parser_opts ndp32_opts = {
465 .nth_sign = USB_CDC_NCM_NTH32_SIGN,
466 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN,
467 .nth_size = sizeof(struct usb_cdc_ncm_nth32),
468 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32),
469 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32),
479 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
483 put_unaligned_le16((u16)val, *p);
486 put_unaligned_le32((u32)val, *p);
496 static inline unsigned get_ncm(__le16 **p, unsigned size)
502 tmp = get_unaligned_le16(*p);
505 tmp = get_unaligned_le32(*p);
515 /*-------------------------------------------------------------------------*/
517 static inline void ncm_reset_values(struct f_ncm *ncm)
519 ncm->parser_opts = &ndp16_opts;
521 ncm->ndp_sign = ncm->parser_opts->ndp_sign;
522 ncm->port.cdc_filter = DEFAULT_FILTER;
524 /* doesn't make sense for ncm, fixed size used */
525 ncm->port.header_len = 0;
527 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
528 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
532 * Context: ncm->lock held
534 static void ncm_do_notify(struct f_ncm *ncm)
536 struct usb_request *req = ncm->notify_req;
537 struct usb_cdc_notification *event;
538 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
542 /* notification already in flight? */
543 if (atomic_read(&ncm->notify_count))
547 switch (ncm->notify_state) {
548 case NCM_NOTIFY_NONE:
551 case NCM_NOTIFY_CONNECT:
552 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
554 event->wValue = cpu_to_le16(1);
556 event->wValue = cpu_to_le16(0);
558 req->length = sizeof *event;
560 DBG(cdev, "notify connect %s\n",
561 ncm->is_open ? "true" : "false");
562 ncm->notify_state = NCM_NOTIFY_NONE;
565 case NCM_NOTIFY_SPEED:
566 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
567 event->wValue = cpu_to_le16(0);
568 event->wLength = cpu_to_le16(8);
569 req->length = NCM_STATUS_BYTECOUNT;
571 /* SPEED_CHANGE data is up/down speeds in bits/sec */
572 data = req->buf + sizeof *event;
573 data[0] = cpu_to_le32(gether_bitrate(cdev->gadget));
576 DBG(cdev, "notify speed %u\n", gether_bitrate(cdev->gadget));
577 ncm->notify_state = NCM_NOTIFY_CONNECT;
580 event->bmRequestType = 0xA1;
581 event->wIndex = cpu_to_le16(ncm->ctrl_id);
583 atomic_inc(&ncm->notify_count);
586 * In double buffering if there is a space in FIFO,
587 * completion callback can be called right after the call,
590 spin_unlock(&ncm->lock);
591 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
592 spin_lock(&ncm->lock);
594 atomic_dec(&ncm->notify_count);
595 DBG(cdev, "notify --> %d\n", status);
600 * Context: ncm->lock held
602 static void ncm_notify(struct f_ncm *ncm)
605 * NOTE on most versions of Linux, host side cdc-ethernet
606 * won't listen for notifications until its netdevice opens.
607 * The first notification then sits in the FIFO for a long
608 * time, and the second one is queued.
610 * If ncm_notify() is called before the second (CONNECT)
611 * notification is sent, then it will reset to send the SPEED
612 * notificaion again (and again, and again), but it's not a problem
614 ncm->notify_state = NCM_NOTIFY_SPEED;
618 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
620 struct f_ncm *ncm = req->context;
621 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
622 struct usb_cdc_notification *event = req->buf;
624 spin_lock(&ncm->lock);
625 switch (req->status) {
627 VDBG(cdev, "Notification %02x sent\n",
628 event->bNotificationType);
629 atomic_dec(&ncm->notify_count);
633 atomic_set(&ncm->notify_count, 0);
634 ncm->notify_state = NCM_NOTIFY_NONE;
637 DBG(cdev, "event %02x --> %d\n",
638 event->bNotificationType, req->status);
639 atomic_dec(&ncm->notify_count);
643 spin_unlock(&ncm->lock);
646 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
648 /* now for SET_NTB_INPUT_SIZE only */
650 struct usb_function *f = req->context;
651 struct f_ncm *ncm = func_to_ncm(f);
652 struct usb_composite_dev *cdev = f->config->cdev;
655 if (req->status || req->actual != req->length) {
656 DBG(cdev, "Bad control-OUT transfer\n");
660 in_size = get_unaligned_le32(req->buf);
661 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
662 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
663 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
667 ncm->port.fixed_in_len = in_size;
668 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
676 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
678 struct f_ncm *ncm = func_to_ncm(f);
679 struct usb_composite_dev *cdev = f->config->cdev;
680 struct usb_request *req = cdev->req;
681 int value = -EOPNOTSUPP;
682 u16 w_index = le16_to_cpu(ctrl->wIndex);
683 u16 w_value = le16_to_cpu(ctrl->wValue);
684 u16 w_length = le16_to_cpu(ctrl->wLength);
687 * composite driver infrastructure handles everything except
688 * CDC class messages; interface activation uses set_alt().
690 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
691 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
692 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
694 * see 6.2.30: no data, wIndex = interface,
695 * wValue = packet filter bitmap
697 if (w_length != 0 || w_index != ncm->ctrl_id)
699 DBG(cdev, "packet filter %02x\n", w_value);
701 * REVISIT locking of cdc_filter. This assumes the UDC
702 * driver won't have a concurrent packet TX irq running on
703 * another CPU; or that if it does, this write is atomic...
705 ncm->port.cdc_filter = w_value;
710 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
711 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
712 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
713 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
714 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
715 * case USB_CDC_GET_ETHERNET_STATISTIC:
718 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
719 | USB_CDC_GET_NTB_PARAMETERS:
721 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
723 value = w_length > sizeof ntb_parameters ?
724 sizeof ntb_parameters : w_length;
725 memcpy(req->buf, &ntb_parameters, value);
726 VDBG(cdev, "Host asked NTB parameters\n");
729 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
730 | USB_CDC_GET_NTB_INPUT_SIZE:
732 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
734 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
736 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
737 ncm->port.fixed_in_len);
740 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
741 | USB_CDC_SET_NTB_INPUT_SIZE:
743 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
745 req->complete = ncm_ep0out_complete;
746 req->length = w_length;
753 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
754 | USB_CDC_GET_NTB_FORMAT:
758 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
760 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
761 put_unaligned_le16(format, req->buf);
763 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
767 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
768 | USB_CDC_SET_NTB_FORMAT:
770 if (w_length != 0 || w_index != ncm->ctrl_id)
774 ncm->parser_opts = &ndp16_opts;
775 DBG(cdev, "NCM16 selected\n");
778 ncm->parser_opts = &ndp32_opts;
779 DBG(cdev, "NCM32 selected\n");
787 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
788 | USB_CDC_GET_CRC_MODE:
792 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
794 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
795 put_unaligned_le16(is_crc, req->buf);
797 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
801 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
802 | USB_CDC_SET_CRC_MODE:
804 if (w_length != 0 || w_index != ncm->ctrl_id)
809 DBG(cdev, "non-CRC mode selected\n");
813 DBG(cdev, "CRC mode selected\n");
822 /* and disabled in ncm descriptor: */
823 /* case USB_CDC_GET_NET_ADDRESS: */
824 /* case USB_CDC_SET_NET_ADDRESS: */
825 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
826 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
830 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
831 ctrl->bRequestType, ctrl->bRequest,
832 w_value, w_index, w_length);
834 ncm->ndp_sign = ncm->parser_opts->ndp_sign |
835 (ncm->is_crc ? NCM_NDP_HDR_CRC : 0);
837 /* respond with data transfer or status phase? */
839 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
840 ctrl->bRequestType, ctrl->bRequest,
841 w_value, w_index, w_length);
844 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
846 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
847 ctrl->bRequestType, ctrl->bRequest,
851 /* device either stalls (value < 0) or reports success */
856 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
858 struct f_ncm *ncm = func_to_ncm(f);
859 struct usb_composite_dev *cdev = f->config->cdev;
861 /* Control interface has only altsetting 0 */
862 if (intf == ncm->ctrl_id) {
866 DBG(cdev, "reset ncm control %d\n", intf);
867 usb_ep_disable(ncm->notify);
869 if (!(ncm->notify->desc)) {
870 DBG(cdev, "init ncm ctrl %d\n", intf);
871 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
874 usb_ep_enable(ncm->notify);
876 /* Data interface has two altsettings, 0 and 1 */
877 } else if (intf == ncm->data_id) {
881 if (ncm->port.in_ep->enabled) {
882 DBG(cdev, "reset ncm\n");
884 gether_disconnect(&ncm->port);
885 ncm_reset_values(ncm);
889 * CDC Network only sends data in non-default altsettings.
890 * Changing altsettings resets filters, statistics, etc.
893 struct net_device *net;
895 if (!ncm->port.in_ep->desc ||
896 !ncm->port.out_ep->desc) {
897 DBG(cdev, "init ncm\n");
898 if (config_ep_by_speed(cdev->gadget, f,
900 config_ep_by_speed(cdev->gadget, f,
902 ncm->port.in_ep->desc = NULL;
903 ncm->port.out_ep->desc = NULL;
909 /* Enable zlps by default for NCM conformance;
910 * override for musb_hdrc (avoids txdma ovhead)
912 ncm->port.is_zlp_ok =
913 gadget_is_zlp_supported(cdev->gadget);
914 ncm->port.cdc_filter = DEFAULT_FILTER;
915 DBG(cdev, "activate ncm\n");
916 net = gether_connect(&ncm->port);
922 spin_lock(&ncm->lock);
924 spin_unlock(&ncm->lock);
934 * Because the data interface supports multiple altsettings,
935 * this NCM function *MUST* implement a get_alt() method.
937 static int ncm_get_alt(struct usb_function *f, unsigned intf)
939 struct f_ncm *ncm = func_to_ncm(f);
941 if (intf == ncm->ctrl_id)
943 return ncm->port.in_ep->enabled ? 1 : 0;
946 static struct sk_buff *package_for_tx(struct f_ncm *ncm)
949 struct sk_buff *skb2 = NULL;
954 const struct ndp_parser_opts *opts = ncm->parser_opts;
955 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
956 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
959 hrtimer_try_to_cancel(&ncm->task_timer);
961 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
962 ncm->skb_tx_data->len;
963 ndp_index = ncm->skb_tx_data->len + ndp_pad;
964 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
966 /* Set the final BlockLength and wNdpIndex */
967 ntb_iter = (void *) ncm->skb_tx_data->data;
968 /* Increment pointer to BlockLength */
969 ntb_iter += 2 + 1 + 1;
970 put_ncm(&ntb_iter, opts->block_length, new_len);
971 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
973 /* Set the final NDP wLength */
974 new_len = opts->ndp_size +
975 (ncm->ndp_dgram_count * dgram_idx_len);
976 ncm->ndp_dgram_count = 0;
977 /* Increment from start to wLength */
978 ntb_iter = (void *) ncm->skb_tx_ndp->data;
980 put_unaligned_le16(new_len, ntb_iter);
983 swap(skb2, ncm->skb_tx_data);
984 if (ncm->skb_tx_data) {
985 dev_consume_skb_any(ncm->skb_tx_data);
986 ncm->skb_tx_data = NULL;
989 /* Insert NDP alignment. */
990 skb_put_zero(skb2, ndp_pad);
992 /* Copy NTB across. */
993 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
994 dev_consume_skb_any(ncm->skb_tx_ndp);
995 ncm->skb_tx_ndp = NULL;
997 /* Insert zero'd datagram. */
998 skb_put_zero(skb2, dgram_idx_len);
1003 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1004 struct sk_buff *skb)
1006 struct f_ncm *ncm = func_to_ncm(&port->func);
1007 struct sk_buff *skb2 = NULL;
1015 unsigned max_size = ncm->port.fixed_in_len;
1016 const struct ndp_parser_opts *opts = ncm->parser_opts;
1017 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1018 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1019 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1020 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
1022 /* Add the CRC if required up front */
1030 crc_pos = skb_put(skb, sizeof(uint32_t));
1031 put_unaligned_le32(crc, crc_pos);
1034 /* If the new skb is too big for the current NCM NTB then
1035 * set the current stored skb to be sent now and clear it
1036 * ready for new data.
1037 * NOTE: Assume maximum align for speed of calculation.
1039 if (ncm->skb_tx_data
1040 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1041 || (ncm->skb_tx_data->len +
1042 div + rem + skb->len +
1043 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1045 skb2 = package_for_tx(ncm);
1050 if (!ncm->skb_tx_data) {
1051 ncb_len = opts->nth_size;
1052 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1053 ncb_len += dgram_pad;
1055 /* Create a new skb for the NTH and datagrams. */
1056 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1057 if (!ncm->skb_tx_data)
1060 ncm->skb_tx_data->dev = ncm->netdev;
1061 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len);
1063 put_unaligned_le32(opts->nth_sign, ntb_data);
1066 put_unaligned_le16(opts->nth_size, ntb_data++);
1068 /* Allocate an skb for storing the NDP,
1069 * TX_MAX_NUM_DPE should easily suffice for a
1072 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1076 if (!ncm->skb_tx_ndp)
1079 ncm->skb_tx_ndp->dev = ncm->netdev;
1080 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size);
1081 memset(ntb_ndp, 0, ncb_len);
1083 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1086 /* There is always a zeroed entry */
1087 ncm->ndp_dgram_count = 1;
1089 /* Note: we skip opts->next_ndp_index */
1091 /* Start the timer. */
1092 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS,
1093 HRTIMER_MODE_REL_SOFT);
1096 /* Add the datagram position entries */
1097 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len);
1099 ncb_len = ncm->skb_tx_data->len;
1100 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1101 ncb_len += dgram_pad;
1103 /* (d)wDatagramIndex */
1104 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1105 /* (d)wDatagramLength */
1106 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1107 ncm->ndp_dgram_count++;
1109 /* Add the new data to the skb */
1110 skb_put_zero(ncm->skb_tx_data, dgram_pad);
1111 skb_put_data(ncm->skb_tx_data, skb->data, skb->len);
1112 dev_consume_skb_any(skb);
1115 } else if (ncm->skb_tx_data) {
1116 /* If we get here ncm_wrap_ntb() was called with NULL skb,
1117 * because eth_start_xmit() was called with NULL skb by
1118 * ncm_tx_timeout() - hence, this is our signal to flush/send.
1120 skb2 = package_for_tx(ncm);
1128 ncm->netdev->stats.tx_dropped++;
1131 dev_kfree_skb_any(skb);
1132 if (ncm->skb_tx_data)
1133 dev_kfree_skb_any(ncm->skb_tx_data);
1134 if (ncm->skb_tx_ndp)
1135 dev_kfree_skb_any(ncm->skb_tx_ndp);
1141 * The transmit should only be run if no skb data has been sent
1142 * for a certain duration.
1144 static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1146 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1147 struct net_device *netdev = READ_ONCE(ncm->netdev);
1150 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1151 * XXX is not sane. The gadget layer should be redesigned so
1152 * XXX that the dev->wrap() invocations to build SKBs is transparent
1153 * XXX and performed in some way outside of the ndo_start_xmit
1156 * This will call directly into u_ether's eth_start_xmit()
1158 netdev->netdev_ops->ndo_start_xmit(NULL, netdev);
1160 return HRTIMER_NORESTART;
1163 static int ncm_unwrap_ntb(struct gether *port,
1164 struct sk_buff *skb,
1165 struct sk_buff_head *list)
1167 struct f_ncm *ncm = func_to_ncm(&port->func);
1168 unsigned char *ntb_ptr = skb->data;
1170 unsigned index, index2;
1172 unsigned dg_len, dg_len2;
1175 struct sk_buff *skb2;
1177 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1179 const struct ndp_parser_opts *opts = ncm->parser_opts;
1180 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1182 int to_process = skb->len;
1183 struct f_ncm_opts *ncm_opts;
1185 ncm_opts = container_of(port->func.fi, struct f_ncm_opts, func_inst);
1186 frame_max = ncm_opts->max_segment_size;
1189 tmp = (__le16 *)ntb_ptr;
1192 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1193 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1195 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1196 skb->data, 32, false);
1202 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1203 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1206 tmp++; /* skip wSequence */
1208 block_len = get_ncm(&tmp, opts->block_length);
1209 /* (d)wBlockLength */
1210 if (block_len > ntb_max) {
1211 INFO(port->func.config->cdev, "OUT size exceeded\n");
1215 ndp_index = get_ncm(&tmp, opts->ndp_index);
1217 /* Run through all the NDP's in the NTB */
1223 if (((ndp_index % 4) != 0) ||
1224 (ndp_index < opts->nth_size) ||
1225 (ndp_index > (block_len -
1227 INFO(port->func.config->cdev, "Bad index: %#X\n",
1236 tmp = (__le16 *)(ntb_ptr + ndp_index);
1237 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1238 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1243 ndp_len = get_unaligned_le16(tmp++);
1248 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1249 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1250 * Each entry is a dgram index and a dgram length.
1252 if ((ndp_len < opts->ndp_size
1253 + 2 * 2 * (opts->dgram_item_len * 2)) ||
1254 (ndp_len % opts->ndplen_align != 0)) {
1255 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1259 tmp += opts->reserved1;
1260 /* Check for another NDP (d)wNextNdpIndex */
1261 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
1262 tmp += opts->reserved2;
1264 ndp_len -= opts->ndp_size;
1265 index2 = get_ncm(&tmp, opts->dgram_item_len);
1266 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1271 /* wDatagramIndex[0] */
1272 if ((index < opts->nth_size) ||
1273 (index > block_len - opts->dpe_size)) {
1274 INFO(port->func.config->cdev,
1275 "Bad index: %#X\n", index);
1281 * wDatagramLength[0]
1282 * ethernet hdr + crc or larger than max frame size
1284 if ((dg_len < 14 + crc_len) ||
1285 (dg_len > frame_max)) {
1286 INFO(port->func.config->cdev,
1287 "Bad dgram length: %#X\n", dg_len);
1293 crc = get_unaligned_le32(ntb_ptr +
1296 crc2 = ~crc32_le(~0,
1300 INFO(port->func.config->cdev,
1306 index2 = get_ncm(&tmp, opts->dgram_item_len);
1307 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1309 /* wDatagramIndex[1] */
1310 if (index2 > block_len - opts->dpe_size) {
1311 INFO(port->func.config->cdev,
1312 "Bad index: %#X\n", index2);
1317 * Copy the data into a new skb.
1318 * This ensures the truesize is correct
1320 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1324 skb_put_data(skb2, ntb_ptr + index,
1327 skb_queue_tail(list, skb2);
1329 ndp_len -= 2 * (opts->dgram_item_len * 2);
1332 if (index2 == 0 || dg_len2 == 0)
1334 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1335 } while (ndp_index);
1337 VDBG(port->func.config->cdev,
1338 "Parsed NTB with %d frames\n", dgram_counter);
1340 to_process -= block_len;
1343 * Windows NCM driver avoids USB ZLPs by adding a 1-byte
1344 * zero pad as needed.
1346 if (to_process == 1 &&
1347 (*(unsigned char *)(ntb_ptr + block_len) == 0x00)) {
1349 } else if ((to_process > 0) && (block_len != 0)) {
1350 ntb_ptr = (unsigned char *)(ntb_ptr + block_len);
1354 dev_consume_skb_any(skb);
1358 skb_queue_purge(list);
1359 dev_kfree_skb_any(skb);
1363 static void ncm_disable(struct usb_function *f)
1365 struct f_ncm *ncm = func_to_ncm(f);
1366 struct usb_composite_dev *cdev = f->config->cdev;
1368 DBG(cdev, "ncm deactivated\n");
1370 if (ncm->port.in_ep->enabled) {
1372 gether_disconnect(&ncm->port);
1375 if (ncm->notify->enabled) {
1376 usb_ep_disable(ncm->notify);
1377 ncm->notify->desc = NULL;
1381 /*-------------------------------------------------------------------------*/
1384 * Callbacks let us notify the host about connect/disconnect when the
1385 * net device is opened or closed.
1387 * For testing, note that link states on this side include both opened
1388 * and closed variants of:
1390 * - disconnected/unconfigured
1391 * - configured but inactive (data alt 0)
1392 * - configured and active (data alt 1)
1394 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1395 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1396 * imply the host is actually polling the notification endpoint, and
1397 * likewise that "active" doesn't imply it's actually using the data
1398 * endpoints for traffic.
1401 static void ncm_open(struct gether *geth)
1403 struct f_ncm *ncm = func_to_ncm(&geth->func);
1405 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1407 spin_lock(&ncm->lock);
1408 ncm->is_open = true;
1410 spin_unlock(&ncm->lock);
1413 static void ncm_close(struct gether *geth)
1415 struct f_ncm *ncm = func_to_ncm(&geth->func);
1417 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1419 spin_lock(&ncm->lock);
1420 ncm->is_open = false;
1422 spin_unlock(&ncm->lock);
1425 /*-------------------------------------------------------------------------*/
1427 /* ethernet function driver setup/binding */
1429 static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1431 struct usb_composite_dev *cdev = c->cdev;
1432 struct f_ncm *ncm = func_to_ncm(f);
1433 struct usb_string *us;
1436 struct f_ncm_opts *ncm_opts;
1438 if (!can_support_ecm(cdev->gadget))
1441 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1443 if (cdev->use_os_string) {
1444 f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
1446 if (!f->os_desc_table)
1449 f->os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc;
1452 mutex_lock(&ncm_opts->lock);
1453 gether_set_gadget(ncm_opts->net, cdev->gadget);
1454 if (!ncm_opts->bound) {
1455 ncm_opts->net->mtu = (ncm_opts->max_segment_size - ETH_HLEN);
1456 status = gether_register_netdev(ncm_opts->net);
1458 mutex_unlock(&ncm_opts->lock);
1463 ncm_opts->bound = true;
1465 us = usb_gstrings_attach(cdev, ncm_strings,
1466 ARRAY_SIZE(ncm_string_defs));
1468 status = PTR_ERR(us);
1471 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1472 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1473 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1474 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1475 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1477 /* allocate instance-specific interface IDs */
1478 status = usb_interface_id(c, f);
1481 ncm->ctrl_id = status;
1482 ncm_iad_desc.bFirstInterface = status;
1484 ncm_control_intf.bInterfaceNumber = status;
1485 ncm_union_desc.bMasterInterface0 = status;
1487 if (cdev->use_os_string)
1488 f->os_desc_table[0].if_id =
1489 ncm_iad_desc.bFirstInterface;
1491 status = usb_interface_id(c, f);
1494 ncm->data_id = status;
1496 ncm_data_nop_intf.bInterfaceNumber = status;
1497 ncm_data_intf.bInterfaceNumber = status;
1498 ncm_union_desc.bSlaveInterface0 = status;
1500 ecm_desc.wMaxSegmentSize = cpu_to_le16(ncm_opts->max_segment_size);
1504 /* allocate instance-specific endpoints */
1505 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1508 ncm->port.in_ep = ep;
1510 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1513 ncm->port.out_ep = ep;
1515 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1522 /* allocate notification request and buffer */
1523 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1524 if (!ncm->notify_req)
1526 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1527 if (!ncm->notify_req->buf)
1529 ncm->notify_req->context = ncm;
1530 ncm->notify_req->complete = ncm_notify_complete;
1533 * support all relevant hardware speeds... we expect that when
1534 * hardware is dual speed, all bulk-capable endpoints work at
1537 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1538 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1539 hs_ncm_notify_desc.bEndpointAddress =
1540 fs_ncm_notify_desc.bEndpointAddress;
1542 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1543 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1544 ss_ncm_notify_desc.bEndpointAddress =
1545 fs_ncm_notify_desc.bEndpointAddress;
1547 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1548 ncm_ss_function, ncm_ss_function);
1553 * NOTE: all that is done without knowing or caring about
1554 * the network link ... which is unavailable to this code
1555 * until we're activated via set_alt().
1558 ncm->port.open = ncm_open;
1559 ncm->port.close = ncm_close;
1561 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1562 ncm->task_timer.function = ncm_tx_timeout;
1564 DBG(cdev, "CDC Network: IN/%s OUT/%s NOTIFY/%s\n",
1565 ncm->port.in_ep->name, ncm->port.out_ep->name,
1570 kfree(f->os_desc_table);
1573 if (ncm->notify_req) {
1574 kfree(ncm->notify_req->buf);
1575 usb_ep_free_request(ncm->notify, ncm->notify_req);
1578 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1583 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1585 return container_of(to_config_group(item), struct f_ncm_opts,
1589 /* f_ncm_item_ops */
1590 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1592 /* f_ncm_opts_dev_addr */
1593 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1595 /* f_ncm_opts_host_addr */
1596 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1598 /* f_ncm_opts_qmult */
1599 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1601 /* f_ncm_opts_ifname */
1602 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1604 static ssize_t ncm_opts_max_segment_size_show(struct config_item *item,
1607 struct f_ncm_opts *opts = to_f_ncm_opts(item);
1610 mutex_lock(&opts->lock);
1611 segment_size = opts->max_segment_size;
1612 mutex_unlock(&opts->lock);
1614 return sysfs_emit(page, "%u\n", segment_size);
1617 static ssize_t ncm_opts_max_segment_size_store(struct config_item *item,
1618 const char *page, size_t len)
1620 struct f_ncm_opts *opts = to_f_ncm_opts(item);
1624 mutex_lock(&opts->lock);
1630 ret = kstrtou16(page, 0, &segment_size);
1634 if (segment_size > MAX_DATAGRAM_SIZE) {
1639 opts->max_segment_size = segment_size;
1642 mutex_unlock(&opts->lock);
1646 CONFIGFS_ATTR(ncm_opts_, max_segment_size);
1648 static struct configfs_attribute *ncm_attrs[] = {
1649 &ncm_opts_attr_dev_addr,
1650 &ncm_opts_attr_host_addr,
1651 &ncm_opts_attr_qmult,
1652 &ncm_opts_attr_ifname,
1653 &ncm_opts_attr_max_segment_size,
1657 static const struct config_item_type ncm_func_type = {
1658 .ct_item_ops = &ncm_item_ops,
1659 .ct_attrs = ncm_attrs,
1660 .ct_owner = THIS_MODULE,
1663 static void ncm_free_inst(struct usb_function_instance *f)
1665 struct f_ncm_opts *opts;
1667 opts = container_of(f, struct f_ncm_opts, func_inst);
1669 gether_cleanup(netdev_priv(opts->net));
1671 free_netdev(opts->net);
1672 kfree(opts->ncm_interf_group);
1676 static struct usb_function_instance *ncm_alloc_inst(void)
1678 struct f_ncm_opts *opts;
1679 struct usb_os_desc *descs[1];
1681 struct config_group *ncm_interf_group;
1683 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1685 return ERR_PTR(-ENOMEM);
1686 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id;
1688 mutex_init(&opts->lock);
1689 opts->func_inst.free_func_inst = ncm_free_inst;
1690 opts->net = gether_setup_default();
1691 if (IS_ERR(opts->net)) {
1692 struct net_device *net = opts->net;
1694 return ERR_CAST(net);
1696 opts->max_segment_size = ETH_FRAME_LEN;
1697 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop);
1699 descs[0] = &opts->ncm_os_desc;
1702 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1704 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
1705 names, THIS_MODULE);
1706 if (IS_ERR(ncm_interf_group)) {
1707 ncm_free_inst(&opts->func_inst);
1708 return ERR_CAST(ncm_interf_group);
1710 opts->ncm_interf_group = ncm_interf_group;
1712 return &opts->func_inst;
1715 static void ncm_free(struct usb_function *f)
1718 struct f_ncm_opts *opts;
1720 ncm = func_to_ncm(f);
1721 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1723 mutex_lock(&opts->lock);
1725 mutex_unlock(&opts->lock);
1728 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1730 struct f_ncm *ncm = func_to_ncm(f);
1732 DBG(c->cdev, "ncm unbind\n");
1734 hrtimer_cancel(&ncm->task_timer);
1736 kfree(f->os_desc_table);
1739 ncm_string_defs[0].id = 0;
1740 usb_free_all_descriptors(f);
1742 if (atomic_read(&ncm->notify_count)) {
1743 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1744 atomic_set(&ncm->notify_count, 0);
1747 kfree(ncm->notify_req->buf);
1748 usb_ep_free_request(ncm->notify, ncm->notify_req);
1751 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1754 struct f_ncm_opts *opts;
1757 /* allocate and initialize one new instance */
1758 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1760 return ERR_PTR(-ENOMEM);
1762 opts = container_of(fi, struct f_ncm_opts, func_inst);
1763 mutex_lock(&opts->lock);
1766 /* export host's Ethernet address in CDC format */
1767 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1768 sizeof(ncm->ethaddr));
1769 if (status < 12) { /* strlen("01234567890a") */
1771 mutex_unlock(&opts->lock);
1772 return ERR_PTR(-EINVAL);
1774 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1776 spin_lock_init(&ncm->lock);
1777 ncm_reset_values(ncm);
1778 ncm->port.ioport = netdev_priv(opts->net);
1779 mutex_unlock(&opts->lock);
1780 ncm->port.is_fixed = true;
1781 ncm->port.supports_multi_frame = true;
1783 ncm->port.func.name = "cdc_network";
1784 /* descriptors are per-instance copies */
1785 ncm->port.func.bind = ncm_bind;
1786 ncm->port.func.unbind = ncm_unbind;
1787 ncm->port.func.set_alt = ncm_set_alt;
1788 ncm->port.func.get_alt = ncm_get_alt;
1789 ncm->port.func.setup = ncm_setup;
1790 ncm->port.func.disable = ncm_disable;
1791 ncm->port.func.free_func = ncm_free;
1793 ncm->port.wrap = ncm_wrap_ntb;
1794 ncm->port.unwrap = ncm_unwrap_ntb;
1796 return &ncm->port.func;
1799 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1800 MODULE_LICENSE("GPL");
1801 MODULE_AUTHOR("Yauheni Kaliuta");