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
106 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
107 USB_CDC_NCM_NTB32_SUPPORTED)
109 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
110 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
111 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
112 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
113 .wNdpInDivisor = cpu_to_le16(4),
114 .wNdpInPayloadRemainder = cpu_to_le16(0),
115 .wNdpInAlignment = cpu_to_le16(4),
117 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
118 .wNdpOutDivisor = cpu_to_le16(4),
119 .wNdpOutPayloadRemainder = cpu_to_le16(0),
120 .wNdpOutAlignment = cpu_to_le16(4),
124 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
125 * packet, to simplify cancellation; and a big transfer interval, to
126 * waste less bandwidth.
129 #define NCM_STATUS_INTERVAL_MS 32
130 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
132 static struct usb_interface_assoc_descriptor ncm_iad_desc = {
133 .bLength = sizeof ncm_iad_desc,
134 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
136 /* .bFirstInterface = DYNAMIC, */
137 .bInterfaceCount = 2, /* control + data */
138 .bFunctionClass = USB_CLASS_COMM,
139 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
140 .bFunctionProtocol = USB_CDC_PROTO_NONE,
141 /* .iFunction = DYNAMIC */
144 /* interface descriptor: */
146 static struct usb_interface_descriptor ncm_control_intf = {
147 .bLength = sizeof ncm_control_intf,
148 .bDescriptorType = USB_DT_INTERFACE,
150 /* .bInterfaceNumber = DYNAMIC */
152 .bInterfaceClass = USB_CLASS_COMM,
153 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
154 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
155 /* .iInterface = DYNAMIC */
158 static struct usb_cdc_header_desc ncm_header_desc = {
159 .bLength = sizeof ncm_header_desc,
160 .bDescriptorType = USB_DT_CS_INTERFACE,
161 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
163 .bcdCDC = cpu_to_le16(0x0110),
166 static struct usb_cdc_union_desc ncm_union_desc = {
167 .bLength = sizeof(ncm_union_desc),
168 .bDescriptorType = USB_DT_CS_INTERFACE,
169 .bDescriptorSubType = USB_CDC_UNION_TYPE,
170 /* .bMasterInterface0 = DYNAMIC */
171 /* .bSlaveInterface0 = DYNAMIC */
174 static struct usb_cdc_ether_desc ecm_desc = {
175 .bLength = sizeof ecm_desc,
176 .bDescriptorType = USB_DT_CS_INTERFACE,
177 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
179 /* this descriptor actually adds value, surprise! */
180 /* .iMACAddress = DYNAMIC */
181 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
182 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
183 .wNumberMCFilters = cpu_to_le16(0),
184 .bNumberPowerFilters = 0,
187 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
189 static struct usb_cdc_ncm_desc ncm_desc = {
190 .bLength = sizeof ncm_desc,
191 .bDescriptorType = USB_DT_CS_INTERFACE,
192 .bDescriptorSubType = USB_CDC_NCM_TYPE,
194 .bcdNcmVersion = cpu_to_le16(0x0100),
195 /* can process SetEthernetPacketFilter */
196 .bmNetworkCapabilities = NCAPS,
199 /* the default data interface has no endpoints ... */
201 static struct usb_interface_descriptor ncm_data_nop_intf = {
202 .bLength = sizeof ncm_data_nop_intf,
203 .bDescriptorType = USB_DT_INTERFACE,
205 .bInterfaceNumber = 1,
206 .bAlternateSetting = 0,
208 .bInterfaceClass = USB_CLASS_CDC_DATA,
209 .bInterfaceSubClass = 0,
210 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
211 /* .iInterface = DYNAMIC */
214 /* ... but the "real" data interface has two bulk endpoints */
216 static struct usb_interface_descriptor ncm_data_intf = {
217 .bLength = sizeof ncm_data_intf,
218 .bDescriptorType = USB_DT_INTERFACE,
220 .bInterfaceNumber = 1,
221 .bAlternateSetting = 1,
223 .bInterfaceClass = USB_CLASS_CDC_DATA,
224 .bInterfaceSubClass = 0,
225 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
226 /* .iInterface = DYNAMIC */
229 /* full speed support: */
231 static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
232 .bLength = USB_DT_ENDPOINT_SIZE,
233 .bDescriptorType = USB_DT_ENDPOINT,
235 .bEndpointAddress = USB_DIR_IN,
236 .bmAttributes = USB_ENDPOINT_XFER_INT,
237 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
238 .bInterval = NCM_STATUS_INTERVAL_MS,
241 static struct usb_endpoint_descriptor fs_ncm_in_desc = {
242 .bLength = USB_DT_ENDPOINT_SIZE,
243 .bDescriptorType = USB_DT_ENDPOINT,
245 .bEndpointAddress = USB_DIR_IN,
246 .bmAttributes = USB_ENDPOINT_XFER_BULK,
249 static struct usb_endpoint_descriptor fs_ncm_out_desc = {
250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
253 .bEndpointAddress = USB_DIR_OUT,
254 .bmAttributes = USB_ENDPOINT_XFER_BULK,
257 static struct usb_descriptor_header *ncm_fs_function[] = {
258 (struct usb_descriptor_header *) &ncm_iad_desc,
259 /* CDC NCM control descriptors */
260 (struct usb_descriptor_header *) &ncm_control_intf,
261 (struct usb_descriptor_header *) &ncm_header_desc,
262 (struct usb_descriptor_header *) &ncm_union_desc,
263 (struct usb_descriptor_header *) &ecm_desc,
264 (struct usb_descriptor_header *) &ncm_desc,
265 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
266 /* data interface, altsettings 0 and 1 */
267 (struct usb_descriptor_header *) &ncm_data_nop_intf,
268 (struct usb_descriptor_header *) &ncm_data_intf,
269 (struct usb_descriptor_header *) &fs_ncm_in_desc,
270 (struct usb_descriptor_header *) &fs_ncm_out_desc,
274 /* high speed support: */
276 static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
277 .bLength = USB_DT_ENDPOINT_SIZE,
278 .bDescriptorType = USB_DT_ENDPOINT,
280 .bEndpointAddress = USB_DIR_IN,
281 .bmAttributes = USB_ENDPOINT_XFER_INT,
282 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
283 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
285 static struct usb_endpoint_descriptor hs_ncm_in_desc = {
286 .bLength = USB_DT_ENDPOINT_SIZE,
287 .bDescriptorType = USB_DT_ENDPOINT,
289 .bEndpointAddress = USB_DIR_IN,
290 .bmAttributes = USB_ENDPOINT_XFER_BULK,
291 .wMaxPacketSize = cpu_to_le16(512),
294 static struct usb_endpoint_descriptor hs_ncm_out_desc = {
295 .bLength = USB_DT_ENDPOINT_SIZE,
296 .bDescriptorType = USB_DT_ENDPOINT,
298 .bEndpointAddress = USB_DIR_OUT,
299 .bmAttributes = USB_ENDPOINT_XFER_BULK,
300 .wMaxPacketSize = cpu_to_le16(512),
303 static struct usb_descriptor_header *ncm_hs_function[] = {
304 (struct usb_descriptor_header *) &ncm_iad_desc,
305 /* CDC NCM control descriptors */
306 (struct usb_descriptor_header *) &ncm_control_intf,
307 (struct usb_descriptor_header *) &ncm_header_desc,
308 (struct usb_descriptor_header *) &ncm_union_desc,
309 (struct usb_descriptor_header *) &ecm_desc,
310 (struct usb_descriptor_header *) &ncm_desc,
311 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
312 /* data interface, altsettings 0 and 1 */
313 (struct usb_descriptor_header *) &ncm_data_nop_intf,
314 (struct usb_descriptor_header *) &ncm_data_intf,
315 (struct usb_descriptor_header *) &hs_ncm_in_desc,
316 (struct usb_descriptor_header *) &hs_ncm_out_desc,
321 /* super speed support: */
323 static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
324 .bLength = USB_DT_ENDPOINT_SIZE,
325 .bDescriptorType = USB_DT_ENDPOINT,
327 .bEndpointAddress = USB_DIR_IN,
328 .bmAttributes = USB_ENDPOINT_XFER_INT,
329 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
330 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
333 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
334 .bLength = sizeof(ss_ncm_notify_comp_desc),
335 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
337 /* the following 3 values can be tweaked if necessary */
338 /* .bMaxBurst = 0, */
339 /* .bmAttributes = 0, */
340 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
343 static struct usb_endpoint_descriptor ss_ncm_in_desc = {
344 .bLength = USB_DT_ENDPOINT_SIZE,
345 .bDescriptorType = USB_DT_ENDPOINT,
347 .bEndpointAddress = USB_DIR_IN,
348 .bmAttributes = USB_ENDPOINT_XFER_BULK,
349 .wMaxPacketSize = cpu_to_le16(1024),
352 static struct usb_endpoint_descriptor ss_ncm_out_desc = {
353 .bLength = USB_DT_ENDPOINT_SIZE,
354 .bDescriptorType = USB_DT_ENDPOINT,
356 .bEndpointAddress = USB_DIR_OUT,
357 .bmAttributes = USB_ENDPOINT_XFER_BULK,
358 .wMaxPacketSize = cpu_to_le16(1024),
361 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
362 .bLength = sizeof(ss_ncm_bulk_comp_desc),
363 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
365 /* the following 2 values can be tweaked if necessary */
367 /* .bmAttributes = 0, */
370 static struct usb_descriptor_header *ncm_ss_function[] = {
371 (struct usb_descriptor_header *) &ncm_iad_desc,
372 /* CDC NCM control descriptors */
373 (struct usb_descriptor_header *) &ncm_control_intf,
374 (struct usb_descriptor_header *) &ncm_header_desc,
375 (struct usb_descriptor_header *) &ncm_union_desc,
376 (struct usb_descriptor_header *) &ecm_desc,
377 (struct usb_descriptor_header *) &ncm_desc,
378 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
379 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
380 /* data interface, altsettings 0 and 1 */
381 (struct usb_descriptor_header *) &ncm_data_nop_intf,
382 (struct usb_descriptor_header *) &ncm_data_intf,
383 (struct usb_descriptor_header *) &ss_ncm_in_desc,
384 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
385 (struct usb_descriptor_header *) &ss_ncm_out_desc,
386 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
390 /* string descriptors: */
392 #define STRING_CTRL_IDX 0
393 #define STRING_MAC_IDX 1
394 #define STRING_DATA_IDX 2
395 #define STRING_IAD_IDX 3
397 static struct usb_string ncm_string_defs[] = {
398 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
399 [STRING_MAC_IDX].s = "",
400 [STRING_DATA_IDX].s = "CDC Network Data",
401 [STRING_IAD_IDX].s = "CDC NCM",
402 { } /* end of list */
405 static struct usb_gadget_strings ncm_string_table = {
406 .language = 0x0409, /* en-us */
407 .strings = ncm_string_defs,
410 static struct usb_gadget_strings *ncm_strings[] = {
416 * Here are options for NCM Datagram Pointer table (NDP) parser.
417 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
418 * in NDP16 offsets and sizes fields are 1 16bit word wide,
419 * in NDP32 -- 2 16bit words wide. Also signatures are different.
420 * To make the parser code the same, put the differences in the structure,
421 * and switch pointers to the structures when the format is changed.
424 struct ndp_parser_opts {
430 unsigned ndplen_align;
431 /* sizes in u16 units */
432 unsigned dgram_item_len; /* index or length */
433 unsigned block_length;
437 unsigned next_ndp_index;
440 static const struct ndp_parser_opts ndp16_opts = {
441 .nth_sign = USB_CDC_NCM_NTH16_SIGN,
442 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN,
443 .nth_size = sizeof(struct usb_cdc_ncm_nth16),
444 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16),
445 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16),
455 static const struct ndp_parser_opts ndp32_opts = {
456 .nth_sign = USB_CDC_NCM_NTH32_SIGN,
457 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN,
458 .nth_size = sizeof(struct usb_cdc_ncm_nth32),
459 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32),
460 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32),
470 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
474 put_unaligned_le16((u16)val, *p);
477 put_unaligned_le32((u32)val, *p);
487 static inline unsigned get_ncm(__le16 **p, unsigned size)
493 tmp = get_unaligned_le16(*p);
496 tmp = get_unaligned_le32(*p);
506 /*-------------------------------------------------------------------------*/
508 static inline void ncm_reset_values(struct f_ncm *ncm)
510 ncm->parser_opts = &ndp16_opts;
512 ncm->ndp_sign = ncm->parser_opts->ndp_sign;
513 ncm->port.cdc_filter = DEFAULT_FILTER;
515 /* doesn't make sense for ncm, fixed size used */
516 ncm->port.header_len = 0;
518 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
519 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
523 * Context: ncm->lock held
525 static void ncm_do_notify(struct f_ncm *ncm)
527 struct usb_request *req = ncm->notify_req;
528 struct usb_cdc_notification *event;
529 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
533 /* notification already in flight? */
534 if (atomic_read(&ncm->notify_count))
538 switch (ncm->notify_state) {
539 case NCM_NOTIFY_NONE:
542 case NCM_NOTIFY_CONNECT:
543 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
545 event->wValue = cpu_to_le16(1);
547 event->wValue = cpu_to_le16(0);
549 req->length = sizeof *event;
551 DBG(cdev, "notify connect %s\n",
552 ncm->is_open ? "true" : "false");
553 ncm->notify_state = NCM_NOTIFY_NONE;
556 case NCM_NOTIFY_SPEED:
557 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
558 event->wValue = cpu_to_le16(0);
559 event->wLength = cpu_to_le16(8);
560 req->length = NCM_STATUS_BYTECOUNT;
562 /* SPEED_CHANGE data is up/down speeds in bits/sec */
563 data = req->buf + sizeof *event;
564 data[0] = cpu_to_le32(gether_bitrate(cdev->gadget));
567 DBG(cdev, "notify speed %u\n", gether_bitrate(cdev->gadget));
568 ncm->notify_state = NCM_NOTIFY_CONNECT;
571 event->bmRequestType = 0xA1;
572 event->wIndex = cpu_to_le16(ncm->ctrl_id);
574 atomic_inc(&ncm->notify_count);
577 * In double buffering if there is a space in FIFO,
578 * completion callback can be called right after the call,
581 spin_unlock(&ncm->lock);
582 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
583 spin_lock(&ncm->lock);
585 atomic_dec(&ncm->notify_count);
586 DBG(cdev, "notify --> %d\n", status);
591 * Context: ncm->lock held
593 static void ncm_notify(struct f_ncm *ncm)
596 * NOTE on most versions of Linux, host side cdc-ethernet
597 * won't listen for notifications until its netdevice opens.
598 * The first notification then sits in the FIFO for a long
599 * time, and the second one is queued.
601 * If ncm_notify() is called before the second (CONNECT)
602 * notification is sent, then it will reset to send the SPEED
603 * notificaion again (and again, and again), but it's not a problem
605 ncm->notify_state = NCM_NOTIFY_SPEED;
609 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
611 struct f_ncm *ncm = req->context;
612 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
613 struct usb_cdc_notification *event = req->buf;
615 spin_lock(&ncm->lock);
616 switch (req->status) {
618 VDBG(cdev, "Notification %02x sent\n",
619 event->bNotificationType);
620 atomic_dec(&ncm->notify_count);
624 atomic_set(&ncm->notify_count, 0);
625 ncm->notify_state = NCM_NOTIFY_NONE;
628 DBG(cdev, "event %02x --> %d\n",
629 event->bNotificationType, req->status);
630 atomic_dec(&ncm->notify_count);
634 spin_unlock(&ncm->lock);
637 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
639 /* now for SET_NTB_INPUT_SIZE only */
641 struct usb_function *f = req->context;
642 struct f_ncm *ncm = func_to_ncm(f);
643 struct usb_composite_dev *cdev = f->config->cdev;
646 if (req->status || req->actual != req->length) {
647 DBG(cdev, "Bad control-OUT transfer\n");
651 in_size = get_unaligned_le32(req->buf);
652 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
653 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
654 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
658 ncm->port.fixed_in_len = in_size;
659 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
667 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
669 struct f_ncm *ncm = func_to_ncm(f);
670 struct usb_composite_dev *cdev = f->config->cdev;
671 struct usb_request *req = cdev->req;
672 int value = -EOPNOTSUPP;
673 u16 w_index = le16_to_cpu(ctrl->wIndex);
674 u16 w_value = le16_to_cpu(ctrl->wValue);
675 u16 w_length = le16_to_cpu(ctrl->wLength);
678 * composite driver infrastructure handles everything except
679 * CDC class messages; interface activation uses set_alt().
681 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
682 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
683 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
685 * see 6.2.30: no data, wIndex = interface,
686 * wValue = packet filter bitmap
688 if (w_length != 0 || w_index != ncm->ctrl_id)
690 DBG(cdev, "packet filter %02x\n", w_value);
692 * REVISIT locking of cdc_filter. This assumes the UDC
693 * driver won't have a concurrent packet TX irq running on
694 * another CPU; or that if it does, this write is atomic...
696 ncm->port.cdc_filter = w_value;
701 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
702 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
703 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
704 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
705 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
706 * case USB_CDC_GET_ETHERNET_STATISTIC:
709 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
710 | USB_CDC_GET_NTB_PARAMETERS:
712 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
714 value = w_length > sizeof ntb_parameters ?
715 sizeof ntb_parameters : w_length;
716 memcpy(req->buf, &ntb_parameters, value);
717 VDBG(cdev, "Host asked NTB parameters\n");
720 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
721 | USB_CDC_GET_NTB_INPUT_SIZE:
723 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
725 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
727 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
728 ncm->port.fixed_in_len);
731 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
732 | USB_CDC_SET_NTB_INPUT_SIZE:
734 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
736 req->complete = ncm_ep0out_complete;
737 req->length = w_length;
744 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
745 | USB_CDC_GET_NTB_FORMAT:
749 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
751 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
752 put_unaligned_le16(format, req->buf);
754 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
758 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
759 | USB_CDC_SET_NTB_FORMAT:
761 if (w_length != 0 || w_index != ncm->ctrl_id)
765 ncm->parser_opts = &ndp16_opts;
766 DBG(cdev, "NCM16 selected\n");
769 ncm->parser_opts = &ndp32_opts;
770 DBG(cdev, "NCM32 selected\n");
778 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
779 | USB_CDC_GET_CRC_MODE:
783 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
785 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
786 put_unaligned_le16(is_crc, req->buf);
788 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
792 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
793 | USB_CDC_SET_CRC_MODE:
795 if (w_length != 0 || w_index != ncm->ctrl_id)
800 DBG(cdev, "non-CRC mode selected\n");
804 DBG(cdev, "CRC mode selected\n");
813 /* and disabled in ncm descriptor: */
814 /* case USB_CDC_GET_NET_ADDRESS: */
815 /* case USB_CDC_SET_NET_ADDRESS: */
816 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
817 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
821 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
822 ctrl->bRequestType, ctrl->bRequest,
823 w_value, w_index, w_length);
825 ncm->ndp_sign = ncm->parser_opts->ndp_sign |
826 (ncm->is_crc ? NCM_NDP_HDR_CRC : 0);
828 /* respond with data transfer or status phase? */
830 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
831 ctrl->bRequestType, ctrl->bRequest,
832 w_value, w_index, w_length);
835 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
837 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
838 ctrl->bRequestType, ctrl->bRequest,
842 /* device either stalls (value < 0) or reports success */
847 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
849 struct f_ncm *ncm = func_to_ncm(f);
850 struct usb_composite_dev *cdev = f->config->cdev;
852 /* Control interface has only altsetting 0 */
853 if (intf == ncm->ctrl_id) {
857 DBG(cdev, "reset ncm control %d\n", intf);
858 usb_ep_disable(ncm->notify);
860 if (!(ncm->notify->desc)) {
861 DBG(cdev, "init ncm ctrl %d\n", intf);
862 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
865 usb_ep_enable(ncm->notify);
867 /* Data interface has two altsettings, 0 and 1 */
868 } else if (intf == ncm->data_id) {
872 if (ncm->port.in_ep->enabled) {
873 DBG(cdev, "reset ncm\n");
875 gether_disconnect(&ncm->port);
876 ncm_reset_values(ncm);
880 * CDC Network only sends data in non-default altsettings.
881 * Changing altsettings resets filters, statistics, etc.
884 struct net_device *net;
886 if (!ncm->port.in_ep->desc ||
887 !ncm->port.out_ep->desc) {
888 DBG(cdev, "init ncm\n");
889 if (config_ep_by_speed(cdev->gadget, f,
891 config_ep_by_speed(cdev->gadget, f,
893 ncm->port.in_ep->desc = NULL;
894 ncm->port.out_ep->desc = NULL;
900 /* Enable zlps by default for NCM conformance;
901 * override for musb_hdrc (avoids txdma ovhead)
903 ncm->port.is_zlp_ok =
904 gadget_is_zlp_supported(cdev->gadget);
905 ncm->port.cdc_filter = DEFAULT_FILTER;
906 DBG(cdev, "activate ncm\n");
907 net = gether_connect(&ncm->port);
913 spin_lock(&ncm->lock);
915 spin_unlock(&ncm->lock);
925 * Because the data interface supports multiple altsettings,
926 * this NCM function *MUST* implement a get_alt() method.
928 static int ncm_get_alt(struct usb_function *f, unsigned intf)
930 struct f_ncm *ncm = func_to_ncm(f);
932 if (intf == ncm->ctrl_id)
934 return ncm->port.in_ep->enabled ? 1 : 0;
937 static struct sk_buff *package_for_tx(struct f_ncm *ncm)
940 struct sk_buff *skb2 = NULL;
945 const struct ndp_parser_opts *opts = ncm->parser_opts;
946 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
947 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
950 hrtimer_try_to_cancel(&ncm->task_timer);
952 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
953 ncm->skb_tx_data->len;
954 ndp_index = ncm->skb_tx_data->len + ndp_pad;
955 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
957 /* Set the final BlockLength and wNdpIndex */
958 ntb_iter = (void *) ncm->skb_tx_data->data;
959 /* Increment pointer to BlockLength */
960 ntb_iter += 2 + 1 + 1;
961 put_ncm(&ntb_iter, opts->block_length, new_len);
962 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
964 /* Set the final NDP wLength */
965 new_len = opts->ndp_size +
966 (ncm->ndp_dgram_count * dgram_idx_len);
967 ncm->ndp_dgram_count = 0;
968 /* Increment from start to wLength */
969 ntb_iter = (void *) ncm->skb_tx_ndp->data;
971 put_unaligned_le16(new_len, ntb_iter);
974 swap(skb2, ncm->skb_tx_data);
975 if (ncm->skb_tx_data) {
976 dev_consume_skb_any(ncm->skb_tx_data);
977 ncm->skb_tx_data = NULL;
980 /* Insert NDP alignment. */
981 skb_put_zero(skb2, ndp_pad);
983 /* Copy NTB across. */
984 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
985 dev_consume_skb_any(ncm->skb_tx_ndp);
986 ncm->skb_tx_ndp = NULL;
988 /* Insert zero'd datagram. */
989 skb_put_zero(skb2, dgram_idx_len);
994 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
997 struct f_ncm *ncm = func_to_ncm(&port->func);
998 struct sk_buff *skb2 = NULL;
1006 unsigned max_size = ncm->port.fixed_in_len;
1007 const struct ndp_parser_opts *opts = ncm->parser_opts;
1008 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1009 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1010 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1011 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
1013 /* Add the CRC if required up front */
1021 crc_pos = skb_put(skb, sizeof(uint32_t));
1022 put_unaligned_le32(crc, crc_pos);
1025 /* If the new skb is too big for the current NCM NTB then
1026 * set the current stored skb to be sent now and clear it
1027 * ready for new data.
1028 * NOTE: Assume maximum align for speed of calculation.
1030 if (ncm->skb_tx_data
1031 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1032 || (ncm->skb_tx_data->len +
1033 div + rem + skb->len +
1034 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1036 skb2 = package_for_tx(ncm);
1041 if (!ncm->skb_tx_data) {
1042 ncb_len = opts->nth_size;
1043 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1044 ncb_len += dgram_pad;
1046 /* Create a new skb for the NTH and datagrams. */
1047 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1048 if (!ncm->skb_tx_data)
1051 ncm->skb_tx_data->dev = ncm->netdev;
1052 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len);
1054 put_unaligned_le32(opts->nth_sign, ntb_data);
1057 put_unaligned_le16(opts->nth_size, ntb_data++);
1059 /* Allocate an skb for storing the NDP,
1060 * TX_MAX_NUM_DPE should easily suffice for a
1063 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1067 if (!ncm->skb_tx_ndp)
1070 ncm->skb_tx_ndp->dev = ncm->netdev;
1071 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size);
1072 memset(ntb_ndp, 0, ncb_len);
1074 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1077 /* There is always a zeroed entry */
1078 ncm->ndp_dgram_count = 1;
1080 /* Note: we skip opts->next_ndp_index */
1082 /* Start the timer. */
1083 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS,
1084 HRTIMER_MODE_REL_SOFT);
1087 /* Add the datagram position entries */
1088 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len);
1090 ncb_len = ncm->skb_tx_data->len;
1091 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1092 ncb_len += dgram_pad;
1094 /* (d)wDatagramIndex */
1095 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1096 /* (d)wDatagramLength */
1097 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1098 ncm->ndp_dgram_count++;
1100 /* Add the new data to the skb */
1101 skb_put_zero(ncm->skb_tx_data, dgram_pad);
1102 skb_put_data(ncm->skb_tx_data, skb->data, skb->len);
1103 dev_consume_skb_any(skb);
1106 } else if (ncm->skb_tx_data) {
1107 /* If we get here ncm_wrap_ntb() was called with NULL skb,
1108 * because eth_start_xmit() was called with NULL skb by
1109 * ncm_tx_timeout() - hence, this is our signal to flush/send.
1111 skb2 = package_for_tx(ncm);
1119 ncm->netdev->stats.tx_dropped++;
1122 dev_kfree_skb_any(skb);
1123 if (ncm->skb_tx_data)
1124 dev_kfree_skb_any(ncm->skb_tx_data);
1125 if (ncm->skb_tx_ndp)
1126 dev_kfree_skb_any(ncm->skb_tx_ndp);
1132 * The transmit should only be run if no skb data has been sent
1133 * for a certain duration.
1135 static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1137 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1138 struct net_device *netdev = READ_ONCE(ncm->netdev);
1141 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1142 * XXX is not sane. The gadget layer should be redesigned so
1143 * XXX that the dev->wrap() invocations to build SKBs is transparent
1144 * XXX and performed in some way outside of the ndo_start_xmit
1147 * This will call directly into u_ether's eth_start_xmit()
1149 netdev->netdev_ops->ndo_start_xmit(NULL, netdev);
1151 return HRTIMER_NORESTART;
1154 static int ncm_unwrap_ntb(struct gether *port,
1155 struct sk_buff *skb,
1156 struct sk_buff_head *list)
1158 struct f_ncm *ncm = func_to_ncm(&port->func);
1159 __le16 *tmp = (void *) skb->data;
1160 unsigned index, index2;
1162 unsigned dg_len, dg_len2;
1165 struct sk_buff *skb2;
1167 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1168 unsigned frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
1169 const struct ndp_parser_opts *opts = ncm->parser_opts;
1170 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1174 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1175 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1177 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1178 skb->data, 32, false);
1184 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1185 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1188 tmp++; /* skip wSequence */
1190 block_len = get_ncm(&tmp, opts->block_length);
1191 /* (d)wBlockLength */
1192 if (block_len > ntb_max) {
1193 INFO(port->func.config->cdev, "OUT size exceeded\n");
1197 ndp_index = get_ncm(&tmp, opts->ndp_index);
1199 /* Run through all the NDP's in the NTB */
1205 if (((ndp_index % 4) != 0) ||
1206 (ndp_index < opts->nth_size) ||
1207 (ndp_index > (block_len -
1209 INFO(port->func.config->cdev, "Bad index: %#X\n",
1218 tmp = (void *)(skb->data + ndp_index);
1219 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1220 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1225 ndp_len = get_unaligned_le16(tmp++);
1230 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1231 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1232 * Each entry is a dgram index and a dgram length.
1234 if ((ndp_len < opts->ndp_size
1235 + 2 * 2 * (opts->dgram_item_len * 2)) ||
1236 (ndp_len % opts->ndplen_align != 0)) {
1237 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1241 tmp += opts->reserved1;
1242 /* Check for another NDP (d)wNextNdpIndex */
1243 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
1244 tmp += opts->reserved2;
1246 ndp_len -= opts->ndp_size;
1247 index2 = get_ncm(&tmp, opts->dgram_item_len);
1248 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1253 /* wDatagramIndex[0] */
1254 if ((index < opts->nth_size) ||
1255 (index > block_len - opts->dpe_size)) {
1256 INFO(port->func.config->cdev,
1257 "Bad index: %#X\n", index);
1263 * wDatagramLength[0]
1264 * ethernet hdr + crc or larger than max frame size
1266 if ((dg_len < 14 + crc_len) ||
1267 (dg_len > frame_max)) {
1268 INFO(port->func.config->cdev,
1269 "Bad dgram length: %#X\n", dg_len);
1275 crc = get_unaligned_le32(skb->data +
1278 crc2 = ~crc32_le(~0,
1282 INFO(port->func.config->cdev,
1288 index2 = get_ncm(&tmp, opts->dgram_item_len);
1289 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1291 /* wDatagramIndex[1] */
1292 if (index2 > block_len - opts->dpe_size) {
1293 INFO(port->func.config->cdev,
1294 "Bad index: %#X\n", index2);
1299 * Copy the data into a new skb.
1300 * This ensures the truesize is correct
1302 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1306 skb_put_data(skb2, skb->data + index,
1309 skb_queue_tail(list, skb2);
1311 ndp_len -= 2 * (opts->dgram_item_len * 2);
1314 if (index2 == 0 || dg_len2 == 0)
1316 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1317 } while (ndp_index);
1319 dev_consume_skb_any(skb);
1321 VDBG(port->func.config->cdev,
1322 "Parsed NTB with %d frames\n", dgram_counter);
1325 skb_queue_purge(list);
1326 dev_kfree_skb_any(skb);
1330 static void ncm_disable(struct usb_function *f)
1332 struct f_ncm *ncm = func_to_ncm(f);
1333 struct usb_composite_dev *cdev = f->config->cdev;
1335 DBG(cdev, "ncm deactivated\n");
1337 if (ncm->port.in_ep->enabled) {
1339 gether_disconnect(&ncm->port);
1342 if (ncm->notify->enabled) {
1343 usb_ep_disable(ncm->notify);
1344 ncm->notify->desc = NULL;
1348 /*-------------------------------------------------------------------------*/
1351 * Callbacks let us notify the host about connect/disconnect when the
1352 * net device is opened or closed.
1354 * For testing, note that link states on this side include both opened
1355 * and closed variants of:
1357 * - disconnected/unconfigured
1358 * - configured but inactive (data alt 0)
1359 * - configured and active (data alt 1)
1361 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1362 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1363 * imply the host is actually polling the notification endpoint, and
1364 * likewise that "active" doesn't imply it's actually using the data
1365 * endpoints for traffic.
1368 static void ncm_open(struct gether *geth)
1370 struct f_ncm *ncm = func_to_ncm(&geth->func);
1372 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1374 spin_lock(&ncm->lock);
1375 ncm->is_open = true;
1377 spin_unlock(&ncm->lock);
1380 static void ncm_close(struct gether *geth)
1382 struct f_ncm *ncm = func_to_ncm(&geth->func);
1384 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1386 spin_lock(&ncm->lock);
1387 ncm->is_open = false;
1389 spin_unlock(&ncm->lock);
1392 /*-------------------------------------------------------------------------*/
1394 /* ethernet function driver setup/binding */
1396 static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1398 struct usb_composite_dev *cdev = c->cdev;
1399 struct f_ncm *ncm = func_to_ncm(f);
1400 struct usb_string *us;
1403 struct f_ncm_opts *ncm_opts;
1405 if (!can_support_ecm(cdev->gadget))
1408 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1410 if (cdev->use_os_string) {
1411 f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
1413 if (!f->os_desc_table)
1416 f->os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc;
1420 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1421 * configurations are bound in sequence with list_for_each_entry,
1422 * in each configuration its functions are bound in sequence
1423 * with list_for_each_entry, so we assume no race condition
1424 * with regard to ncm_opts->bound access
1426 if (!ncm_opts->bound) {
1427 mutex_lock(&ncm_opts->lock);
1428 gether_set_gadget(ncm_opts->net, cdev->gadget);
1429 status = gether_register_netdev(ncm_opts->net);
1430 mutex_unlock(&ncm_opts->lock);
1433 ncm_opts->bound = true;
1435 us = usb_gstrings_attach(cdev, ncm_strings,
1436 ARRAY_SIZE(ncm_string_defs));
1438 status = PTR_ERR(us);
1441 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1442 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1443 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1444 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1445 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1447 /* allocate instance-specific interface IDs */
1448 status = usb_interface_id(c, f);
1451 ncm->ctrl_id = status;
1452 ncm_iad_desc.bFirstInterface = status;
1454 ncm_control_intf.bInterfaceNumber = status;
1455 ncm_union_desc.bMasterInterface0 = status;
1457 if (cdev->use_os_string)
1458 f->os_desc_table[0].if_id =
1459 ncm_iad_desc.bFirstInterface;
1461 status = usb_interface_id(c, f);
1464 ncm->data_id = status;
1466 ncm_data_nop_intf.bInterfaceNumber = status;
1467 ncm_data_intf.bInterfaceNumber = status;
1468 ncm_union_desc.bSlaveInterface0 = status;
1472 /* allocate instance-specific endpoints */
1473 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1476 ncm->port.in_ep = ep;
1478 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1481 ncm->port.out_ep = ep;
1483 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1490 /* allocate notification request and buffer */
1491 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1492 if (!ncm->notify_req)
1494 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1495 if (!ncm->notify_req->buf)
1497 ncm->notify_req->context = ncm;
1498 ncm->notify_req->complete = ncm_notify_complete;
1501 * support all relevant hardware speeds... we expect that when
1502 * hardware is dual speed, all bulk-capable endpoints work at
1505 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1506 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1507 hs_ncm_notify_desc.bEndpointAddress =
1508 fs_ncm_notify_desc.bEndpointAddress;
1510 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1511 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1512 ss_ncm_notify_desc.bEndpointAddress =
1513 fs_ncm_notify_desc.bEndpointAddress;
1515 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1516 ncm_ss_function, ncm_ss_function);
1521 * NOTE: all that is done without knowing or caring about
1522 * the network link ... which is unavailable to this code
1523 * until we're activated via set_alt().
1526 ncm->port.open = ncm_open;
1527 ncm->port.close = ncm_close;
1529 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1530 ncm->task_timer.function = ncm_tx_timeout;
1532 DBG(cdev, "CDC Network: IN/%s OUT/%s NOTIFY/%s\n",
1533 ncm->port.in_ep->name, ncm->port.out_ep->name,
1538 kfree(f->os_desc_table);
1541 if (ncm->notify_req) {
1542 kfree(ncm->notify_req->buf);
1543 usb_ep_free_request(ncm->notify, ncm->notify_req);
1546 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1551 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1553 return container_of(to_config_group(item), struct f_ncm_opts,
1557 /* f_ncm_item_ops */
1558 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1560 /* f_ncm_opts_dev_addr */
1561 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1563 /* f_ncm_opts_host_addr */
1564 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1566 /* f_ncm_opts_qmult */
1567 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1569 /* f_ncm_opts_ifname */
1570 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1572 static struct configfs_attribute *ncm_attrs[] = {
1573 &ncm_opts_attr_dev_addr,
1574 &ncm_opts_attr_host_addr,
1575 &ncm_opts_attr_qmult,
1576 &ncm_opts_attr_ifname,
1580 static const struct config_item_type ncm_func_type = {
1581 .ct_item_ops = &ncm_item_ops,
1582 .ct_attrs = ncm_attrs,
1583 .ct_owner = THIS_MODULE,
1586 static void ncm_free_inst(struct usb_function_instance *f)
1588 struct f_ncm_opts *opts;
1590 opts = container_of(f, struct f_ncm_opts, func_inst);
1592 gether_cleanup(netdev_priv(opts->net));
1594 free_netdev(opts->net);
1595 kfree(opts->ncm_interf_group);
1599 static struct usb_function_instance *ncm_alloc_inst(void)
1601 struct f_ncm_opts *opts;
1602 struct usb_os_desc *descs[1];
1604 struct config_group *ncm_interf_group;
1606 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1608 return ERR_PTR(-ENOMEM);
1609 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id;
1611 mutex_init(&opts->lock);
1612 opts->func_inst.free_func_inst = ncm_free_inst;
1613 opts->net = gether_setup_default();
1614 if (IS_ERR(opts->net)) {
1615 struct net_device *net = opts->net;
1617 return ERR_CAST(net);
1619 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop);
1621 descs[0] = &opts->ncm_os_desc;
1624 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1626 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
1627 names, THIS_MODULE);
1628 if (IS_ERR(ncm_interf_group)) {
1629 ncm_free_inst(&opts->func_inst);
1630 return ERR_CAST(ncm_interf_group);
1632 opts->ncm_interf_group = ncm_interf_group;
1634 return &opts->func_inst;
1637 static void ncm_free(struct usb_function *f)
1640 struct f_ncm_opts *opts;
1642 ncm = func_to_ncm(f);
1643 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1645 mutex_lock(&opts->lock);
1647 mutex_unlock(&opts->lock);
1650 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1652 struct f_ncm *ncm = func_to_ncm(f);
1654 DBG(c->cdev, "ncm unbind\n");
1656 hrtimer_cancel(&ncm->task_timer);
1658 kfree(f->os_desc_table);
1661 ncm_string_defs[0].id = 0;
1662 usb_free_all_descriptors(f);
1664 if (atomic_read(&ncm->notify_count)) {
1665 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1666 atomic_set(&ncm->notify_count, 0);
1669 kfree(ncm->notify_req->buf);
1670 usb_ep_free_request(ncm->notify, ncm->notify_req);
1673 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1676 struct f_ncm_opts *opts;
1679 /* allocate and initialize one new instance */
1680 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1682 return ERR_PTR(-ENOMEM);
1684 opts = container_of(fi, struct f_ncm_opts, func_inst);
1685 mutex_lock(&opts->lock);
1688 /* export host's Ethernet address in CDC format */
1689 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1690 sizeof(ncm->ethaddr));
1691 if (status < 12) { /* strlen("01234567890a") */
1693 mutex_unlock(&opts->lock);
1694 return ERR_PTR(-EINVAL);
1696 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1698 spin_lock_init(&ncm->lock);
1699 ncm_reset_values(ncm);
1700 ncm->port.ioport = netdev_priv(opts->net);
1701 mutex_unlock(&opts->lock);
1702 ncm->port.is_fixed = true;
1703 ncm->port.supports_multi_frame = true;
1705 ncm->port.func.name = "cdc_network";
1706 /* descriptors are per-instance copies */
1707 ncm->port.func.bind = ncm_bind;
1708 ncm->port.func.unbind = ncm_unbind;
1709 ncm->port.func.set_alt = ncm_set_alt;
1710 ncm->port.func.get_alt = ncm_get_alt;
1711 ncm->port.func.setup = ncm_setup;
1712 ncm->port.func.disable = ncm_disable;
1713 ncm->port.func.free_func = ncm_free;
1715 ncm->port.wrap = ncm_wrap_ntb;
1716 ncm->port.unwrap = ncm_unwrap_ntb;
1718 return &ncm->port.func;
1721 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1722 MODULE_LICENSE("GPL");
1723 MODULE_AUTHOR("Yauheni Kaliuta");