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/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/etherdevice.h>
23 #include <linux/crc32.h>
25 #include <linux/usb/cdc.h>
28 #include "u_ether_configfs.h"
32 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
33 * NCM is intended to be used with high-speed network attachments.
35 * Note that NCM requires the use of "alternate settings" for its data
36 * interface. This means that the set_alt() method has real work to do,
37 * and also means that a get_alt() method is required.
40 /* to trigger crc/non-crc ndp signature */
42 #define NCM_NDP_HDR_CRC_MASK 0x01000000
43 #define NCM_NDP_HDR_CRC 0x01000000
44 #define NCM_NDP_HDR_NOCRC 0x00000000
46 enum ncm_notify_state {
47 NCM_NOTIFY_NONE, /* don't notify */
48 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
49 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
58 struct usb_ep *notify;
59 struct usb_request *notify_req;
61 atomic_t notify_count;
64 const struct ndp_parser_opts *parser_opts;
69 * for notification, it is accessed from both
70 * callback and ethernet open/close
74 struct net_device *netdev;
76 /* For multi-frame NDP TX */
77 struct sk_buff *skb_tx_data;
78 struct sk_buff *skb_tx_ndp;
81 struct tasklet_struct tx_tasklet;
82 struct hrtimer task_timer;
87 static inline struct f_ncm *func_to_ncm(struct usb_function *f)
89 return container_of(f, struct f_ncm, port.func);
92 /* peak (theoretical) bulk transfer rate in bits-per-second */
93 static inline unsigned ncm_bitrate(struct usb_gadget *g)
97 else if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER_PLUS)
99 else if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
101 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
102 return 13 * 512 * 8 * 1000 * 8;
104 return 19 * 64 * 1 * 1000 * 8;
107 /*-------------------------------------------------------------------------*/
110 * We cannot group frames so use just the minimal size which ok to put
111 * one max-size ethernet frame.
112 * If the host can group frames, allow it to do that, 16K is selected,
113 * because it's used by default by the current linux host driver
115 #define NTB_DEFAULT_IN_SIZE 16384
116 #define NTB_OUT_SIZE 16384
118 /* Allocation for storing the NDP, 32 should suffice for a
119 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
120 * be transmitted in a single 16kB skb, though when sending full size
121 * packets this limit will be plenty.
122 * Smaller packets are not likely to be trying to maximize the
123 * throughput and will be mstly sending smaller infrequent frames.
125 #define TX_MAX_NUM_DPE 32
127 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
128 #define TX_TIMEOUT_NSECS 300000
130 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
131 USB_CDC_NCM_NTB32_SUPPORTED)
133 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
134 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
135 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
136 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
137 .wNdpInDivisor = cpu_to_le16(4),
138 .wNdpInPayloadRemainder = cpu_to_le16(0),
139 .wNdpInAlignment = cpu_to_le16(4),
141 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
142 .wNdpOutDivisor = cpu_to_le16(4),
143 .wNdpOutPayloadRemainder = cpu_to_le16(0),
144 .wNdpOutAlignment = cpu_to_le16(4),
148 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
149 * packet, to simplify cancellation; and a big transfer interval, to
150 * waste less bandwidth.
153 #define NCM_STATUS_INTERVAL_MS 32
154 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
156 static struct usb_interface_assoc_descriptor ncm_iad_desc = {
157 .bLength = sizeof ncm_iad_desc,
158 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
160 /* .bFirstInterface = DYNAMIC, */
161 .bInterfaceCount = 2, /* control + data */
162 .bFunctionClass = USB_CLASS_COMM,
163 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
164 .bFunctionProtocol = USB_CDC_PROTO_NONE,
165 /* .iFunction = DYNAMIC */
168 /* interface descriptor: */
170 static struct usb_interface_descriptor ncm_control_intf = {
171 .bLength = sizeof ncm_control_intf,
172 .bDescriptorType = USB_DT_INTERFACE,
174 /* .bInterfaceNumber = DYNAMIC */
176 .bInterfaceClass = USB_CLASS_COMM,
177 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
178 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
179 /* .iInterface = DYNAMIC */
182 static struct usb_cdc_header_desc ncm_header_desc = {
183 .bLength = sizeof ncm_header_desc,
184 .bDescriptorType = USB_DT_CS_INTERFACE,
185 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
187 .bcdCDC = cpu_to_le16(0x0110),
190 static struct usb_cdc_union_desc ncm_union_desc = {
191 .bLength = sizeof(ncm_union_desc),
192 .bDescriptorType = USB_DT_CS_INTERFACE,
193 .bDescriptorSubType = USB_CDC_UNION_TYPE,
194 /* .bMasterInterface0 = DYNAMIC */
195 /* .bSlaveInterface0 = DYNAMIC */
198 static struct usb_cdc_ether_desc ecm_desc = {
199 .bLength = sizeof ecm_desc,
200 .bDescriptorType = USB_DT_CS_INTERFACE,
201 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
203 /* this descriptor actually adds value, surprise! */
204 /* .iMACAddress = DYNAMIC */
205 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
206 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
207 .wNumberMCFilters = cpu_to_le16(0),
208 .bNumberPowerFilters = 0,
211 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
213 static struct usb_cdc_ncm_desc ncm_desc = {
214 .bLength = sizeof ncm_desc,
215 .bDescriptorType = USB_DT_CS_INTERFACE,
216 .bDescriptorSubType = USB_CDC_NCM_TYPE,
218 .bcdNcmVersion = cpu_to_le16(0x0100),
219 /* can process SetEthernetPacketFilter */
220 .bmNetworkCapabilities = NCAPS,
223 /* the default data interface has no endpoints ... */
225 static struct usb_interface_descriptor ncm_data_nop_intf = {
226 .bLength = sizeof ncm_data_nop_intf,
227 .bDescriptorType = USB_DT_INTERFACE,
229 .bInterfaceNumber = 1,
230 .bAlternateSetting = 0,
232 .bInterfaceClass = USB_CLASS_CDC_DATA,
233 .bInterfaceSubClass = 0,
234 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
235 /* .iInterface = DYNAMIC */
238 /* ... but the "real" data interface has two bulk endpoints */
240 static struct usb_interface_descriptor ncm_data_intf = {
241 .bLength = sizeof ncm_data_intf,
242 .bDescriptorType = USB_DT_INTERFACE,
244 .bInterfaceNumber = 1,
245 .bAlternateSetting = 1,
247 .bInterfaceClass = USB_CLASS_CDC_DATA,
248 .bInterfaceSubClass = 0,
249 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
250 /* .iInterface = DYNAMIC */
253 /* full speed support: */
255 static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
259 .bEndpointAddress = USB_DIR_IN,
260 .bmAttributes = USB_ENDPOINT_XFER_INT,
261 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
262 .bInterval = NCM_STATUS_INTERVAL_MS,
265 static struct usb_endpoint_descriptor fs_ncm_in_desc = {
266 .bLength = USB_DT_ENDPOINT_SIZE,
267 .bDescriptorType = USB_DT_ENDPOINT,
269 .bEndpointAddress = USB_DIR_IN,
270 .bmAttributes = USB_ENDPOINT_XFER_BULK,
273 static struct usb_endpoint_descriptor fs_ncm_out_desc = {
274 .bLength = USB_DT_ENDPOINT_SIZE,
275 .bDescriptorType = USB_DT_ENDPOINT,
277 .bEndpointAddress = USB_DIR_OUT,
278 .bmAttributes = USB_ENDPOINT_XFER_BULK,
281 static struct usb_descriptor_header *ncm_fs_function[] = {
282 (struct usb_descriptor_header *) &ncm_iad_desc,
283 /* CDC NCM control descriptors */
284 (struct usb_descriptor_header *) &ncm_control_intf,
285 (struct usb_descriptor_header *) &ncm_header_desc,
286 (struct usb_descriptor_header *) &ncm_union_desc,
287 (struct usb_descriptor_header *) &ecm_desc,
288 (struct usb_descriptor_header *) &ncm_desc,
289 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
290 /* data interface, altsettings 0 and 1 */
291 (struct usb_descriptor_header *) &ncm_data_nop_intf,
292 (struct usb_descriptor_header *) &ncm_data_intf,
293 (struct usb_descriptor_header *) &fs_ncm_in_desc,
294 (struct usb_descriptor_header *) &fs_ncm_out_desc,
298 /* high speed support: */
300 static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
301 .bLength = USB_DT_ENDPOINT_SIZE,
302 .bDescriptorType = USB_DT_ENDPOINT,
304 .bEndpointAddress = USB_DIR_IN,
305 .bmAttributes = USB_ENDPOINT_XFER_INT,
306 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
307 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
309 static struct usb_endpoint_descriptor hs_ncm_in_desc = {
310 .bLength = USB_DT_ENDPOINT_SIZE,
311 .bDescriptorType = USB_DT_ENDPOINT,
313 .bEndpointAddress = USB_DIR_IN,
314 .bmAttributes = USB_ENDPOINT_XFER_BULK,
315 .wMaxPacketSize = cpu_to_le16(512),
318 static struct usb_endpoint_descriptor hs_ncm_out_desc = {
319 .bLength = USB_DT_ENDPOINT_SIZE,
320 .bDescriptorType = USB_DT_ENDPOINT,
322 .bEndpointAddress = USB_DIR_OUT,
323 .bmAttributes = USB_ENDPOINT_XFER_BULK,
324 .wMaxPacketSize = cpu_to_le16(512),
327 static struct usb_descriptor_header *ncm_hs_function[] = {
328 (struct usb_descriptor_header *) &ncm_iad_desc,
329 /* CDC NCM control descriptors */
330 (struct usb_descriptor_header *) &ncm_control_intf,
331 (struct usb_descriptor_header *) &ncm_header_desc,
332 (struct usb_descriptor_header *) &ncm_union_desc,
333 (struct usb_descriptor_header *) &ecm_desc,
334 (struct usb_descriptor_header *) &ncm_desc,
335 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
336 /* data interface, altsettings 0 and 1 */
337 (struct usb_descriptor_header *) &ncm_data_nop_intf,
338 (struct usb_descriptor_header *) &ncm_data_intf,
339 (struct usb_descriptor_header *) &hs_ncm_in_desc,
340 (struct usb_descriptor_header *) &hs_ncm_out_desc,
345 /* super speed support: */
347 static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
348 .bLength = USB_DT_ENDPOINT_SIZE,
349 .bDescriptorType = USB_DT_ENDPOINT,
351 .bEndpointAddress = USB_DIR_IN,
352 .bmAttributes = USB_ENDPOINT_XFER_INT,
353 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
354 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
357 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
358 .bLength = sizeof(ss_ncm_notify_comp_desc),
359 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
361 /* the following 3 values can be tweaked if necessary */
362 /* .bMaxBurst = 0, */
363 /* .bmAttributes = 0, */
364 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT),
367 static struct usb_endpoint_descriptor ss_ncm_in_desc = {
368 .bLength = USB_DT_ENDPOINT_SIZE,
369 .bDescriptorType = USB_DT_ENDPOINT,
371 .bEndpointAddress = USB_DIR_IN,
372 .bmAttributes = USB_ENDPOINT_XFER_BULK,
373 .wMaxPacketSize = cpu_to_le16(1024),
376 static struct usb_endpoint_descriptor ss_ncm_out_desc = {
377 .bLength = USB_DT_ENDPOINT_SIZE,
378 .bDescriptorType = USB_DT_ENDPOINT,
380 .bEndpointAddress = USB_DIR_OUT,
381 .bmAttributes = USB_ENDPOINT_XFER_BULK,
382 .wMaxPacketSize = cpu_to_le16(1024),
385 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
386 .bLength = sizeof(ss_ncm_bulk_comp_desc),
387 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
389 /* the following 2 values can be tweaked if necessary */
390 /* .bMaxBurst = 0, */
391 /* .bmAttributes = 0, */
394 static struct usb_descriptor_header *ncm_ss_function[] = {
395 (struct usb_descriptor_header *) &ncm_iad_desc,
396 /* CDC NCM control descriptors */
397 (struct usb_descriptor_header *) &ncm_control_intf,
398 (struct usb_descriptor_header *) &ncm_header_desc,
399 (struct usb_descriptor_header *) &ncm_union_desc,
400 (struct usb_descriptor_header *) &ecm_desc,
401 (struct usb_descriptor_header *) &ncm_desc,
402 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
403 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
404 /* data interface, altsettings 0 and 1 */
405 (struct usb_descriptor_header *) &ncm_data_nop_intf,
406 (struct usb_descriptor_header *) &ncm_data_intf,
407 (struct usb_descriptor_header *) &ss_ncm_in_desc,
408 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
409 (struct usb_descriptor_header *) &ss_ncm_out_desc,
410 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
414 /* string descriptors: */
416 #define STRING_CTRL_IDX 0
417 #define STRING_MAC_IDX 1
418 #define STRING_DATA_IDX 2
419 #define STRING_IAD_IDX 3
421 static struct usb_string ncm_string_defs[] = {
422 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
423 [STRING_MAC_IDX].s = "",
424 [STRING_DATA_IDX].s = "CDC Network Data",
425 [STRING_IAD_IDX].s = "CDC NCM",
426 { } /* end of list */
429 static struct usb_gadget_strings ncm_string_table = {
430 .language = 0x0409, /* en-us */
431 .strings = ncm_string_defs,
434 static struct usb_gadget_strings *ncm_strings[] = {
440 * Here are options for NCM Datagram Pointer table (NDP) parser.
441 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
442 * in NDP16 offsets and sizes fields are 1 16bit word wide,
443 * in NDP32 -- 2 16bit words wide. Also signatures are different.
444 * To make the parser code the same, put the differences in the structure,
445 * and switch pointers to the structures when the format is changed.
448 struct ndp_parser_opts {
454 unsigned ndplen_align;
455 /* sizes in u16 units */
456 unsigned dgram_item_len; /* index or length */
457 unsigned block_length;
461 unsigned next_ndp_index;
464 #define INIT_NDP16_OPTS { \
465 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
466 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
467 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
468 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
469 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
471 .dgram_item_len = 1, \
476 .next_ndp_index = 1, \
480 #define INIT_NDP32_OPTS { \
481 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
482 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
483 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
484 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
485 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
487 .dgram_item_len = 2, \
492 .next_ndp_index = 2, \
495 static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
496 static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
498 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
502 put_unaligned_le16((u16)val, *p);
505 put_unaligned_le32((u32)val, *p);
515 static inline unsigned get_ncm(__le16 **p, unsigned size)
521 tmp = get_unaligned_le16(*p);
524 tmp = get_unaligned_le32(*p);
534 /*-------------------------------------------------------------------------*/
536 static inline void ncm_reset_values(struct f_ncm *ncm)
538 ncm->parser_opts = &ndp16_opts;
540 ncm->port.cdc_filter = DEFAULT_FILTER;
542 /* doesn't make sense for ncm, fixed size used */
543 ncm->port.header_len = 0;
545 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
546 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
550 * Context: ncm->lock held
552 static void ncm_do_notify(struct f_ncm *ncm)
554 struct usb_request *req = ncm->notify_req;
555 struct usb_cdc_notification *event;
556 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
560 /* notification already in flight? */
561 if (atomic_read(&ncm->notify_count))
565 switch (ncm->notify_state) {
566 case NCM_NOTIFY_NONE:
569 case NCM_NOTIFY_CONNECT:
570 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
572 event->wValue = cpu_to_le16(1);
574 event->wValue = cpu_to_le16(0);
576 req->length = sizeof *event;
578 DBG(cdev, "notify connect %s\n",
579 ncm->is_open ? "true" : "false");
580 ncm->notify_state = NCM_NOTIFY_NONE;
583 case NCM_NOTIFY_SPEED:
584 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
585 event->wValue = cpu_to_le16(0);
586 event->wLength = cpu_to_le16(8);
587 req->length = NCM_STATUS_BYTECOUNT;
589 /* SPEED_CHANGE data is up/down speeds in bits/sec */
590 data = req->buf + sizeof *event;
591 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
594 DBG(cdev, "notify speed %u\n", ncm_bitrate(cdev->gadget));
595 ncm->notify_state = NCM_NOTIFY_CONNECT;
598 event->bmRequestType = 0xA1;
599 event->wIndex = cpu_to_le16(ncm->ctrl_id);
601 atomic_inc(&ncm->notify_count);
604 * In double buffering if there is a space in FIFO,
605 * completion callback can be called right after the call,
608 spin_unlock(&ncm->lock);
609 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
610 spin_lock(&ncm->lock);
612 atomic_dec(&ncm->notify_count);
613 DBG(cdev, "notify --> %d\n", status);
618 * Context: ncm->lock held
620 static void ncm_notify(struct f_ncm *ncm)
623 * NOTE on most versions of Linux, host side cdc-ethernet
624 * won't listen for notifications until its netdevice opens.
625 * The first notification then sits in the FIFO for a long
626 * time, and the second one is queued.
628 * If ncm_notify() is called before the second (CONNECT)
629 * notification is sent, then it will reset to send the SPEED
630 * notificaion again (and again, and again), but it's not a problem
632 ncm->notify_state = NCM_NOTIFY_SPEED;
636 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
638 struct f_ncm *ncm = req->context;
639 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
640 struct usb_cdc_notification *event = req->buf;
642 spin_lock(&ncm->lock);
643 switch (req->status) {
645 VDBG(cdev, "Notification %02x sent\n",
646 event->bNotificationType);
647 atomic_dec(&ncm->notify_count);
651 atomic_set(&ncm->notify_count, 0);
652 ncm->notify_state = NCM_NOTIFY_NONE;
655 DBG(cdev, "event %02x --> %d\n",
656 event->bNotificationType, req->status);
657 atomic_dec(&ncm->notify_count);
661 spin_unlock(&ncm->lock);
664 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
666 /* now for SET_NTB_INPUT_SIZE only */
668 struct usb_function *f = req->context;
669 struct f_ncm *ncm = func_to_ncm(f);
670 struct usb_composite_dev *cdev = f->config->cdev;
673 if (req->status || req->actual != req->length) {
674 DBG(cdev, "Bad control-OUT transfer\n");
678 in_size = get_unaligned_le32(req->buf);
679 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
680 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
681 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
685 ncm->port.fixed_in_len = in_size;
686 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
694 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
696 struct f_ncm *ncm = func_to_ncm(f);
697 struct usb_composite_dev *cdev = f->config->cdev;
698 struct usb_request *req = cdev->req;
699 int value = -EOPNOTSUPP;
700 u16 w_index = le16_to_cpu(ctrl->wIndex);
701 u16 w_value = le16_to_cpu(ctrl->wValue);
702 u16 w_length = le16_to_cpu(ctrl->wLength);
705 * composite driver infrastructure handles everything except
706 * CDC class messages; interface activation uses set_alt().
708 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
709 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
710 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
712 * see 6.2.30: no data, wIndex = interface,
713 * wValue = packet filter bitmap
715 if (w_length != 0 || w_index != ncm->ctrl_id)
717 DBG(cdev, "packet filter %02x\n", w_value);
719 * REVISIT locking of cdc_filter. This assumes the UDC
720 * driver won't have a concurrent packet TX irq running on
721 * another CPU; or that if it does, this write is atomic...
723 ncm->port.cdc_filter = w_value;
728 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
729 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
730 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
731 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
732 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
733 * case USB_CDC_GET_ETHERNET_STATISTIC:
736 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
737 | USB_CDC_GET_NTB_PARAMETERS:
739 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
741 value = w_length > sizeof ntb_parameters ?
742 sizeof ntb_parameters : w_length;
743 memcpy(req->buf, &ntb_parameters, value);
744 VDBG(cdev, "Host asked NTB parameters\n");
747 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
748 | USB_CDC_GET_NTB_INPUT_SIZE:
750 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
752 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
754 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
755 ncm->port.fixed_in_len);
758 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
759 | USB_CDC_SET_NTB_INPUT_SIZE:
761 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
763 req->complete = ncm_ep0out_complete;
764 req->length = w_length;
771 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
772 | USB_CDC_GET_NTB_FORMAT:
776 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
778 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
779 put_unaligned_le16(format, req->buf);
781 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
785 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
786 | USB_CDC_SET_NTB_FORMAT:
788 if (w_length != 0 || w_index != ncm->ctrl_id)
792 ncm->parser_opts = &ndp16_opts;
793 DBG(cdev, "NCM16 selected\n");
796 ncm->parser_opts = &ndp32_opts;
797 DBG(cdev, "NCM32 selected\n");
805 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
806 | USB_CDC_GET_CRC_MODE:
810 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
812 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
813 put_unaligned_le16(is_crc, req->buf);
815 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
819 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
820 | USB_CDC_SET_CRC_MODE:
824 if (w_length != 0 || w_index != ncm->ctrl_id)
829 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
830 DBG(cdev, "non-CRC mode selected\n");
834 ndp_hdr_crc = NCM_NDP_HDR_CRC;
835 DBG(cdev, "CRC mode selected\n");
840 ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
845 /* and disabled in ncm descriptor: */
846 /* case USB_CDC_GET_NET_ADDRESS: */
847 /* case USB_CDC_SET_NET_ADDRESS: */
848 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
849 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
853 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
854 ctrl->bRequestType, ctrl->bRequest,
855 w_value, w_index, w_length);
858 /* respond with data transfer or status phase? */
860 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
861 ctrl->bRequestType, ctrl->bRequest,
862 w_value, w_index, w_length);
865 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
867 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
868 ctrl->bRequestType, ctrl->bRequest,
872 /* device either stalls (value < 0) or reports success */
877 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
879 struct f_ncm *ncm = func_to_ncm(f);
880 struct usb_composite_dev *cdev = f->config->cdev;
882 /* Control interface has only altsetting 0 */
883 if (intf == ncm->ctrl_id) {
887 DBG(cdev, "reset ncm control %d\n", intf);
888 usb_ep_disable(ncm->notify);
890 if (!(ncm->notify->desc)) {
891 DBG(cdev, "init ncm ctrl %d\n", intf);
892 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
895 usb_ep_enable(ncm->notify);
897 /* Data interface has two altsettings, 0 and 1 */
898 } else if (intf == ncm->data_id) {
902 if (ncm->port.in_ep->enabled) {
903 DBG(cdev, "reset ncm\n");
904 ncm->timer_stopping = true;
906 gether_disconnect(&ncm->port);
907 ncm_reset_values(ncm);
911 * CDC Network only sends data in non-default altsettings.
912 * Changing altsettings resets filters, statistics, etc.
915 struct net_device *net;
917 if (!ncm->port.in_ep->desc ||
918 !ncm->port.out_ep->desc) {
919 DBG(cdev, "init ncm\n");
920 if (config_ep_by_speed(cdev->gadget, f,
922 config_ep_by_speed(cdev->gadget, f,
924 ncm->port.in_ep->desc = NULL;
925 ncm->port.out_ep->desc = NULL;
931 /* Enable zlps by default for NCM conformance;
932 * override for musb_hdrc (avoids txdma ovhead)
934 ncm->port.is_zlp_ok =
935 gadget_is_zlp_supported(cdev->gadget);
936 ncm->port.cdc_filter = DEFAULT_FILTER;
937 DBG(cdev, "activate ncm\n");
938 net = gether_connect(&ncm->port);
942 ncm->timer_stopping = false;
945 spin_lock(&ncm->lock);
947 spin_unlock(&ncm->lock);
957 * Because the data interface supports multiple altsettings,
958 * this NCM function *MUST* implement a get_alt() method.
960 static int ncm_get_alt(struct usb_function *f, unsigned intf)
962 struct f_ncm *ncm = func_to_ncm(f);
964 if (intf == ncm->ctrl_id)
966 return ncm->port.in_ep->enabled ? 1 : 0;
969 static struct sk_buff *package_for_tx(struct f_ncm *ncm)
972 struct sk_buff *skb2 = NULL;
977 const struct ndp_parser_opts *opts = ncm->parser_opts;
978 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
979 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
982 hrtimer_try_to_cancel(&ncm->task_timer);
984 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
985 ncm->skb_tx_data->len;
986 ndp_index = ncm->skb_tx_data->len + ndp_pad;
987 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
989 /* Set the final BlockLength and wNdpIndex */
990 ntb_iter = (void *) ncm->skb_tx_data->data;
991 /* Increment pointer to BlockLength */
992 ntb_iter += 2 + 1 + 1;
993 put_ncm(&ntb_iter, opts->block_length, new_len);
994 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
996 /* Set the final NDP wLength */
997 new_len = opts->ndp_size +
998 (ncm->ndp_dgram_count * dgram_idx_len);
999 ncm->ndp_dgram_count = 0;
1000 /* Increment from start to wLength */
1001 ntb_iter = (void *) ncm->skb_tx_ndp->data;
1003 put_unaligned_le16(new_len, ntb_iter);
1005 /* Merge the skbs */
1006 swap(skb2, ncm->skb_tx_data);
1007 if (ncm->skb_tx_data) {
1008 dev_consume_skb_any(ncm->skb_tx_data);
1009 ncm->skb_tx_data = NULL;
1012 /* Insert NDP alignment. */
1013 skb_put_zero(skb2, ndp_pad);
1015 /* Copy NTB across. */
1016 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
1017 dev_consume_skb_any(ncm->skb_tx_ndp);
1018 ncm->skb_tx_ndp = NULL;
1020 /* Insert zero'd datagram. */
1021 skb_put_zero(skb2, dgram_idx_len);
1026 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1027 struct sk_buff *skb)
1029 struct f_ncm *ncm = func_to_ncm(&port->func);
1030 struct sk_buff *skb2 = NULL;
1036 unsigned max_size = ncm->port.fixed_in_len;
1037 const struct ndp_parser_opts *opts = ncm->parser_opts;
1038 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1039 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1040 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1041 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
1043 if (!skb && !ncm->skb_tx_data)
1047 /* Add the CRC if required up front */
1055 crc_pos = skb_put(skb, sizeof(uint32_t));
1056 put_unaligned_le32(crc, crc_pos);
1059 /* If the new skb is too big for the current NCM NTB then
1060 * set the current stored skb to be sent now and clear it
1061 * ready for new data.
1062 * NOTE: Assume maximum align for speed of calculation.
1064 if (ncm->skb_tx_data
1065 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1066 || (ncm->skb_tx_data->len +
1067 div + rem + skb->len +
1068 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1070 skb2 = package_for_tx(ncm);
1075 if (!ncm->skb_tx_data) {
1076 ncb_len = opts->nth_size;
1077 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1078 ncb_len += dgram_pad;
1080 /* Create a new skb for the NTH and datagrams. */
1081 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1082 if (!ncm->skb_tx_data)
1085 ncm->skb_tx_data->dev = ncm->netdev;
1086 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len);
1088 put_unaligned_le32(opts->nth_sign, ntb_data);
1091 put_unaligned_le16(opts->nth_size, ntb_data++);
1093 /* Allocate an skb for storing the NDP,
1094 * TX_MAX_NUM_DPE should easily suffice for a
1097 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1101 if (!ncm->skb_tx_ndp)
1104 ncm->skb_tx_ndp->dev = ncm->netdev;
1105 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size);
1106 memset(ntb_ndp, 0, ncb_len);
1108 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1111 /* There is always a zeroed entry */
1112 ncm->ndp_dgram_count = 1;
1114 /* Note: we skip opts->next_ndp_index */
1117 /* Delay the timer. */
1118 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS,
1121 /* Add the datagram position entries */
1122 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len);
1124 ncb_len = ncm->skb_tx_data->len;
1125 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1126 ncb_len += dgram_pad;
1128 /* (d)wDatagramIndex */
1129 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1130 /* (d)wDatagramLength */
1131 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1132 ncm->ndp_dgram_count++;
1134 /* Add the new data to the skb */
1135 skb_put_zero(ncm->skb_tx_data, dgram_pad);
1136 skb_put_data(ncm->skb_tx_data, skb->data, skb->len);
1137 dev_consume_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 unsigned char *ntb_ptr = skb->data;
1206 unsigned index, index2;
1208 unsigned dg_len, dg_len2;
1211 struct sk_buff *skb2;
1213 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1214 unsigned frame_max = le16_to_cpu(ecm_desc.wMaxSegmentSize);
1215 const struct ndp_parser_opts *opts = ncm->parser_opts;
1216 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1218 int to_process = skb->len;
1221 tmp = (__le16 *)ntb_ptr;
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 = (__le16 *)(ntb_ptr + 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(ntb_ptr +
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 skb_put_data(skb2, ntb_ptr + index,
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 VDBG(port->func.config->cdev,
1370 "Parsed NTB with %d frames\n", dgram_counter);
1372 to_process -= block_len;
1373 if (to_process != 0) {
1374 ntb_ptr = (unsigned char *)(ntb_ptr + block_len);
1378 dev_consume_skb_any(skb);
1382 skb_queue_purge(list);
1383 dev_kfree_skb_any(skb);
1387 static void ncm_disable(struct usb_function *f)
1389 struct f_ncm *ncm = func_to_ncm(f);
1390 struct usb_composite_dev *cdev = f->config->cdev;
1392 DBG(cdev, "ncm deactivated\n");
1394 if (ncm->port.in_ep->enabled) {
1395 ncm->timer_stopping = true;
1397 gether_disconnect(&ncm->port);
1400 if (ncm->notify->enabled) {
1401 usb_ep_disable(ncm->notify);
1402 ncm->notify->desc = NULL;
1406 /*-------------------------------------------------------------------------*/
1409 * Callbacks let us notify the host about connect/disconnect when the
1410 * net device is opened or closed.
1412 * For testing, note that link states on this side include both opened
1413 * and closed variants of:
1415 * - disconnected/unconfigured
1416 * - configured but inactive (data alt 0)
1417 * - configured and active (data alt 1)
1419 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1420 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1421 * imply the host is actually polling the notification endpoint, and
1422 * likewise that "active" doesn't imply it's actually using the data
1423 * endpoints for traffic.
1426 static void ncm_open(struct gether *geth)
1428 struct f_ncm *ncm = func_to_ncm(&geth->func);
1430 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1432 spin_lock(&ncm->lock);
1433 ncm->is_open = true;
1435 spin_unlock(&ncm->lock);
1438 static void ncm_close(struct gether *geth)
1440 struct f_ncm *ncm = func_to_ncm(&geth->func);
1442 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1444 spin_lock(&ncm->lock);
1445 ncm->is_open = false;
1447 spin_unlock(&ncm->lock);
1450 /*-------------------------------------------------------------------------*/
1452 /* ethernet function driver setup/binding */
1454 static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1456 struct usb_composite_dev *cdev = c->cdev;
1457 struct f_ncm *ncm = func_to_ncm(f);
1458 struct usb_string *us;
1461 struct f_ncm_opts *ncm_opts;
1463 if (!can_support_ecm(cdev->gadget))
1466 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1468 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1469 * configurations are bound in sequence with list_for_each_entry,
1470 * in each configuration its functions are bound in sequence
1471 * with list_for_each_entry, so we assume no race condition
1472 * with regard to ncm_opts->bound access
1474 if (!ncm_opts->bound) {
1475 mutex_lock(&ncm_opts->lock);
1476 gether_set_gadget(ncm_opts->net, cdev->gadget);
1477 status = gether_register_netdev(ncm_opts->net);
1478 mutex_unlock(&ncm_opts->lock);
1481 ncm_opts->bound = true;
1483 us = usb_gstrings_attach(cdev, ncm_strings,
1484 ARRAY_SIZE(ncm_string_defs));
1487 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1488 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1489 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1490 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1491 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1493 /* allocate instance-specific interface IDs */
1494 status = usb_interface_id(c, f);
1497 ncm->ctrl_id = status;
1498 ncm_iad_desc.bFirstInterface = status;
1500 ncm_control_intf.bInterfaceNumber = status;
1501 ncm_union_desc.bMasterInterface0 = status;
1503 status = usb_interface_id(c, f);
1506 ncm->data_id = status;
1508 ncm_data_nop_intf.bInterfaceNumber = status;
1509 ncm_data_intf.bInterfaceNumber = status;
1510 ncm_union_desc.bSlaveInterface0 = status;
1514 /* allocate instance-specific endpoints */
1515 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1518 ncm->port.in_ep = ep;
1520 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1523 ncm->port.out_ep = ep;
1525 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1532 /* allocate notification request and buffer */
1533 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1534 if (!ncm->notify_req)
1536 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1537 if (!ncm->notify_req->buf)
1539 ncm->notify_req->context = ncm;
1540 ncm->notify_req->complete = ncm_notify_complete;
1543 * support all relevant hardware speeds... we expect that when
1544 * hardware is dual speed, all bulk-capable endpoints work at
1547 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1548 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1549 hs_ncm_notify_desc.bEndpointAddress =
1550 fs_ncm_notify_desc.bEndpointAddress;
1552 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1553 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1554 ss_ncm_notify_desc.bEndpointAddress =
1555 fs_ncm_notify_desc.bEndpointAddress;
1557 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1558 ncm_ss_function, ncm_ss_function);
1563 * NOTE: all that is done without knowing or caring about
1564 * the network link ... which is unavailable to this code
1565 * until we're activated via set_alt().
1568 ncm->port.open = ncm_open;
1569 ncm->port.close = ncm_close;
1571 tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
1572 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1573 ncm->task_timer.function = ncm_tx_timeout;
1575 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1576 gadget_is_superspeed(c->cdev->gadget) ? "super" :
1577 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1578 ncm->port.in_ep->name, ncm->port.out_ep->name,
1583 if (ncm->notify_req) {
1584 kfree(ncm->notify_req->buf);
1585 usb_ep_free_request(ncm->notify, ncm->notify_req);
1588 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1593 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1595 return container_of(to_config_group(item), struct f_ncm_opts,
1599 /* f_ncm_item_ops */
1600 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1602 /* f_ncm_opts_dev_addr */
1603 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1605 /* f_ncm_opts_host_addr */
1606 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1608 /* f_ncm_opts_qmult */
1609 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1611 /* f_ncm_opts_ifname */
1612 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1614 static struct configfs_attribute *ncm_attrs[] = {
1615 &ncm_opts_attr_dev_addr,
1616 &ncm_opts_attr_host_addr,
1617 &ncm_opts_attr_qmult,
1618 &ncm_opts_attr_ifname,
1622 static struct config_item_type ncm_func_type = {
1623 .ct_item_ops = &ncm_item_ops,
1624 .ct_attrs = ncm_attrs,
1625 .ct_owner = THIS_MODULE,
1628 static void ncm_free_inst(struct usb_function_instance *f)
1630 struct f_ncm_opts *opts;
1632 opts = container_of(f, struct f_ncm_opts, func_inst);
1634 gether_cleanup(netdev_priv(opts->net));
1636 free_netdev(opts->net);
1640 static struct usb_function_instance *ncm_alloc_inst(void)
1642 struct f_ncm_opts *opts;
1644 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1646 return ERR_PTR(-ENOMEM);
1647 mutex_init(&opts->lock);
1648 opts->func_inst.free_func_inst = ncm_free_inst;
1649 opts->net = gether_setup_default();
1650 if (IS_ERR(opts->net)) {
1651 struct net_device *net = opts->net;
1653 return ERR_CAST(net);
1656 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1658 return &opts->func_inst;
1661 static void ncm_free(struct usb_function *f)
1664 struct f_ncm_opts *opts;
1666 ncm = func_to_ncm(f);
1667 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1669 mutex_lock(&opts->lock);
1671 mutex_unlock(&opts->lock);
1674 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1676 struct f_ncm *ncm = func_to_ncm(f);
1678 DBG(c->cdev, "ncm unbind\n");
1680 hrtimer_cancel(&ncm->task_timer);
1681 tasklet_kill(&ncm->tx_tasklet);
1683 ncm_string_defs[0].id = 0;
1684 usb_free_all_descriptors(f);
1686 if (atomic_read(&ncm->notify_count)) {
1687 usb_ep_dequeue(ncm->notify, ncm->notify_req);
1688 atomic_set(&ncm->notify_count, 0);
1691 kfree(ncm->notify_req->buf);
1692 usb_ep_free_request(ncm->notify, ncm->notify_req);
1695 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1698 struct f_ncm_opts *opts;
1701 /* allocate and initialize one new instance */
1702 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1704 return ERR_PTR(-ENOMEM);
1706 opts = container_of(fi, struct f_ncm_opts, func_inst);
1707 mutex_lock(&opts->lock);
1710 /* export host's Ethernet address in CDC format */
1711 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1712 sizeof(ncm->ethaddr));
1713 if (status < 12) { /* strlen("01234567890a") */
1715 mutex_unlock(&opts->lock);
1716 return ERR_PTR(-EINVAL);
1718 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1720 spin_lock_init(&ncm->lock);
1721 ncm_reset_values(ncm);
1722 ncm->port.ioport = netdev_priv(opts->net);
1723 mutex_unlock(&opts->lock);
1724 ncm->port.is_fixed = true;
1725 ncm->port.supports_multi_frame = true;
1727 ncm->port.func.name = "cdc_network";
1728 /* descriptors are per-instance copies */
1729 ncm->port.func.bind = ncm_bind;
1730 ncm->port.func.unbind = ncm_unbind;
1731 ncm->port.func.set_alt = ncm_set_alt;
1732 ncm->port.func.get_alt = ncm_get_alt;
1733 ncm->port.func.setup = ncm_setup;
1734 ncm->port.func.disable = ncm_disable;
1735 ncm->port.func.free_func = ncm_free;
1737 ncm->port.wrap = ncm_wrap_ntb;
1738 ncm->port.unwrap = ncm_unwrap_ntb;
1740 return &ncm->port.func;
1743 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1744 MODULE_LICENSE("GPL");
1745 MODULE_AUTHOR("Yauheni Kaliuta");