2 * f_ncm.c -- USB CDC Network (NCM) link function driver
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
7 * The driver borrows from f_ecm.c which is:
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2008 Nokia Corporation
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/etherdevice.h>
22 #include <linux/crc32.h>
24 #include <linux/usb/cdc.h>
27 #include "u_ether_configfs.h"
31 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
32 * NCM is intended to be used with high-speed network attachments.
34 * Note that NCM requires the use of "alternate settings" for its data
35 * interface. This means that the set_alt() method has real work to do,
36 * and also means that a get_alt() method is required.
39 /* to trigger crc/non-crc ndp signature */
41 #define NCM_NDP_HDR_CRC_MASK 0x01000000
42 #define NCM_NDP_HDR_CRC 0x01000000
43 #define NCM_NDP_HDR_NOCRC 0x00000000
45 enum ncm_notify_state {
46 NCM_NOTIFY_NONE, /* don't notify */
47 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
48 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
57 struct usb_ep *notify;
58 struct usb_request *notify_req;
60 atomic_t notify_count;
63 const struct ndp_parser_opts *parser_opts;
68 * for notification, it is accessed from both
69 * callback and ethernet open/close
73 struct net_device *netdev;
75 /* For multi-frame NDP TX */
76 struct sk_buff *skb_tx_data;
77 struct sk_buff *skb_tx_ndp;
80 struct tasklet_struct tx_tasklet;
81 struct hrtimer task_timer;
86 static inline struct f_ncm *func_to_ncm(struct usb_function *f)
88 return container_of(f, struct f_ncm, port.func);
91 /* peak (theoretical) bulk transfer rate in bits-per-second */
92 static inline unsigned ncm_bitrate(struct usb_gadget *g)
94 if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER_PLUS)
96 else if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
98 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
99 return 13 * 512 * 8 * 1000 * 8;
101 return 19 * 64 * 1 * 1000 * 8;
104 /*-------------------------------------------------------------------------*/
107 * We cannot group frames so use just the minimal size which ok to put
108 * one max-size ethernet frame.
109 * If the host can group frames, allow it to do that, 16K is selected,
110 * because it's used by default by the current linux host driver
112 #define NTB_DEFAULT_IN_SIZE 16384
113 #define NTB_OUT_SIZE 16384
115 /* Allocation for storing the NDP, 32 should suffice for a
116 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
117 * be transmitted in a single 16kB skb, though when sending full size
118 * packets this limit will be plenty.
119 * Smaller packets are not likely to be trying to maximize the
120 * throughput and will be mstly sending smaller infrequent frames.
122 #define TX_MAX_NUM_DPE 32
124 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
125 #define TX_TIMEOUT_NSECS 300000
127 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
128 USB_CDC_NCM_NTB32_SUPPORTED)
130 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
131 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
132 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
133 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
134 .wNdpInDivisor = cpu_to_le16(4),
135 .wNdpInPayloadRemainder = cpu_to_le16(0),
136 .wNdpInAlignment = cpu_to_le16(4),
138 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
139 .wNdpOutDivisor = cpu_to_le16(4),
140 .wNdpOutPayloadRemainder = cpu_to_le16(0),
141 .wNdpOutAlignment = cpu_to_le16(4),
145 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
146 * packet, to simplify cancellation; and a big transfer interval, to
147 * waste less bandwidth.
150 #define NCM_STATUS_INTERVAL_MS 32
151 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
153 static struct usb_interface_assoc_descriptor ncm_iad_desc = {
154 .bLength = sizeof ncm_iad_desc,
155 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
157 /* .bFirstInterface = DYNAMIC, */
158 .bInterfaceCount = 2, /* control + data */
159 .bFunctionClass = USB_CLASS_COMM,
160 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
161 .bFunctionProtocol = USB_CDC_PROTO_NONE,
162 /* .iFunction = DYNAMIC */
165 /* interface descriptor: */
167 static struct usb_interface_descriptor ncm_control_intf = {
168 .bLength = sizeof ncm_control_intf,
169 .bDescriptorType = USB_DT_INTERFACE,
171 /* .bInterfaceNumber = DYNAMIC */
173 .bInterfaceClass = USB_CLASS_COMM,
174 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
175 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
176 /* .iInterface = DYNAMIC */
179 static struct usb_cdc_header_desc ncm_header_desc = {
180 .bLength = sizeof ncm_header_desc,
181 .bDescriptorType = USB_DT_CS_INTERFACE,
182 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
184 .bcdCDC = cpu_to_le16(0x0110),
187 static struct usb_cdc_union_desc ncm_union_desc = {
188 .bLength = sizeof(ncm_union_desc),
189 .bDescriptorType = USB_DT_CS_INTERFACE,
190 .bDescriptorSubType = USB_CDC_UNION_TYPE,
191 /* .bMasterInterface0 = DYNAMIC */
192 /* .bSlaveInterface0 = DYNAMIC */
195 static struct usb_cdc_ether_desc ecm_desc = {
196 .bLength = sizeof ecm_desc,
197 .bDescriptorType = USB_DT_CS_INTERFACE,
198 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
200 /* this descriptor actually adds value, surprise! */
201 /* .iMACAddress = DYNAMIC */
202 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
203 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
204 .wNumberMCFilters = cpu_to_le16(0),
205 .bNumberPowerFilters = 0,
208 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
210 static struct usb_cdc_ncm_desc ncm_desc = {
211 .bLength = sizeof ncm_desc,
212 .bDescriptorType = USB_DT_CS_INTERFACE,
213 .bDescriptorSubType = USB_CDC_NCM_TYPE,
215 .bcdNcmVersion = cpu_to_le16(0x0100),
216 /* can process SetEthernetPacketFilter */
217 .bmNetworkCapabilities = NCAPS,
220 /* the default data interface has no endpoints ... */
222 static struct usb_interface_descriptor ncm_data_nop_intf = {
223 .bLength = sizeof ncm_data_nop_intf,
224 .bDescriptorType = USB_DT_INTERFACE,
226 .bInterfaceNumber = 1,
227 .bAlternateSetting = 0,
229 .bInterfaceClass = USB_CLASS_CDC_DATA,
230 .bInterfaceSubClass = 0,
231 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
232 /* .iInterface = DYNAMIC */
235 /* ... but the "real" data interface has two bulk endpoints */
237 static struct usb_interface_descriptor ncm_data_intf = {
238 .bLength = sizeof ncm_data_intf,
239 .bDescriptorType = USB_DT_INTERFACE,
241 .bInterfaceNumber = 1,
242 .bAlternateSetting = 1,
244 .bInterfaceClass = USB_CLASS_CDC_DATA,
245 .bInterfaceSubClass = 0,
246 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
247 /* .iInterface = DYNAMIC */
250 /* full speed support: */
252 static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
253 .bLength = USB_DT_ENDPOINT_SIZE,
254 .bDescriptorType = USB_DT_ENDPOINT,
256 .bEndpointAddress = USB_DIR_IN,
257 .bmAttributes = USB_ENDPOINT_XFER_INT,
258 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
259 .bInterval = NCM_STATUS_INTERVAL_MS,
262 static struct usb_endpoint_descriptor fs_ncm_in_desc = {
263 .bLength = USB_DT_ENDPOINT_SIZE,
264 .bDescriptorType = USB_DT_ENDPOINT,
266 .bEndpointAddress = USB_DIR_IN,
267 .bmAttributes = USB_ENDPOINT_XFER_BULK,
270 static struct usb_endpoint_descriptor fs_ncm_out_desc = {
271 .bLength = USB_DT_ENDPOINT_SIZE,
272 .bDescriptorType = USB_DT_ENDPOINT,
274 .bEndpointAddress = USB_DIR_OUT,
275 .bmAttributes = USB_ENDPOINT_XFER_BULK,
278 static struct usb_descriptor_header *ncm_fs_function[] = {
279 (struct usb_descriptor_header *) &ncm_iad_desc,
280 /* CDC NCM control descriptors */
281 (struct usb_descriptor_header *) &ncm_control_intf,
282 (struct usb_descriptor_header *) &ncm_header_desc,
283 (struct usb_descriptor_header *) &ncm_union_desc,
284 (struct usb_descriptor_header *) &ecm_desc,
285 (struct usb_descriptor_header *) &ncm_desc,
286 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
287 /* data interface, altsettings 0 and 1 */
288 (struct usb_descriptor_header *) &ncm_data_nop_intf,
289 (struct usb_descriptor_header *) &ncm_data_intf,
290 (struct usb_descriptor_header *) &fs_ncm_in_desc,
291 (struct usb_descriptor_header *) &fs_ncm_out_desc,
295 /* high speed support: */
297 static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
298 .bLength = USB_DT_ENDPOINT_SIZE,
299 .bDescriptorType = USB_DT_ENDPOINT,
301 .bEndpointAddress = USB_DIR_IN,
302 .bmAttributes = USB_ENDPOINT_XFER_INT,
303 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
304 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
306 static struct usb_endpoint_descriptor hs_ncm_in_desc = {
307 .bLength = USB_DT_ENDPOINT_SIZE,
308 .bDescriptorType = USB_DT_ENDPOINT,
310 .bEndpointAddress = USB_DIR_IN,
311 .bmAttributes = USB_ENDPOINT_XFER_BULK,
312 .wMaxPacketSize = cpu_to_le16(512),
315 static struct usb_endpoint_descriptor hs_ncm_out_desc = {
316 .bLength = USB_DT_ENDPOINT_SIZE,
317 .bDescriptorType = USB_DT_ENDPOINT,
319 .bEndpointAddress = USB_DIR_OUT,
320 .bmAttributes = USB_ENDPOINT_XFER_BULK,
321 .wMaxPacketSize = cpu_to_le16(512),
324 static struct usb_descriptor_header *ncm_hs_function[] = {
325 (struct usb_descriptor_header *) &ncm_iad_desc,
326 /* CDC NCM control descriptors */
327 (struct usb_descriptor_header *) &ncm_control_intf,
328 (struct usb_descriptor_header *) &ncm_header_desc,
329 (struct usb_descriptor_header *) &ncm_union_desc,
330 (struct usb_descriptor_header *) &ecm_desc,
331 (struct usb_descriptor_header *) &ncm_desc,
332 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
333 /* data interface, altsettings 0 and 1 */
334 (struct usb_descriptor_header *) &ncm_data_nop_intf,
335 (struct usb_descriptor_header *) &ncm_data_intf,
336 (struct usb_descriptor_header *) &hs_ncm_in_desc,
337 (struct usb_descriptor_header *) &hs_ncm_out_desc,
342 /* super speed support: */
344 static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
345 .bLength = USB_DT_ENDPOINT_SIZE,
346 .bDescriptorType = USB_DT_ENDPOINT,
348 .bEndpointAddress = USB_DIR_IN,
349 .bmAttributes = USB_ENDPOINT_XFER_INT,
350 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
351 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
354 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
355 .bLength = sizeof(ss_ncm_notify_comp_desc),
356 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
358 /* the following 3 values can be tweaked if necessary */
359 /* .bMaxBurst = 0, */
360 /* .bmAttributes = 0, */
361 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
364 static struct usb_endpoint_descriptor ss_ncm_in_desc = {
365 .bLength = USB_DT_ENDPOINT_SIZE,
366 .bDescriptorType = USB_DT_ENDPOINT,
368 .bEndpointAddress = USB_DIR_IN,
369 .bmAttributes = USB_ENDPOINT_XFER_BULK,
370 .wMaxPacketSize = cpu_to_le16(1024),
373 static struct usb_endpoint_descriptor ss_ncm_out_desc = {
374 .bLength = USB_DT_ENDPOINT_SIZE,
375 .bDescriptorType = USB_DT_ENDPOINT,
377 .bEndpointAddress = USB_DIR_OUT,
378 .bmAttributes = USB_ENDPOINT_XFER_BULK,
379 .wMaxPacketSize = cpu_to_le16(1024),
382 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
383 .bLength = sizeof(ss_ncm_bulk_comp_desc),
384 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
386 /* the following 2 values can be tweaked if necessary */
387 /* .bMaxBurst = 0, */
388 /* .bmAttributes = 0, */
391 static struct usb_descriptor_header *ncm_ss_function[] = {
392 (struct usb_descriptor_header *) &ncm_iad_desc,
393 /* CDC NCM control descriptors */
394 (struct usb_descriptor_header *) &ncm_control_intf,
395 (struct usb_descriptor_header *) &ncm_header_desc,
396 (struct usb_descriptor_header *) &ncm_union_desc,
397 (struct usb_descriptor_header *) &ecm_desc,
398 (struct usb_descriptor_header *) &ncm_desc,
399 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
400 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
401 /* data interface, altsettings 0 and 1 */
402 (struct usb_descriptor_header *) &ncm_data_nop_intf,
403 (struct usb_descriptor_header *) &ncm_data_intf,
404 (struct usb_descriptor_header *) &ss_ncm_in_desc,
405 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
406 (struct usb_descriptor_header *) &ss_ncm_out_desc,
407 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
411 /* string descriptors: */
413 #define STRING_CTRL_IDX 0
414 #define STRING_MAC_IDX 1
415 #define STRING_DATA_IDX 2
416 #define STRING_IAD_IDX 3
418 static struct usb_string ncm_string_defs[] = {
419 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
420 [STRING_MAC_IDX].s = "",
421 [STRING_DATA_IDX].s = "CDC Network Data",
422 [STRING_IAD_IDX].s = "CDC NCM",
423 { } /* end of list */
426 static struct usb_gadget_strings ncm_string_table = {
427 .language = 0x0409, /* en-us */
428 .strings = ncm_string_defs,
431 static struct usb_gadget_strings *ncm_strings[] = {
437 * Here are options for NCM Datagram Pointer table (NDP) parser.
438 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
439 * in NDP16 offsets and sizes fields are 1 16bit word wide,
440 * in NDP32 -- 2 16bit words wide. Also signatures are different.
441 * To make the parser code the same, put the differences in the structure,
442 * and switch pointers to the structures when the format is changed.
445 struct ndp_parser_opts {
451 unsigned ndplen_align;
452 /* sizes in u16 units */
453 unsigned dgram_item_len; /* index or length */
454 unsigned block_length;
458 unsigned next_ndp_index;
461 #define INIT_NDP16_OPTS { \
462 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
463 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
464 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
465 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
466 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
468 .dgram_item_len = 1, \
473 .next_ndp_index = 1, \
477 #define INIT_NDP32_OPTS { \
478 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
479 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
480 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
481 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
482 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
484 .dgram_item_len = 2, \
489 .next_ndp_index = 2, \
492 static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
493 static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
495 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
499 put_unaligned_le16((u16)val, *p);
502 put_unaligned_le32((u32)val, *p);
512 static inline unsigned get_ncm(__le16 **p, unsigned size)
518 tmp = get_unaligned_le16(*p);
521 tmp = get_unaligned_le32(*p);
531 /*-------------------------------------------------------------------------*/
533 static inline void ncm_reset_values(struct f_ncm *ncm)
535 ncm->parser_opts = &ndp16_opts;
537 ncm->port.cdc_filter = DEFAULT_FILTER;
539 /* doesn't make sense for ncm, fixed size used */
540 ncm->port.header_len = 0;
542 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
543 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
547 * Context: ncm->lock held
549 static void ncm_do_notify(struct f_ncm *ncm)
551 struct usb_request *req = ncm->notify_req;
552 struct usb_cdc_notification *event;
553 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
557 /* notification already in flight? */
558 if (atomic_read(&ncm->notify_count))
562 switch (ncm->notify_state) {
563 case NCM_NOTIFY_NONE:
566 case NCM_NOTIFY_CONNECT:
567 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
569 event->wValue = cpu_to_le16(1);
571 event->wValue = cpu_to_le16(0);
573 req->length = sizeof *event;
575 DBG(cdev, "notify connect %s\n",
576 ncm->is_open ? "true" : "false");
577 ncm->notify_state = NCM_NOTIFY_NONE;
580 case NCM_NOTIFY_SPEED:
581 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
582 event->wValue = cpu_to_le16(0);
583 event->wLength = cpu_to_le16(8);
584 req->length = NCM_STATUS_BYTECOUNT;
586 /* SPEED_CHANGE data is up/down speeds in bits/sec */
587 data = req->buf + sizeof *event;
588 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
591 DBG(cdev, "notify speed %u\n", ncm_bitrate(cdev->gadget));
592 ncm->notify_state = NCM_NOTIFY_CONNECT;
595 event->bmRequestType = 0xA1;
596 event->wIndex = cpu_to_le16(ncm->ctrl_id);
598 atomic_inc(&ncm->notify_count);
601 * In double buffering if there is a space in FIFO,
602 * completion callback can be called right after the call,
605 spin_unlock(&ncm->lock);
606 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
607 spin_lock(&ncm->lock);
609 atomic_dec(&ncm->notify_count);
610 DBG(cdev, "notify --> %d\n", status);
615 * Context: ncm->lock held
617 static void ncm_notify(struct f_ncm *ncm)
620 * NOTE on most versions of Linux, host side cdc-ethernet
621 * won't listen for notifications until its netdevice opens.
622 * The first notification then sits in the FIFO for a long
623 * time, and the second one is queued.
625 * If ncm_notify() is called before the second (CONNECT)
626 * notification is sent, then it will reset to send the SPEED
627 * notificaion again (and again, and again), but it's not a problem
629 ncm->notify_state = NCM_NOTIFY_SPEED;
633 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
635 struct f_ncm *ncm = req->context;
636 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
637 struct usb_cdc_notification *event = req->buf;
639 spin_lock(&ncm->lock);
640 switch (req->status) {
642 VDBG(cdev, "Notification %02x sent\n",
643 event->bNotificationType);
644 atomic_dec(&ncm->notify_count);
648 atomic_set(&ncm->notify_count, 0);
649 ncm->notify_state = NCM_NOTIFY_NONE;
652 DBG(cdev, "event %02x --> %d\n",
653 event->bNotificationType, req->status);
654 atomic_dec(&ncm->notify_count);
658 spin_unlock(&ncm->lock);
661 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
663 /* now for SET_NTB_INPUT_SIZE only */
665 struct usb_function *f = req->context;
666 struct f_ncm *ncm = func_to_ncm(f);
667 struct usb_composite_dev *cdev = f->config->cdev;
670 if (req->status || req->actual != req->length) {
671 DBG(cdev, "Bad control-OUT transfer\n");
675 in_size = get_unaligned_le32(req->buf);
676 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
677 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
678 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
682 ncm->port.fixed_in_len = in_size;
683 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
691 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
693 struct f_ncm *ncm = func_to_ncm(f);
694 struct usb_composite_dev *cdev = f->config->cdev;
695 struct usb_request *req = cdev->req;
696 int value = -EOPNOTSUPP;
697 u16 w_index = le16_to_cpu(ctrl->wIndex);
698 u16 w_value = le16_to_cpu(ctrl->wValue);
699 u16 w_length = le16_to_cpu(ctrl->wLength);
702 * composite driver infrastructure handles everything except
703 * CDC class messages; interface activation uses set_alt().
705 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
706 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
707 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
709 * see 6.2.30: no data, wIndex = interface,
710 * wValue = packet filter bitmap
712 if (w_length != 0 || w_index != ncm->ctrl_id)
714 DBG(cdev, "packet filter %02x\n", w_value);
716 * REVISIT locking of cdc_filter. This assumes the UDC
717 * driver won't have a concurrent packet TX irq running on
718 * another CPU; or that if it does, this write is atomic...
720 ncm->port.cdc_filter = w_value;
725 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
726 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
727 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
728 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
729 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
730 * case USB_CDC_GET_ETHERNET_STATISTIC:
733 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
734 | USB_CDC_GET_NTB_PARAMETERS:
736 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
738 value = w_length > sizeof ntb_parameters ?
739 sizeof ntb_parameters : w_length;
740 memcpy(req->buf, &ntb_parameters, value);
741 VDBG(cdev, "Host asked NTB parameters\n");
744 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
745 | USB_CDC_GET_NTB_INPUT_SIZE:
747 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
749 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
751 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
752 ncm->port.fixed_in_len);
755 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
756 | USB_CDC_SET_NTB_INPUT_SIZE:
758 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
760 req->complete = ncm_ep0out_complete;
761 req->length = w_length;
768 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
769 | USB_CDC_GET_NTB_FORMAT:
773 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
775 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
776 put_unaligned_le16(format, req->buf);
778 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
782 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
783 | USB_CDC_SET_NTB_FORMAT:
785 if (w_length != 0 || w_index != ncm->ctrl_id)
789 ncm->parser_opts = &ndp16_opts;
790 DBG(cdev, "NCM16 selected\n");
793 ncm->parser_opts = &ndp32_opts;
794 DBG(cdev, "NCM32 selected\n");
802 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
803 | USB_CDC_GET_CRC_MODE:
807 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
809 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
810 put_unaligned_le16(is_crc, req->buf);
812 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
816 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
817 | USB_CDC_SET_CRC_MODE:
821 if (w_length != 0 || w_index != ncm->ctrl_id)
826 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
827 DBG(cdev, "non-CRC mode selected\n");
831 ndp_hdr_crc = NCM_NDP_HDR_CRC;
832 DBG(cdev, "CRC mode selected\n");
837 ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
842 /* and disabled in ncm descriptor: */
843 /* case USB_CDC_GET_NET_ADDRESS: */
844 /* case USB_CDC_SET_NET_ADDRESS: */
845 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
846 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
850 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
851 ctrl->bRequestType, ctrl->bRequest,
852 w_value, w_index, w_length);
855 /* respond with data transfer or status phase? */
857 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
858 ctrl->bRequestType, ctrl->bRequest,
859 w_value, w_index, w_length);
862 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
864 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
865 ctrl->bRequestType, ctrl->bRequest,
869 /* device either stalls (value < 0) or reports success */
874 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
876 struct f_ncm *ncm = func_to_ncm(f);
877 struct usb_composite_dev *cdev = f->config->cdev;
879 /* Control interface has only altsetting 0 */
880 if (intf == ncm->ctrl_id) {
884 DBG(cdev, "reset ncm control %d\n", intf);
885 usb_ep_disable(ncm->notify);
887 if (!(ncm->notify->desc)) {
888 DBG(cdev, "init ncm ctrl %d\n", intf);
889 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
892 usb_ep_enable(ncm->notify);
894 /* Data interface has two altsettings, 0 and 1 */
895 } else if (intf == ncm->data_id) {
899 if (ncm->port.in_ep->enabled) {
900 DBG(cdev, "reset ncm\n");
901 ncm->timer_stopping = true;
903 gether_disconnect(&ncm->port);
904 ncm_reset_values(ncm);
908 * CDC Network only sends data in non-default altsettings.
909 * Changing altsettings resets filters, statistics, etc.
912 struct net_device *net;
914 if (!ncm->port.in_ep->desc ||
915 !ncm->port.out_ep->desc) {
916 DBG(cdev, "init ncm\n");
917 if (config_ep_by_speed(cdev->gadget, f,
919 config_ep_by_speed(cdev->gadget, f,
921 ncm->port.in_ep->desc = NULL;
922 ncm->port.out_ep->desc = NULL;
928 /* Enable zlps by default for NCM conformance;
929 * override for musb_hdrc (avoids txdma ovhead)
931 ncm->port.is_zlp_ok =
932 gadget_is_zlp_supported(cdev->gadget);
933 ncm->port.no_skb_reserve =
934 gadget_avoids_skb_reserve(cdev->gadget);
935 ncm->port.cdc_filter = DEFAULT_FILTER;
936 DBG(cdev, "activate ncm\n");
937 net = gether_connect(&ncm->port);
941 ncm->timer_stopping = false;
944 spin_lock(&ncm->lock);
946 spin_unlock(&ncm->lock);
956 * Because the data interface supports multiple altsettings,
957 * this NCM function *MUST* implement a get_alt() method.
959 static int ncm_get_alt(struct usb_function *f, unsigned intf)
961 struct f_ncm *ncm = func_to_ncm(f);
963 if (intf == ncm->ctrl_id)
965 return ncm->port.in_ep->enabled ? 1 : 0;
968 static struct sk_buff *package_for_tx(struct f_ncm *ncm)
971 struct sk_buff *skb2 = NULL;
976 const struct ndp_parser_opts *opts = ncm->parser_opts;
977 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
978 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
981 hrtimer_try_to_cancel(&ncm->task_timer);
983 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
984 ncm->skb_tx_data->len;
985 ndp_index = ncm->skb_tx_data->len + ndp_pad;
986 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
988 /* Set the final BlockLength and wNdpIndex */
989 ntb_iter = (void *) ncm->skb_tx_data->data;
990 /* Increment pointer to BlockLength */
991 ntb_iter += 2 + 1 + 1;
992 put_ncm(&ntb_iter, opts->block_length, new_len);
993 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
995 /* Set the final NDP wLength */
996 new_len = opts->ndp_size +
997 (ncm->ndp_dgram_count * dgram_idx_len);
998 ncm->ndp_dgram_count = 0;
999 /* Increment from start to wLength */
1000 ntb_iter = (void *) ncm->skb_tx_ndp->data;
1002 put_unaligned_le16(new_len, ntb_iter);
1004 /* Merge the skbs */
1005 swap(skb2, ncm->skb_tx_data);
1006 if (ncm->skb_tx_data) {
1007 dev_kfree_skb_any(ncm->skb_tx_data);
1008 ncm->skb_tx_data = NULL;
1011 /* Insert NDP alignment. */
1012 ntb_iter = (void *) skb_put(skb2, ndp_pad);
1013 memset(ntb_iter, 0, ndp_pad);
1015 /* Copy NTB across. */
1016 ntb_iter = (void *) skb_put(skb2, ncm->skb_tx_ndp->len);
1017 memcpy(ntb_iter, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
1018 dev_kfree_skb_any(ncm->skb_tx_ndp);
1019 ncm->skb_tx_ndp = NULL;
1021 /* Insert zero'd datagram. */
1022 ntb_iter = (void *) skb_put(skb2, dgram_idx_len);
1023 memset(ntb_iter, 0, dgram_idx_len);
1028 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1029 struct sk_buff *skb)
1031 struct f_ncm *ncm = func_to_ncm(&port->func);
1032 struct sk_buff *skb2 = NULL;
1038 unsigned max_size = ncm->port.fixed_in_len;
1039 const struct ndp_parser_opts *opts = ncm->parser_opts;
1040 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1041 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1042 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1043 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
1045 if (!skb && !ncm->skb_tx_data)
1049 /* Add the CRC if required up front */
1057 crc_pos = (void *) skb_put(skb, sizeof(uint32_t));
1058 put_unaligned_le32(crc, crc_pos);
1061 /* If the new skb is too big for the current NCM NTB then
1062 * set the current stored skb to be sent now and clear it
1063 * ready for new data.
1064 * NOTE: Assume maximum align for speed of calculation.
1066 if (ncm->skb_tx_data
1067 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1068 || (ncm->skb_tx_data->len +
1069 div + rem + skb->len +
1070 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1072 skb2 = package_for_tx(ncm);
1077 if (!ncm->skb_tx_data) {
1078 ncb_len = opts->nth_size;
1079 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1080 ncb_len += dgram_pad;
1082 /* Create a new skb for the NTH and datagrams. */
1083 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1084 if (!ncm->skb_tx_data)
1087 ntb_data = (void *) skb_put(ncm->skb_tx_data, ncb_len);
1088 memset(ntb_data, 0, ncb_len);
1090 put_unaligned_le32(opts->nth_sign, ntb_data);
1093 put_unaligned_le16(opts->nth_size, ntb_data++);
1095 /* Allocate an skb for storing the NDP,
1096 * TX_MAX_NUM_DPE should easily suffice for a
1099 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1103 if (!ncm->skb_tx_ndp)
1105 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp,
1107 memset(ntb_ndp, 0, ncb_len);
1109 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1112 /* There is always a zeroed entry */
1113 ncm->ndp_dgram_count = 1;
1115 /* Note: we skip opts->next_ndp_index */
1118 /* Delay the timer. */
1119 hrtimer_start(&ncm->task_timer,
1120 ktime_set(0, TX_TIMEOUT_NSECS),
1123 /* Add the datagram position entries */
1124 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp, dgram_idx_len);
1125 memset(ntb_ndp, 0, dgram_idx_len);
1127 ncb_len = ncm->skb_tx_data->len;
1128 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1129 ncb_len += dgram_pad;
1131 /* (d)wDatagramIndex */
1132 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1133 /* (d)wDatagramLength */
1134 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1135 ncm->ndp_dgram_count++;
1137 /* Add the new data to the skb */
1138 ntb_data = (void *) skb_put(ncm->skb_tx_data, dgram_pad);
1139 memset(ntb_data, 0, dgram_pad);
1140 ntb_data = (void *) skb_put(ncm->skb_tx_data, skb->len);
1141 memcpy(ntb_data, skb->data, skb->len);
1142 dev_kfree_skb_any(skb);
1145 } else if (ncm->skb_tx_data && ncm->timer_force_tx) {
1146 /* If the tx was requested because of a timeout then send */
1147 skb2 = package_for_tx(ncm);
1155 ncm->netdev->stats.tx_dropped++;
1158 dev_kfree_skb_any(skb);
1159 if (ncm->skb_tx_data)
1160 dev_kfree_skb_any(ncm->skb_tx_data);
1161 if (ncm->skb_tx_ndp)
1162 dev_kfree_skb_any(ncm->skb_tx_ndp);
1168 * This transmits the NTB if there are frames waiting.
1170 static void ncm_tx_tasklet(unsigned long data)
1172 struct f_ncm *ncm = (void *)data;
1174 if (ncm->timer_stopping)
1177 /* Only send if data is available. */
1178 if (ncm->skb_tx_data) {
1179 ncm->timer_force_tx = true;
1181 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1182 * XXX is not sane. The gadget layer should be redesigned so
1183 * XXX that the dev->wrap() invocations to build SKBs is transparent
1184 * XXX and performed in some way outside of the ndo_start_xmit
1187 ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
1189 ncm->timer_force_tx = false;
1194 * The transmit should only be run if no skb data has been sent
1195 * for a certain duration.
1197 static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1199 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1200 tasklet_schedule(&ncm->tx_tasklet);
1201 return HRTIMER_NORESTART;
1204 static int ncm_unwrap_ntb(struct gether *port,
1205 struct sk_buff *skb,
1206 struct sk_buff_head *list)
1208 struct f_ncm *ncm = func_to_ncm(&port->func);
1209 __le16 *tmp = (void *) skb->data;
1210 unsigned index, index2;
1212 unsigned dg_len, dg_len2;
1215 struct sk_buff *skb2;
1217 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1218 unsigned frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
1219 const struct ndp_parser_opts *opts = ncm->parser_opts;
1220 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1224 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1225 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1227 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1228 skb->data, 32, false);
1234 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1235 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1238 tmp++; /* skip wSequence */
1240 block_len = get_ncm(&tmp, opts->block_length);
1241 /* (d)wBlockLength */
1242 if (block_len > ntb_max) {
1243 INFO(port->func.config->cdev, "OUT size exceeded\n");
1247 ndp_index = get_ncm(&tmp, opts->ndp_index);
1249 /* Run through all the NDP's in the NTB */
1255 if (((ndp_index % 4) != 0) ||
1256 (ndp_index < opts->nth_size) ||
1257 (ndp_index > (block_len -
1259 INFO(port->func.config->cdev, "Bad index: %#X\n",
1268 tmp = (void *)(skb->data + ndp_index);
1269 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1270 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1275 ndp_len = get_unaligned_le16(tmp++);
1280 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1281 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1282 * Each entry is a dgram index and a dgram length.
1284 if ((ndp_len < opts->ndp_size
1285 + 2 * 2 * (opts->dgram_item_len * 2)) ||
1286 (ndp_len % opts->ndplen_align != 0)) {
1287 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1291 tmp += opts->reserved1;
1292 /* Check for another NDP (d)wNextNdpIndex */
1293 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
1294 tmp += opts->reserved2;
1296 ndp_len -= opts->ndp_size;
1297 index2 = get_ncm(&tmp, opts->dgram_item_len);
1298 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1303 /* wDatagramIndex[0] */
1304 if ((index < opts->nth_size) ||
1305 (index > block_len - opts->dpe_size)) {
1306 INFO(port->func.config->cdev,
1307 "Bad index: %#X\n", index);
1313 * wDatagramLength[0]
1314 * ethernet hdr + crc or larger than max frame size
1316 if ((dg_len < 14 + crc_len) ||
1317 (dg_len > frame_max)) {
1318 INFO(port->func.config->cdev,
1319 "Bad dgram length: %#X\n", dg_len);
1325 crc = get_unaligned_le32(skb->data +
1328 crc2 = ~crc32_le(~0,
1332 INFO(port->func.config->cdev,
1338 index2 = get_ncm(&tmp, opts->dgram_item_len);
1339 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1341 /* wDatagramIndex[1] */
1342 if (index2 > block_len - opts->dpe_size) {
1343 INFO(port->func.config->cdev,
1344 "Bad index: %#X\n", index2);
1349 * Copy the data into a new skb.
1350 * This ensures the truesize is correct
1352 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1356 memcpy(skb_put(skb2, dg_len - crc_len),
1357 skb->data + index, dg_len - crc_len);
1359 skb_queue_tail(list, skb2);
1361 ndp_len -= 2 * (opts->dgram_item_len * 2);
1364 if (index2 == 0 || dg_len2 == 0)
1366 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1367 } while (ndp_index);
1369 dev_kfree_skb_any(skb);
1371 VDBG(port->func.config->cdev,
1372 "Parsed NTB with %d frames\n", dgram_counter);
1375 skb_queue_purge(list);
1376 dev_kfree_skb_any(skb);
1380 static void ncm_disable(struct usb_function *f)
1382 struct f_ncm *ncm = func_to_ncm(f);
1383 struct usb_composite_dev *cdev = f->config->cdev;
1385 DBG(cdev, "ncm deactivated\n");
1387 if (ncm->port.in_ep->enabled) {
1388 ncm->timer_stopping = true;
1390 gether_disconnect(&ncm->port);
1393 if (ncm->notify->enabled) {
1394 usb_ep_disable(ncm->notify);
1395 ncm->notify->desc = NULL;
1399 /*-------------------------------------------------------------------------*/
1402 * Callbacks let us notify the host about connect/disconnect when the
1403 * net device is opened or closed.
1405 * For testing, note that link states on this side include both opened
1406 * and closed variants of:
1408 * - disconnected/unconfigured
1409 * - configured but inactive (data alt 0)
1410 * - configured and active (data alt 1)
1412 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1413 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1414 * imply the host is actually polling the notification endpoint, and
1415 * likewise that "active" doesn't imply it's actually using the data
1416 * endpoints for traffic.
1419 static void ncm_open(struct gether *geth)
1421 struct f_ncm *ncm = func_to_ncm(&geth->func);
1423 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1425 spin_lock(&ncm->lock);
1426 ncm->is_open = true;
1428 spin_unlock(&ncm->lock);
1431 static void ncm_close(struct gether *geth)
1433 struct f_ncm *ncm = func_to_ncm(&geth->func);
1435 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1437 spin_lock(&ncm->lock);
1438 ncm->is_open = false;
1440 spin_unlock(&ncm->lock);
1443 /*-------------------------------------------------------------------------*/
1445 /* ethernet function driver setup/binding */
1447 static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1449 struct usb_composite_dev *cdev = c->cdev;
1450 struct f_ncm *ncm = func_to_ncm(f);
1451 struct usb_string *us;
1454 struct f_ncm_opts *ncm_opts;
1456 if (!can_support_ecm(cdev->gadget))
1459 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1461 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1462 * configurations are bound in sequence with list_for_each_entry,
1463 * in each configuration its functions are bound in sequence
1464 * with list_for_each_entry, so we assume no race condition
1465 * with regard to ncm_opts->bound access
1467 if (!ncm_opts->bound) {
1468 mutex_lock(&ncm_opts->lock);
1469 gether_set_gadget(ncm_opts->net, cdev->gadget);
1470 status = gether_register_netdev(ncm_opts->net);
1471 mutex_unlock(&ncm_opts->lock);
1474 ncm_opts->bound = true;
1476 us = usb_gstrings_attach(cdev, ncm_strings,
1477 ARRAY_SIZE(ncm_string_defs));
1480 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1481 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1482 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1483 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1484 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1486 /* allocate instance-specific interface IDs */
1487 status = usb_interface_id(c, f);
1490 ncm->ctrl_id = status;
1491 ncm_iad_desc.bFirstInterface = status;
1493 ncm_control_intf.bInterfaceNumber = status;
1494 ncm_union_desc.bMasterInterface0 = status;
1496 status = usb_interface_id(c, f);
1499 ncm->data_id = status;
1501 ncm_data_nop_intf.bInterfaceNumber = status;
1502 ncm_data_intf.bInterfaceNumber = status;
1503 ncm_union_desc.bSlaveInterface0 = status;
1507 /* allocate instance-specific endpoints */
1508 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1511 ncm->port.in_ep = ep;
1513 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1516 ncm->port.out_ep = ep;
1518 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1525 /* allocate notification request and buffer */
1526 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1527 if (!ncm->notify_req)
1529 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1530 if (!ncm->notify_req->buf)
1532 ncm->notify_req->context = ncm;
1533 ncm->notify_req->complete = ncm_notify_complete;
1536 * support all relevant hardware speeds... we expect that when
1537 * hardware is dual speed, all bulk-capable endpoints work at
1540 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1541 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1542 hs_ncm_notify_desc.bEndpointAddress =
1543 fs_ncm_notify_desc.bEndpointAddress;
1545 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1546 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1547 ss_ncm_notify_desc.bEndpointAddress =
1548 fs_ncm_notify_desc.bEndpointAddress;
1550 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1551 ncm_ss_function, ncm_ss_function);
1556 * NOTE: all that is done without knowing or caring about
1557 * the network link ... which is unavailable to this code
1558 * until we're activated via set_alt().
1561 ncm->port.open = ncm_open;
1562 ncm->port.close = ncm_close;
1564 tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
1565 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1566 ncm->task_timer.function = ncm_tx_timeout;
1568 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1569 gadget_is_superspeed(c->cdev->gadget) ? "super" :
1570 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1571 ncm->port.in_ep->name, ncm->port.out_ep->name,
1576 if (ncm->notify_req) {
1577 kfree(ncm->notify_req->buf);
1578 usb_ep_free_request(ncm->notify, ncm->notify_req);
1581 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1586 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1588 return container_of(to_config_group(item), struct f_ncm_opts,
1592 /* f_ncm_item_ops */
1593 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1595 /* f_ncm_opts_dev_addr */
1596 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1598 /* f_ncm_opts_host_addr */
1599 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1601 /* f_ncm_opts_qmult */
1602 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1604 /* f_ncm_opts_ifname */
1605 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1607 static struct configfs_attribute *ncm_attrs[] = {
1608 &ncm_opts_attr_dev_addr,
1609 &ncm_opts_attr_host_addr,
1610 &ncm_opts_attr_qmult,
1611 &ncm_opts_attr_ifname,
1615 static struct config_item_type ncm_func_type = {
1616 .ct_item_ops = &ncm_item_ops,
1617 .ct_attrs = ncm_attrs,
1618 .ct_owner = THIS_MODULE,
1621 static void ncm_free_inst(struct usb_function_instance *f)
1623 struct f_ncm_opts *opts;
1625 opts = container_of(f, struct f_ncm_opts, func_inst);
1627 gether_cleanup(netdev_priv(opts->net));
1629 free_netdev(opts->net);
1633 static struct usb_function_instance *ncm_alloc_inst(void)
1635 struct f_ncm_opts *opts;
1637 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1639 return ERR_PTR(-ENOMEM);
1640 mutex_init(&opts->lock);
1641 opts->func_inst.free_func_inst = ncm_free_inst;
1642 opts->net = gether_setup_default();
1643 if (IS_ERR(opts->net)) {
1644 struct net_device *net = opts->net;
1646 return ERR_CAST(net);
1649 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1651 return &opts->func_inst;
1654 static void ncm_free(struct usb_function *f)
1657 struct f_ncm_opts *opts;
1659 ncm = func_to_ncm(f);
1660 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1662 mutex_lock(&opts->lock);
1664 mutex_unlock(&opts->lock);
1667 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1669 struct f_ncm *ncm = func_to_ncm(f);
1671 DBG(c->cdev, "ncm unbind\n");
1673 hrtimer_cancel(&ncm->task_timer);
1674 tasklet_kill(&ncm->tx_tasklet);
1676 ncm_string_defs[0].id = 0;
1677 usb_free_all_descriptors(f);
1679 if (atomic_read(&ncm->notify_count)) {
1680 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1681 atomic_set(&ncm->notify_count, 0);
1684 kfree(ncm->notify_req->buf);
1685 usb_ep_free_request(ncm->notify, ncm->notify_req);
1688 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1691 struct f_ncm_opts *opts;
1694 /* allocate and initialize one new instance */
1695 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1697 return ERR_PTR(-ENOMEM);
1699 opts = container_of(fi, struct f_ncm_opts, func_inst);
1700 mutex_lock(&opts->lock);
1703 /* export host's Ethernet address in CDC format */
1704 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1705 sizeof(ncm->ethaddr));
1706 if (status < 12) { /* strlen("01234567890a") */
1708 mutex_unlock(&opts->lock);
1709 return ERR_PTR(-EINVAL);
1711 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1713 spin_lock_init(&ncm->lock);
1714 ncm_reset_values(ncm);
1715 ncm->port.ioport = netdev_priv(opts->net);
1716 mutex_unlock(&opts->lock);
1717 ncm->port.is_fixed = true;
1718 ncm->port.supports_multi_frame = true;
1720 ncm->port.func.name = "cdc_network";
1721 /* descriptors are per-instance copies */
1722 ncm->port.func.bind = ncm_bind;
1723 ncm->port.func.unbind = ncm_unbind;
1724 ncm->port.func.set_alt = ncm_set_alt;
1725 ncm->port.func.get_alt = ncm_get_alt;
1726 ncm->port.func.setup = ncm_setup;
1727 ncm->port.func.disable = ncm_disable;
1728 ncm->port.func.free_func = ncm_free;
1730 ncm->port.wrap = ncm_wrap_ntb;
1731 ncm->port.unwrap = ncm_unwrap_ntb;
1733 return &ncm->port.func;
1736 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1737 MODULE_LICENSE("GPL");
1738 MODULE_AUTHOR("Yauheni Kaliuta");