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_dualspeed(g) && g->speed == USB_SPEED_HIGH)
95 return 13 * 512 * 8 * 1000 * 8;
97 return 19 * 64 * 1 * 1000 * 8;
100 /*-------------------------------------------------------------------------*/
103 * We cannot group frames so use just the minimal size which ok to put
104 * one max-size ethernet frame.
105 * If the host can group frames, allow it to do that, 16K is selected,
106 * because it's used by default by the current linux host driver
108 #define NTB_DEFAULT_IN_SIZE 16384
109 #define NTB_OUT_SIZE 16384
111 /* Allocation for storing the NDP, 32 should suffice for a
112 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
113 * be transmitted in a single 16kB skb, though when sending full size
114 * packets this limit will be plenty.
115 * Smaller packets are not likely to be trying to maximize the
116 * throughput and will be mstly sending smaller infrequent frames.
118 #define TX_MAX_NUM_DPE 32
120 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
121 #define TX_TIMEOUT_NSECS 300000
123 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
124 USB_CDC_NCM_NTB32_SUPPORTED)
126 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
127 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
128 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
129 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
130 .wNdpInDivisor = cpu_to_le16(4),
131 .wNdpInPayloadRemainder = cpu_to_le16(0),
132 .wNdpInAlignment = cpu_to_le16(4),
134 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
135 .wNdpOutDivisor = cpu_to_le16(4),
136 .wNdpOutPayloadRemainder = cpu_to_le16(0),
137 .wNdpOutAlignment = cpu_to_le16(4),
141 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
142 * packet, to simplify cancellation; and a big transfer interval, to
143 * waste less bandwidth.
146 #define NCM_STATUS_INTERVAL_MS 32
147 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
149 static struct usb_interface_assoc_descriptor ncm_iad_desc = {
150 .bLength = sizeof ncm_iad_desc,
151 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
153 /* .bFirstInterface = DYNAMIC, */
154 .bInterfaceCount = 2, /* control + data */
155 .bFunctionClass = USB_CLASS_COMM,
156 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
157 .bFunctionProtocol = USB_CDC_PROTO_NONE,
158 /* .iFunction = DYNAMIC */
161 /* interface descriptor: */
163 static struct usb_interface_descriptor ncm_control_intf = {
164 .bLength = sizeof ncm_control_intf,
165 .bDescriptorType = USB_DT_INTERFACE,
167 /* .bInterfaceNumber = DYNAMIC */
169 .bInterfaceClass = USB_CLASS_COMM,
170 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
171 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
172 /* .iInterface = DYNAMIC */
175 static struct usb_cdc_header_desc ncm_header_desc = {
176 .bLength = sizeof ncm_header_desc,
177 .bDescriptorType = USB_DT_CS_INTERFACE,
178 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
180 .bcdCDC = cpu_to_le16(0x0110),
183 static struct usb_cdc_union_desc ncm_union_desc = {
184 .bLength = sizeof(ncm_union_desc),
185 .bDescriptorType = USB_DT_CS_INTERFACE,
186 .bDescriptorSubType = USB_CDC_UNION_TYPE,
187 /* .bMasterInterface0 = DYNAMIC */
188 /* .bSlaveInterface0 = DYNAMIC */
191 static struct usb_cdc_ether_desc ecm_desc = {
192 .bLength = sizeof ecm_desc,
193 .bDescriptorType = USB_DT_CS_INTERFACE,
194 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
196 /* this descriptor actually adds value, surprise! */
197 /* .iMACAddress = DYNAMIC */
198 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
199 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
200 .wNumberMCFilters = cpu_to_le16(0),
201 .bNumberPowerFilters = 0,
204 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
206 static struct usb_cdc_ncm_desc ncm_desc = {
207 .bLength = sizeof ncm_desc,
208 .bDescriptorType = USB_DT_CS_INTERFACE,
209 .bDescriptorSubType = USB_CDC_NCM_TYPE,
211 .bcdNcmVersion = cpu_to_le16(0x0100),
212 /* can process SetEthernetPacketFilter */
213 .bmNetworkCapabilities = NCAPS,
216 /* the default data interface has no endpoints ... */
218 static struct usb_interface_descriptor ncm_data_nop_intf = {
219 .bLength = sizeof ncm_data_nop_intf,
220 .bDescriptorType = USB_DT_INTERFACE,
222 .bInterfaceNumber = 1,
223 .bAlternateSetting = 0,
225 .bInterfaceClass = USB_CLASS_CDC_DATA,
226 .bInterfaceSubClass = 0,
227 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
228 /* .iInterface = DYNAMIC */
231 /* ... but the "real" data interface has two bulk endpoints */
233 static struct usb_interface_descriptor ncm_data_intf = {
234 .bLength = sizeof ncm_data_intf,
235 .bDescriptorType = USB_DT_INTERFACE,
237 .bInterfaceNumber = 1,
238 .bAlternateSetting = 1,
240 .bInterfaceClass = USB_CLASS_CDC_DATA,
241 .bInterfaceSubClass = 0,
242 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
243 /* .iInterface = DYNAMIC */
246 /* full speed support: */
248 static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
252 .bEndpointAddress = USB_DIR_IN,
253 .bmAttributes = USB_ENDPOINT_XFER_INT,
254 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
255 .bInterval = NCM_STATUS_INTERVAL_MS,
258 static struct usb_endpoint_descriptor fs_ncm_in_desc = {
259 .bLength = USB_DT_ENDPOINT_SIZE,
260 .bDescriptorType = USB_DT_ENDPOINT,
262 .bEndpointAddress = USB_DIR_IN,
263 .bmAttributes = USB_ENDPOINT_XFER_BULK,
266 static struct usb_endpoint_descriptor fs_ncm_out_desc = {
267 .bLength = USB_DT_ENDPOINT_SIZE,
268 .bDescriptorType = USB_DT_ENDPOINT,
270 .bEndpointAddress = USB_DIR_OUT,
271 .bmAttributes = USB_ENDPOINT_XFER_BULK,
274 static struct usb_descriptor_header *ncm_fs_function[] = {
275 (struct usb_descriptor_header *) &ncm_iad_desc,
276 /* CDC NCM control descriptors */
277 (struct usb_descriptor_header *) &ncm_control_intf,
278 (struct usb_descriptor_header *) &ncm_header_desc,
279 (struct usb_descriptor_header *) &ncm_union_desc,
280 (struct usb_descriptor_header *) &ecm_desc,
281 (struct usb_descriptor_header *) &ncm_desc,
282 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
283 /* data interface, altsettings 0 and 1 */
284 (struct usb_descriptor_header *) &ncm_data_nop_intf,
285 (struct usb_descriptor_header *) &ncm_data_intf,
286 (struct usb_descriptor_header *) &fs_ncm_in_desc,
287 (struct usb_descriptor_header *) &fs_ncm_out_desc,
291 /* high speed support: */
293 static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
294 .bLength = USB_DT_ENDPOINT_SIZE,
295 .bDescriptorType = USB_DT_ENDPOINT,
297 .bEndpointAddress = USB_DIR_IN,
298 .bmAttributes = USB_ENDPOINT_XFER_INT,
299 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
300 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
302 static struct usb_endpoint_descriptor hs_ncm_in_desc = {
303 .bLength = USB_DT_ENDPOINT_SIZE,
304 .bDescriptorType = USB_DT_ENDPOINT,
306 .bEndpointAddress = USB_DIR_IN,
307 .bmAttributes = USB_ENDPOINT_XFER_BULK,
308 .wMaxPacketSize = cpu_to_le16(512),
311 static struct usb_endpoint_descriptor hs_ncm_out_desc = {
312 .bLength = USB_DT_ENDPOINT_SIZE,
313 .bDescriptorType = USB_DT_ENDPOINT,
315 .bEndpointAddress = USB_DIR_OUT,
316 .bmAttributes = USB_ENDPOINT_XFER_BULK,
317 .wMaxPacketSize = cpu_to_le16(512),
320 static struct usb_descriptor_header *ncm_hs_function[] = {
321 (struct usb_descriptor_header *) &ncm_iad_desc,
322 /* CDC NCM control descriptors */
323 (struct usb_descriptor_header *) &ncm_control_intf,
324 (struct usb_descriptor_header *) &ncm_header_desc,
325 (struct usb_descriptor_header *) &ncm_union_desc,
326 (struct usb_descriptor_header *) &ecm_desc,
327 (struct usb_descriptor_header *) &ncm_desc,
328 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
329 /* data interface, altsettings 0 and 1 */
330 (struct usb_descriptor_header *) &ncm_data_nop_intf,
331 (struct usb_descriptor_header *) &ncm_data_intf,
332 (struct usb_descriptor_header *) &hs_ncm_in_desc,
333 (struct usb_descriptor_header *) &hs_ncm_out_desc,
337 /* Super Speed Support */
338 static struct usb_endpoint_descriptor ncm_ss_notify_desc = {
339 .bLength = USB_DT_ENDPOINT_SIZE,
340 .bDescriptorType = USB_DT_ENDPOINT,
341 .bEndpointAddress = USB_DIR_IN,
342 .bmAttributes = USB_ENDPOINT_XFER_INT,
343 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
344 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
347 static struct usb_ss_ep_comp_descriptor ncm_ss_notify_comp_desc = {
348 .bLength = sizeof(ncm_ss_notify_comp_desc),
349 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
350 /* the following 3 values can be tweaked if necessary */
351 /* .bMaxBurst = 0, */
352 /* .bmAttributes = 0, */
353 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
356 static struct usb_endpoint_descriptor ncm_ss_in_desc = {
357 .bLength = USB_DT_ENDPOINT_SIZE,
358 .bDescriptorType = USB_DT_ENDPOINT,
359 .bEndpointAddress = USB_DIR_IN,
360 .bmAttributes = USB_ENDPOINT_XFER_BULK,
361 .wMaxPacketSize = cpu_to_le16(1024),
364 static struct usb_ss_ep_comp_descriptor ncm_ss_in_comp_desc = {
365 .bLength = sizeof(ncm_ss_in_comp_desc),
366 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
367 /* the following 2 values can be tweaked if necessary */
368 /* .bMaxBurst = 0, */
369 /* .bmAttributes = 0, */
372 static struct usb_endpoint_descriptor ncm_ss_out_desc = {
373 .bLength = USB_DT_ENDPOINT_SIZE,
374 .bDescriptorType = USB_DT_ENDPOINT,
375 .bEndpointAddress = USB_DIR_OUT,
376 .bmAttributes = USB_ENDPOINT_XFER_BULK,
377 .wMaxPacketSize = cpu_to_le16(1024),
380 static struct usb_ss_ep_comp_descriptor ncm_ss_out_comp_desc = {
381 .bLength = sizeof(ncm_ss_out_comp_desc),
382 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
383 /* the following 2 values can be tweaked if necessary */
384 /* .bMaxBurst = 0, */
385 /* .bmAttributes = 0, */
388 static struct usb_descriptor_header *ncm_ss_function[] = {
389 (struct usb_descriptor_header *) &ncm_iad_desc,
390 /* CDC NCM control descriptors */
391 (struct usb_descriptor_header *) &ncm_control_intf,
392 (struct usb_descriptor_header *) &ncm_header_desc,
393 (struct usb_descriptor_header *) &ncm_union_desc,
394 (struct usb_descriptor_header *) &ecm_desc,
395 (struct usb_descriptor_header *) &ncm_desc,
396 (struct usb_descriptor_header *) &ncm_ss_notify_desc,
397 (struct usb_descriptor_header *) &ncm_ss_notify_comp_desc,
398 /* data interface, altsettings 0 and 1 */
399 (struct usb_descriptor_header *) &ncm_data_nop_intf,
400 (struct usb_descriptor_header *) &ncm_data_intf,
401 (struct usb_descriptor_header *) &ncm_ss_in_desc,
402 (struct usb_descriptor_header *) &ncm_ss_in_comp_desc,
403 (struct usb_descriptor_header *) &ncm_ss_out_desc,
404 (struct usb_descriptor_header *) &ncm_ss_out_comp_desc,
408 /* string descriptors: */
410 #define STRING_CTRL_IDX 0
411 #define STRING_MAC_IDX 1
412 #define STRING_DATA_IDX 2
413 #define STRING_IAD_IDX 3
415 static struct usb_string ncm_string_defs[] = {
416 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
417 [STRING_MAC_IDX].s = "",
418 [STRING_DATA_IDX].s = "CDC Network Data",
419 [STRING_IAD_IDX].s = "CDC NCM",
420 { } /* end of list */
423 static struct usb_gadget_strings ncm_string_table = {
424 .language = 0x0409, /* en-us */
425 .strings = ncm_string_defs,
428 static struct usb_gadget_strings *ncm_strings[] = {
434 * Here are options for NCM Datagram Pointer table (NDP) parser.
435 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
436 * in NDP16 offsets and sizes fields are 1 16bit word wide,
437 * in NDP32 -- 2 16bit words wide. Also signatures are different.
438 * To make the parser code the same, put the differences in the structure,
439 * and switch pointers to the structures when the format is changed.
442 struct ndp_parser_opts {
448 unsigned ndplen_align;
449 /* sizes in u16 units */
450 unsigned dgram_item_len; /* index or length */
451 unsigned block_length;
455 unsigned next_ndp_index;
458 #define INIT_NDP16_OPTS { \
459 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
460 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
461 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
462 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
463 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
465 .dgram_item_len = 1, \
470 .next_ndp_index = 1, \
474 #define INIT_NDP32_OPTS { \
475 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
476 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
477 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
478 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
479 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
481 .dgram_item_len = 2, \
486 .next_ndp_index = 2, \
489 static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
490 static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
492 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
496 put_unaligned_le16((u16)val, *p);
499 put_unaligned_le32((u32)val, *p);
509 static inline unsigned get_ncm(__le16 **p, unsigned size)
515 tmp = get_unaligned_le16(*p);
518 tmp = get_unaligned_le32(*p);
528 /*-------------------------------------------------------------------------*/
530 static inline void ncm_reset_values(struct f_ncm *ncm)
532 ncm->parser_opts = &ndp16_opts;
534 ncm->port.cdc_filter = DEFAULT_FILTER;
536 /* doesn't make sense for ncm, fixed size used */
537 ncm->port.header_len = 0;
539 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
540 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
544 * Context: ncm->lock held
546 static void ncm_do_notify(struct f_ncm *ncm)
548 struct usb_request *req = ncm->notify_req;
549 struct usb_cdc_notification *event;
550 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
554 /* notification already in flight? */
555 if (atomic_read(&ncm->notify_count))
559 switch (ncm->notify_state) {
560 case NCM_NOTIFY_NONE:
563 case NCM_NOTIFY_CONNECT:
564 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
566 event->wValue = cpu_to_le16(1);
568 event->wValue = cpu_to_le16(0);
570 req->length = sizeof *event;
572 DBG(cdev, "notify connect %s\n",
573 ncm->is_open ? "true" : "false");
574 ncm->notify_state = NCM_NOTIFY_NONE;
577 case NCM_NOTIFY_SPEED:
578 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
579 event->wValue = cpu_to_le16(0);
580 event->wLength = cpu_to_le16(8);
581 req->length = NCM_STATUS_BYTECOUNT;
583 /* SPEED_CHANGE data is up/down speeds in bits/sec */
584 data = req->buf + sizeof *event;
585 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
588 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
589 ncm->notify_state = NCM_NOTIFY_CONNECT;
592 event->bmRequestType = 0xA1;
593 event->wIndex = cpu_to_le16(ncm->ctrl_id);
595 atomic_inc(&ncm->notify_count);
598 * In double buffering if there is a space in FIFO,
599 * completion callback can be called right after the call,
602 spin_unlock(&ncm->lock);
603 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
604 spin_lock(&ncm->lock);
606 atomic_dec(&ncm->notify_count);
607 DBG(cdev, "notify --> %d\n", status);
612 * Context: ncm->lock held
614 static void ncm_notify(struct f_ncm *ncm)
617 * NOTE on most versions of Linux, host side cdc-ethernet
618 * won't listen for notifications until its netdevice opens.
619 * The first notification then sits in the FIFO for a long
620 * time, and the second one is queued.
622 * If ncm_notify() is called before the second (CONNECT)
623 * notification is sent, then it will reset to send the SPEED
624 * notificaion again (and again, and again), but it's not a problem
626 ncm->notify_state = NCM_NOTIFY_SPEED;
630 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
632 struct f_ncm *ncm = req->context;
633 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
634 struct usb_cdc_notification *event = req->buf;
636 spin_lock(&ncm->lock);
637 switch (req->status) {
639 VDBG(cdev, "Notification %02x sent\n",
640 event->bNotificationType);
641 atomic_dec(&ncm->notify_count);
645 atomic_set(&ncm->notify_count, 0);
646 ncm->notify_state = NCM_NOTIFY_NONE;
649 DBG(cdev, "event %02x --> %d\n",
650 event->bNotificationType, req->status);
651 atomic_dec(&ncm->notify_count);
655 spin_unlock(&ncm->lock);
658 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
660 /* now for SET_NTB_INPUT_SIZE only */
662 struct usb_function *f = req->context;
663 struct f_ncm *ncm = func_to_ncm(f);
664 struct usb_composite_dev *cdev = f->config->cdev;
667 if (req->status || req->actual != req->length) {
668 DBG(cdev, "Bad control-OUT transfer\n");
672 in_size = get_unaligned_le32(req->buf);
673 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
674 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
675 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
679 ncm->port.fixed_in_len = in_size;
680 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
688 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
690 struct f_ncm *ncm = func_to_ncm(f);
691 struct usb_composite_dev *cdev = f->config->cdev;
692 struct usb_request *req = cdev->req;
693 int value = -EOPNOTSUPP;
694 u16 w_index = le16_to_cpu(ctrl->wIndex);
695 u16 w_value = le16_to_cpu(ctrl->wValue);
696 u16 w_length = le16_to_cpu(ctrl->wLength);
699 * composite driver infrastructure handles everything except
700 * CDC class messages; interface activation uses set_alt().
702 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
703 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
704 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
706 * see 6.2.30: no data, wIndex = interface,
707 * wValue = packet filter bitmap
709 if (w_length != 0 || w_index != ncm->ctrl_id)
711 DBG(cdev, "packet filter %02x\n", w_value);
713 * REVISIT locking of cdc_filter. This assumes the UDC
714 * driver won't have a concurrent packet TX irq running on
715 * another CPU; or that if it does, this write is atomic...
717 ncm->port.cdc_filter = w_value;
722 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
723 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
724 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
725 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
726 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
727 * case USB_CDC_GET_ETHERNET_STATISTIC:
730 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
731 | USB_CDC_GET_NTB_PARAMETERS:
733 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
735 value = w_length > sizeof ntb_parameters ?
736 sizeof ntb_parameters : w_length;
737 memcpy(req->buf, &ntb_parameters, value);
738 VDBG(cdev, "Host asked NTB parameters\n");
741 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
742 | USB_CDC_GET_NTB_INPUT_SIZE:
744 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
746 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
748 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
749 ncm->port.fixed_in_len);
752 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
753 | USB_CDC_SET_NTB_INPUT_SIZE:
755 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
757 req->complete = ncm_ep0out_complete;
758 req->length = w_length;
765 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
766 | USB_CDC_GET_NTB_FORMAT:
770 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
772 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
773 put_unaligned_le16(format, req->buf);
775 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
779 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
780 | USB_CDC_SET_NTB_FORMAT:
782 if (w_length != 0 || w_index != ncm->ctrl_id)
786 ncm->parser_opts = &ndp16_opts;
787 DBG(cdev, "NCM16 selected\n");
790 ncm->parser_opts = &ndp32_opts;
791 DBG(cdev, "NCM32 selected\n");
799 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
800 | USB_CDC_GET_CRC_MODE:
804 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
806 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
807 put_unaligned_le16(is_crc, req->buf);
809 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
813 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
814 | USB_CDC_SET_CRC_MODE:
818 if (w_length != 0 || w_index != ncm->ctrl_id)
823 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
824 DBG(cdev, "non-CRC mode selected\n");
828 ndp_hdr_crc = NCM_NDP_HDR_CRC;
829 DBG(cdev, "CRC mode selected\n");
834 ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
839 /* and disabled in ncm descriptor: */
840 /* case USB_CDC_GET_NET_ADDRESS: */
841 /* case USB_CDC_SET_NET_ADDRESS: */
842 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
843 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
847 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
848 ctrl->bRequestType, ctrl->bRequest,
849 w_value, w_index, w_length);
852 /* respond with data transfer or status phase? */
854 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
855 ctrl->bRequestType, ctrl->bRequest,
856 w_value, w_index, w_length);
859 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
861 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
862 ctrl->bRequestType, ctrl->bRequest,
866 /* device either stalls (value < 0) or reports success */
871 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
873 struct f_ncm *ncm = func_to_ncm(f);
874 struct usb_composite_dev *cdev = f->config->cdev;
876 /* Control interface has only altsetting 0 */
877 if (intf == ncm->ctrl_id) {
881 DBG(cdev, "reset ncm control %d\n", intf);
882 usb_ep_disable(ncm->notify);
884 if (!(ncm->notify->desc)) {
885 DBG(cdev, "init ncm ctrl %d\n", intf);
886 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
889 usb_ep_enable(ncm->notify);
891 /* Data interface has two altsettings, 0 and 1 */
892 } else if (intf == ncm->data_id) {
896 if (ncm->port.in_ep->enabled) {
897 DBG(cdev, "reset ncm\n");
898 ncm->timer_stopping = true;
900 gether_disconnect(&ncm->port);
901 ncm_reset_values(ncm);
905 * CDC Network only sends data in non-default altsettings.
906 * Changing altsettings resets filters, statistics, etc.
909 struct net_device *net;
911 if (!ncm->port.in_ep->desc ||
912 !ncm->port.out_ep->desc) {
913 DBG(cdev, "init ncm\n");
914 if (config_ep_by_speed(cdev->gadget, f,
916 config_ep_by_speed(cdev->gadget, f,
918 ncm->port.in_ep->desc = NULL;
919 ncm->port.out_ep->desc = NULL;
925 /* Enable zlps by default for NCM conformance;
926 * override for musb_hdrc (avoids txdma ovhead)
928 ncm->port.is_zlp_ok =
929 gadget_is_zlp_supported(cdev->gadget);
930 ncm->port.cdc_filter = DEFAULT_FILTER;
931 DBG(cdev, "activate ncm\n");
932 net = gether_connect(&ncm->port);
936 ncm->timer_stopping = false;
939 spin_lock(&ncm->lock);
941 spin_unlock(&ncm->lock);
951 * Because the data interface supports multiple altsettings,
952 * this NCM function *MUST* implement a get_alt() method.
954 static int ncm_get_alt(struct usb_function *f, unsigned intf)
956 struct f_ncm *ncm = func_to_ncm(f);
958 if (intf == ncm->ctrl_id)
960 return ncm->port.in_ep->enabled ? 1 : 0;
963 static struct sk_buff *package_for_tx(struct f_ncm *ncm)
966 struct sk_buff *skb2 = NULL;
971 const struct ndp_parser_opts *opts = ncm->parser_opts;
972 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
973 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
976 hrtimer_try_to_cancel(&ncm->task_timer);
978 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
979 ncm->skb_tx_data->len;
980 ndp_index = ncm->skb_tx_data->len + ndp_pad;
981 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
983 /* Set the final BlockLength and wNdpIndex */
984 ntb_iter = (void *) ncm->skb_tx_data->data;
985 /* Increment pointer to BlockLength */
986 ntb_iter += 2 + 1 + 1;
987 put_ncm(&ntb_iter, opts->block_length, new_len);
988 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
990 /* Set the final NDP wLength */
991 new_len = opts->ndp_size +
992 (ncm->ndp_dgram_count * dgram_idx_len);
993 ncm->ndp_dgram_count = 0;
994 /* Increment from start to wLength */
995 ntb_iter = (void *) ncm->skb_tx_ndp->data;
997 put_unaligned_le16(new_len, ntb_iter);
1000 swap(skb2, ncm->skb_tx_data);
1001 if (ncm->skb_tx_data) {
1002 dev_kfree_skb_any(ncm->skb_tx_data);
1003 ncm->skb_tx_data = NULL;
1006 /* Insert NDP alignment. */
1007 ntb_iter = (void *) skb_put(skb2, ndp_pad);
1008 memset(ntb_iter, 0, ndp_pad);
1010 /* Copy NTB across. */
1011 ntb_iter = (void *) skb_put(skb2, ncm->skb_tx_ndp->len);
1012 memcpy(ntb_iter, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
1013 dev_kfree_skb_any(ncm->skb_tx_ndp);
1014 ncm->skb_tx_ndp = NULL;
1016 /* Insert zero'd datagram. */
1017 ntb_iter = (void *) skb_put(skb2, dgram_idx_len);
1018 memset(ntb_iter, 0, dgram_idx_len);
1023 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1024 struct sk_buff *skb)
1026 struct f_ncm *ncm = func_to_ncm(&port->func);
1027 struct sk_buff *skb2 = NULL;
1033 unsigned max_size = ncm->port.fixed_in_len;
1034 const struct ndp_parser_opts *opts = ncm->parser_opts;
1035 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1036 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1037 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1038 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
1040 if (!skb && !ncm->skb_tx_data)
1044 /* Add the CRC if required up front */
1052 crc_pos = (void *) skb_put(skb, sizeof(uint32_t));
1053 put_unaligned_le32(crc, crc_pos);
1056 /* If the new skb is too big for the current NCM NTB then
1057 * set the current stored skb to be sent now and clear it
1058 * ready for new data.
1059 * NOTE: Assume maximum align for speed of calculation.
1061 if (ncm->skb_tx_data
1062 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1063 || (ncm->skb_tx_data->len +
1064 div + rem + skb->len +
1065 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1067 skb2 = package_for_tx(ncm);
1072 if (!ncm->skb_tx_data) {
1073 ncb_len = opts->nth_size;
1074 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1075 ncb_len += dgram_pad;
1077 /* Create a new skb for the NTH and datagrams. */
1078 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1079 if (!ncm->skb_tx_data)
1082 ntb_data = (void *) skb_put(ncm->skb_tx_data, ncb_len);
1083 memset(ntb_data, 0, ncb_len);
1085 put_unaligned_le32(opts->nth_sign, ntb_data);
1088 put_unaligned_le16(opts->nth_size, ntb_data++);
1090 /* Allocate an skb for storing the NDP,
1091 * TX_MAX_NUM_DPE should easily suffice for a
1094 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1098 if (!ncm->skb_tx_ndp)
1100 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp,
1102 memset(ntb_ndp, 0, ncb_len);
1104 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1107 /* There is always a zeroed entry */
1108 ncm->ndp_dgram_count = 1;
1110 /* Note: we skip opts->next_ndp_index */
1113 /* Delay the timer. */
1114 hrtimer_start(&ncm->task_timer,
1115 ktime_set(0, TX_TIMEOUT_NSECS),
1118 /* Add the datagram position entries */
1119 ntb_ndp = (void *) skb_put(ncm->skb_tx_ndp, dgram_idx_len);
1120 memset(ntb_ndp, 0, dgram_idx_len);
1122 ncb_len = ncm->skb_tx_data->len;
1123 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1124 ncb_len += dgram_pad;
1126 /* (d)wDatagramIndex */
1127 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1128 /* (d)wDatagramLength */
1129 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1130 ncm->ndp_dgram_count++;
1132 /* Add the new data to the skb */
1133 ntb_data = (void *) skb_put(ncm->skb_tx_data, dgram_pad);
1134 memset(ntb_data, 0, dgram_pad);
1135 ntb_data = (void *) skb_put(ncm->skb_tx_data, skb->len);
1136 memcpy(ntb_data, skb->data, skb->len);
1137 dev_kfree_skb_any(skb);
1140 } else if (ncm->skb_tx_data && ncm->timer_force_tx) {
1141 /* If the tx was requested because of a timeout then send */
1142 skb2 = package_for_tx(ncm);
1150 ncm->netdev->stats.tx_dropped++;
1153 dev_kfree_skb_any(skb);
1154 if (ncm->skb_tx_data)
1155 dev_kfree_skb_any(ncm->skb_tx_data);
1156 if (ncm->skb_tx_ndp)
1157 dev_kfree_skb_any(ncm->skb_tx_ndp);
1163 * This transmits the NTB if there are frames waiting.
1165 static void ncm_tx_tasklet(unsigned long data)
1167 struct f_ncm *ncm = (void *)data;
1169 if (ncm->timer_stopping)
1172 /* Only send if data is available. */
1173 if (ncm->skb_tx_data) {
1174 ncm->timer_force_tx = true;
1176 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1177 * XXX is not sane. The gadget layer should be redesigned so
1178 * XXX that the dev->wrap() invocations to build SKBs is transparent
1179 * XXX and performed in some way outside of the ndo_start_xmit
1182 ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
1184 ncm->timer_force_tx = false;
1189 * The transmit should only be run if no skb data has been sent
1190 * for a certain duration.
1192 static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1194 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1195 tasklet_schedule(&ncm->tx_tasklet);
1196 return HRTIMER_NORESTART;
1199 static int ncm_unwrap_ntb(struct gether *port,
1200 struct sk_buff *skb,
1201 struct sk_buff_head *list)
1203 struct f_ncm *ncm = func_to_ncm(&port->func);
1204 __le16 *tmp = (void *) skb->data;
1205 unsigned index, index2;
1207 unsigned dg_len, dg_len2;
1209 struct sk_buff *skb2;
1211 unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1212 const struct ndp_parser_opts *opts = ncm->parser_opts;
1213 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1217 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1218 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1220 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1221 skb->data, 32, false);
1227 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1228 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1231 tmp++; /* skip wSequence */
1233 /* (d)wBlockLength */
1234 if (get_ncm(&tmp, opts->block_length) > max_size) {
1235 INFO(port->func.config->cdev, "OUT size exceeded\n");
1239 ndp_index = get_ncm(&tmp, opts->ndp_index);
1241 /* Run through all the NDP's in the NTB */
1244 if (((ndp_index % 4) != 0) &&
1245 (ndp_index < opts->nth_size)) {
1246 INFO(port->func.config->cdev, "Bad index: %#X\n",
1251 /* walk through NDP */
1252 tmp = (void *)(skb->data + ndp_index);
1253 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1254 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1259 ndp_len = get_unaligned_le16(tmp++);
1263 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1264 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1265 * Each entry is a dgram index and a dgram length.
1267 if ((ndp_len < opts->ndp_size
1268 + 2 * 2 * (opts->dgram_item_len * 2))
1269 || (ndp_len % opts->ndplen_align != 0)) {
1270 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1274 tmp += opts->reserved1;
1275 /* Check for another NDP (d)wNextNdpIndex */
1276 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
1277 tmp += opts->reserved2;
1279 ndp_len -= opts->ndp_size;
1280 index2 = get_ncm(&tmp, opts->dgram_item_len);
1281 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1287 if (dg_len < 14 + crc_len) { /* ethernet hdr + crc */
1288 INFO(port->func.config->cdev,
1289 "Bad dgram length: %#X\n", dg_len);
1295 crc = get_unaligned_le32(skb->data +
1298 crc2 = ~crc32_le(~0,
1302 INFO(port->func.config->cdev,
1308 index2 = get_ncm(&tmp, opts->dgram_item_len);
1309 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1312 * Copy the data into a new skb.
1313 * This ensures the truesize is correct
1315 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1319 memcpy(skb_put(skb2, dg_len - crc_len),
1320 skb->data + index, dg_len - crc_len);
1322 skb_queue_tail(list, skb2);
1324 ndp_len -= 2 * (opts->dgram_item_len * 2);
1328 if (index2 == 0 || dg_len2 == 0)
1330 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1331 } while (ndp_index);
1333 dev_kfree_skb_any(skb);
1335 VDBG(port->func.config->cdev,
1336 "Parsed NTB with %d frames\n", dgram_counter);
1339 skb_queue_purge(list);
1340 dev_kfree_skb_any(skb);
1344 static void ncm_disable(struct usb_function *f)
1346 struct f_ncm *ncm = func_to_ncm(f);
1347 struct usb_composite_dev *cdev = f->config->cdev;
1349 DBG(cdev, "ncm deactivated\n");
1351 if (ncm->port.in_ep->enabled) {
1352 ncm->timer_stopping = true;
1354 gether_disconnect(&ncm->port);
1357 if (ncm->notify->enabled) {
1358 usb_ep_disable(ncm->notify);
1359 ncm->notify->desc = NULL;
1363 /*-------------------------------------------------------------------------*/
1366 * Callbacks let us notify the host about connect/disconnect when the
1367 * net device is opened or closed.
1369 * For testing, note that link states on this side include both opened
1370 * and closed variants of:
1372 * - disconnected/unconfigured
1373 * - configured but inactive (data alt 0)
1374 * - configured and active (data alt 1)
1376 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1377 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1378 * imply the host is actually polling the notification endpoint, and
1379 * likewise that "active" doesn't imply it's actually using the data
1380 * endpoints for traffic.
1383 static void ncm_open(struct gether *geth)
1385 struct f_ncm *ncm = func_to_ncm(&geth->func);
1387 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1389 spin_lock(&ncm->lock);
1390 ncm->is_open = true;
1392 spin_unlock(&ncm->lock);
1395 static void ncm_close(struct gether *geth)
1397 struct f_ncm *ncm = func_to_ncm(&geth->func);
1399 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1401 spin_lock(&ncm->lock);
1402 ncm->is_open = false;
1404 spin_unlock(&ncm->lock);
1407 /*-------------------------------------------------------------------------*/
1409 /* ethernet function driver setup/binding */
1411 static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1413 struct usb_composite_dev *cdev = c->cdev;
1414 struct f_ncm *ncm = func_to_ncm(f);
1415 struct usb_string *us;
1418 struct f_ncm_opts *ncm_opts;
1420 if (!can_support_ecm(cdev->gadget))
1423 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1425 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1426 * configurations are bound in sequence with list_for_each_entry,
1427 * in each configuration its functions are bound in sequence
1428 * with list_for_each_entry, so we assume no race condition
1429 * with regard to ncm_opts->bound access
1431 if (!ncm_opts->bound) {
1432 mutex_lock(&ncm_opts->lock);
1433 ncm_opts->net = gether_setup_default();
1434 if (IS_ERR(ncm_opts->net)) {
1435 status = PTR_ERR(ncm_opts->net);
1436 mutex_unlock(&ncm_opts->lock);
1439 gether_set_gadget(ncm_opts->net, cdev->gadget);
1440 status = gether_register_netdev(ncm_opts->net);
1441 mutex_unlock(&ncm_opts->lock);
1443 free_netdev(ncm_opts->net);
1446 ncm_opts->bound = true;
1449 /* export host's Ethernet address in CDC format */
1450 status = gether_get_host_addr_cdc(ncm_opts->net, ncm->ethaddr,
1451 sizeof(ncm->ethaddr));
1452 if (status < 12) { /* strlen("01234567890a") */
1453 ERROR(cdev, "%s: failed to get host eth addr, err %d\n",
1456 goto netdev_cleanup;
1458 ncm->port.ioport = netdev_priv(ncm_opts->net);
1460 us = usb_gstrings_attach(cdev, ncm_strings,
1461 ARRAY_SIZE(ncm_string_defs));
1463 status = PTR_ERR(us);
1464 goto netdev_cleanup;
1466 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1467 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1468 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1469 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1470 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1472 /* allocate instance-specific interface IDs */
1473 status = usb_interface_id(c, f);
1476 ncm->ctrl_id = status;
1477 ncm_iad_desc.bFirstInterface = status;
1479 ncm_control_intf.bInterfaceNumber = status;
1480 ncm_union_desc.bMasterInterface0 = status;
1482 status = usb_interface_id(c, f);
1485 ncm->data_id = status;
1487 ncm_data_nop_intf.bInterfaceNumber = status;
1488 ncm_data_intf.bInterfaceNumber = status;
1489 ncm_union_desc.bSlaveInterface0 = status;
1493 /* allocate instance-specific endpoints */
1494 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1497 ncm->port.in_ep = ep;
1499 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1502 ncm->port.out_ep = ep;
1504 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1511 /* allocate notification request and buffer */
1512 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1513 if (!ncm->notify_req)
1515 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1516 if (!ncm->notify_req->buf)
1518 ncm->notify_req->context = ncm;
1519 ncm->notify_req->complete = ncm_notify_complete;
1522 * support all relevant hardware speeds... we expect that when
1523 * hardware is dual speed, all bulk-capable endpoints work at
1526 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1527 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1528 hs_ncm_notify_desc.bEndpointAddress =
1529 fs_ncm_notify_desc.bEndpointAddress;
1531 if (gadget_is_superspeed(c->cdev->gadget)) {
1532 ncm_ss_in_desc.bEndpointAddress =
1533 fs_ncm_in_desc.bEndpointAddress;
1534 ncm_ss_out_desc.bEndpointAddress =
1535 fs_ncm_out_desc.bEndpointAddress;
1536 ncm_ss_notify_desc.bEndpointAddress =
1537 fs_ncm_notify_desc.bEndpointAddress;
1540 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1546 * NOTE: all that is done without knowing or caring about
1547 * the network link ... which is unavailable to this code
1548 * until we're activated via set_alt().
1551 ncm->port.open = ncm_open;
1552 ncm->port.close = ncm_close;
1554 tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
1555 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1556 ncm->task_timer.function = ncm_tx_timeout;
1558 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1559 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1560 ncm->port.in_ep->name, ncm->port.out_ep->name,
1565 if (ncm->notify_req) {
1566 kfree(ncm->notify_req->buf);
1567 usb_ep_free_request(ncm->notify, ncm->notify_req);
1570 gether_cleanup(netdev_priv(ncm_opts->net));
1573 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1578 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1580 return container_of(to_config_group(item), struct f_ncm_opts,
1584 /* f_ncm_item_ops */
1585 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1587 /* f_ncm_opts_dev_addr */
1588 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1590 /* f_ncm_opts_host_addr */
1591 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1593 /* f_ncm_opts_qmult */
1594 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1596 /* f_ncm_opts_ifname */
1597 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1599 static struct configfs_attribute *ncm_attrs[] = {
1600 &ncm_opts_attr_dev_addr,
1601 &ncm_opts_attr_host_addr,
1602 &ncm_opts_attr_qmult,
1603 &ncm_opts_attr_ifname,
1607 static struct config_item_type ncm_func_type = {
1608 .ct_item_ops = &ncm_item_ops,
1609 .ct_attrs = ncm_attrs,
1610 .ct_owner = THIS_MODULE,
1613 static void ncm_free_inst(struct usb_function_instance *f)
1615 struct f_ncm_opts *opts;
1617 opts = container_of(f, struct f_ncm_opts, func_inst);
1619 gether_cleanup(netdev_priv(opts->net));
1623 static struct usb_function_instance *ncm_alloc_inst(void)
1625 struct f_ncm_opts *opts;
1627 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1629 return ERR_PTR(-ENOMEM);
1630 mutex_init(&opts->lock);
1631 opts->func_inst.free_func_inst = ncm_free_inst;
1633 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1635 return &opts->func_inst;
1638 static void ncm_free(struct usb_function *f)
1641 struct f_ncm_opts *opts;
1643 ncm = func_to_ncm(f);
1644 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1646 mutex_lock(&opts->lock);
1648 mutex_unlock(&opts->lock);
1651 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1653 struct f_ncm *ncm = func_to_ncm(f);
1654 struct f_ncm_opts *opts = container_of(f->fi, struct f_ncm_opts,
1657 DBG(c->cdev, "ncm unbind\n");
1659 opts->bound = false;
1661 hrtimer_cancel(&ncm->task_timer);
1662 tasklet_kill(&ncm->tx_tasklet);
1664 ncm_string_defs[0].id = 0;
1665 usb_free_all_descriptors(f);
1667 if (atomic_read(&ncm->notify_count)) {
1668 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1669 atomic_set(&ncm->notify_count, 0);
1672 kfree(ncm->notify_req->buf);
1673 usb_ep_free_request(ncm->notify, ncm->notify_req);
1675 gether_cleanup(netdev_priv(opts->net));
1678 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1681 struct f_ncm_opts *opts;
1683 /* allocate and initialize one new instance */
1684 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1686 return ERR_PTR(-ENOMEM);
1688 opts = container_of(fi, struct f_ncm_opts, func_inst);
1689 mutex_lock(&opts->lock);
1691 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1692 spin_lock_init(&ncm->lock);
1693 ncm_reset_values(ncm);
1694 mutex_unlock(&opts->lock);
1695 ncm->port.is_fixed = true;
1696 ncm->port.supports_multi_frame = true;
1698 ncm->port.func.name = "cdc_network";
1699 /* descriptors are per-instance copies */
1700 ncm->port.func.bind = ncm_bind;
1701 ncm->port.func.unbind = ncm_unbind;
1702 ncm->port.func.set_alt = ncm_set_alt;
1703 ncm->port.func.get_alt = ncm_get_alt;
1704 ncm->port.func.setup = ncm_setup;
1705 ncm->port.func.disable = ncm_disable;
1706 ncm->port.func.free_func = ncm_free;
1708 ncm->port.wrap = ncm_wrap_ntb;
1709 ncm->port.unwrap = ncm_unwrap_ntb;
1711 return &ncm->port.func;
1714 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1715 MODULE_LICENSE("GPL");
1716 MODULE_AUTHOR("Yauheni Kaliuta");