x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / usb / gadget / function / f_ncm.c
blob5780fba620ab13014369ec0304910d9ee317e1bf
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * f_ncm.c -- USB CDC Network (NCM) link function driver
5 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
8 * The driver borrows from f_ecm.c which is:
10 * Copyright (C) 2003-2005,2008 David Brownell
11 * Copyright (C) 2008 Nokia Corporation
14 #include <linux/kernel.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/etherdevice.h>
19 #include <linux/crc32.h>
21 #include <linux/usb/cdc.h>
23 #include "u_ether.h"
24 #include "u_ether_configfs.h"
25 #include "u_ncm.h"
28 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
29 * NCM is intended to be used with high-speed network attachments.
31 * Note that NCM requires the use of "alternate settings" for its data
32 * interface. This means that the set_alt() method has real work to do,
33 * and also means that a get_alt() method is required.
36 /* to trigger crc/non-crc ndp signature */
38 #define NCM_NDP_HDR_CRC_MASK 0x01000000
39 #define NCM_NDP_HDR_CRC 0x01000000
40 #define NCM_NDP_HDR_NOCRC 0x00000000
42 enum ncm_notify_state {
43 NCM_NOTIFY_NONE, /* don't notify */
44 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
45 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
48 struct f_ncm {
49 struct gether port;
50 u8 ctrl_id, data_id;
52 char ethaddr[14];
54 struct usb_ep *notify;
55 struct usb_request *notify_req;
56 u8 notify_state;
57 bool is_open;
59 const struct ndp_parser_opts *parser_opts;
60 bool is_crc;
61 u32 ndp_sign;
64 * for notification, it is accessed from both
65 * callback and ethernet open/close
67 spinlock_t lock;
69 struct net_device *netdev;
71 /* For multi-frame NDP TX */
72 struct sk_buff *skb_tx_data;
73 struct sk_buff *skb_tx_ndp;
74 u16 ndp_dgram_count;
75 bool timer_force_tx;
76 struct hrtimer task_timer;
77 bool timer_stopping;
80 static inline struct f_ncm *func_to_ncm(struct usb_function *f)
82 return container_of(f, struct f_ncm, port.func);
85 /* peak (theoretical) bulk transfer rate in bits-per-second */
86 static inline unsigned ncm_bitrate(struct usb_gadget *g)
88 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
89 return 13 * 1024 * 8 * 1000 * 8;
90 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
91 return 13 * 512 * 8 * 1000 * 8;
92 else
93 return 19 * 64 * 1 * 1000 * 8;
96 /*-------------------------------------------------------------------------*/
99 * We cannot group frames so use just the minimal size which ok to put
100 * one max-size ethernet frame.
101 * If the host can group frames, allow it to do that, 16K is selected,
102 * because it's used by default by the current linux host driver
104 #define NTB_DEFAULT_IN_SIZE 16384
105 #define NTB_OUT_SIZE 16384
107 /* Allocation for storing the NDP, 32 should suffice for a
108 * 16k packet. This allows a maximum of 32 * 507 Byte packets to
109 * be transmitted in a single 16kB skb, though when sending full size
110 * packets this limit will be plenty.
111 * Smaller packets are not likely to be trying to maximize the
112 * throughput and will be mstly sending smaller infrequent frames.
114 #define TX_MAX_NUM_DPE 32
116 /* Delay for the transmit to wait before sending an unfilled NTB frame. */
117 #define TX_TIMEOUT_NSECS 300000
119 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
120 USB_CDC_NCM_NTB32_SUPPORTED)
122 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
123 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
124 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
125 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
126 .wNdpInDivisor = cpu_to_le16(4),
127 .wNdpInPayloadRemainder = cpu_to_le16(0),
128 .wNdpInAlignment = cpu_to_le16(4),
130 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
131 .wNdpOutDivisor = cpu_to_le16(4),
132 .wNdpOutPayloadRemainder = cpu_to_le16(0),
133 .wNdpOutAlignment = cpu_to_le16(4),
137 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
138 * packet, to simplify cancellation; and a big transfer interval, to
139 * waste less bandwidth.
142 #define NCM_STATUS_INTERVAL_MS 32
143 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
145 static struct usb_interface_assoc_descriptor ncm_iad_desc = {
146 .bLength = sizeof ncm_iad_desc,
147 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
149 /* .bFirstInterface = DYNAMIC, */
150 .bInterfaceCount = 2, /* control + data */
151 .bFunctionClass = USB_CLASS_COMM,
152 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
153 .bFunctionProtocol = USB_CDC_PROTO_NONE,
154 /* .iFunction = DYNAMIC */
157 /* interface descriptor: */
159 static struct usb_interface_descriptor ncm_control_intf = {
160 .bLength = sizeof ncm_control_intf,
161 .bDescriptorType = USB_DT_INTERFACE,
163 /* .bInterfaceNumber = DYNAMIC */
164 .bNumEndpoints = 1,
165 .bInterfaceClass = USB_CLASS_COMM,
166 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
167 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
168 /* .iInterface = DYNAMIC */
171 static struct usb_cdc_header_desc ncm_header_desc = {
172 .bLength = sizeof ncm_header_desc,
173 .bDescriptorType = USB_DT_CS_INTERFACE,
174 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
176 .bcdCDC = cpu_to_le16(0x0110),
179 static struct usb_cdc_union_desc ncm_union_desc = {
180 .bLength = sizeof(ncm_union_desc),
181 .bDescriptorType = USB_DT_CS_INTERFACE,
182 .bDescriptorSubType = USB_CDC_UNION_TYPE,
183 /* .bMasterInterface0 = DYNAMIC */
184 /* .bSlaveInterface0 = DYNAMIC */
187 static struct usb_cdc_ether_desc ecm_desc = {
188 .bLength = sizeof ecm_desc,
189 .bDescriptorType = USB_DT_CS_INTERFACE,
190 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
192 /* this descriptor actually adds value, surprise! */
193 /* .iMACAddress = DYNAMIC */
194 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
195 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
196 .wNumberMCFilters = cpu_to_le16(0),
197 .bNumberPowerFilters = 0,
200 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
202 static struct usb_cdc_ncm_desc ncm_desc = {
203 .bLength = sizeof ncm_desc,
204 .bDescriptorType = USB_DT_CS_INTERFACE,
205 .bDescriptorSubType = USB_CDC_NCM_TYPE,
207 .bcdNcmVersion = cpu_to_le16(0x0100),
208 /* can process SetEthernetPacketFilter */
209 .bmNetworkCapabilities = NCAPS,
212 /* the default data interface has no endpoints ... */
214 static struct usb_interface_descriptor ncm_data_nop_intf = {
215 .bLength = sizeof ncm_data_nop_intf,
216 .bDescriptorType = USB_DT_INTERFACE,
218 .bInterfaceNumber = 1,
219 .bAlternateSetting = 0,
220 .bNumEndpoints = 0,
221 .bInterfaceClass = USB_CLASS_CDC_DATA,
222 .bInterfaceSubClass = 0,
223 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
224 /* .iInterface = DYNAMIC */
227 /* ... but the "real" data interface has two bulk endpoints */
229 static struct usb_interface_descriptor ncm_data_intf = {
230 .bLength = sizeof ncm_data_intf,
231 .bDescriptorType = USB_DT_INTERFACE,
233 .bInterfaceNumber = 1,
234 .bAlternateSetting = 1,
235 .bNumEndpoints = 2,
236 .bInterfaceClass = USB_CLASS_CDC_DATA,
237 .bInterfaceSubClass = 0,
238 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
239 /* .iInterface = DYNAMIC */
242 /* full speed support: */
244 static struct usb_endpoint_descriptor fs_ncm_notify_desc = {
245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
248 .bEndpointAddress = USB_DIR_IN,
249 .bmAttributes = USB_ENDPOINT_XFER_INT,
250 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
251 .bInterval = NCM_STATUS_INTERVAL_MS,
254 static struct usb_endpoint_descriptor fs_ncm_in_desc = {
255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT,
258 .bEndpointAddress = USB_DIR_IN,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK,
262 static struct usb_endpoint_descriptor fs_ncm_out_desc = {
263 .bLength = USB_DT_ENDPOINT_SIZE,
264 .bDescriptorType = USB_DT_ENDPOINT,
266 .bEndpointAddress = USB_DIR_OUT,
267 .bmAttributes = USB_ENDPOINT_XFER_BULK,
270 static struct usb_descriptor_header *ncm_fs_function[] = {
271 (struct usb_descriptor_header *) &ncm_iad_desc,
272 /* CDC NCM control descriptors */
273 (struct usb_descriptor_header *) &ncm_control_intf,
274 (struct usb_descriptor_header *) &ncm_header_desc,
275 (struct usb_descriptor_header *) &ncm_union_desc,
276 (struct usb_descriptor_header *) &ecm_desc,
277 (struct usb_descriptor_header *) &ncm_desc,
278 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
279 /* data interface, altsettings 0 and 1 */
280 (struct usb_descriptor_header *) &ncm_data_nop_intf,
281 (struct usb_descriptor_header *) &ncm_data_intf,
282 (struct usb_descriptor_header *) &fs_ncm_in_desc,
283 (struct usb_descriptor_header *) &fs_ncm_out_desc,
284 NULL,
287 /* high speed support: */
289 static struct usb_endpoint_descriptor hs_ncm_notify_desc = {
290 .bLength = USB_DT_ENDPOINT_SIZE,
291 .bDescriptorType = USB_DT_ENDPOINT,
293 .bEndpointAddress = USB_DIR_IN,
294 .bmAttributes = USB_ENDPOINT_XFER_INT,
295 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
296 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
298 static struct usb_endpoint_descriptor hs_ncm_in_desc = {
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
302 .bEndpointAddress = USB_DIR_IN,
303 .bmAttributes = USB_ENDPOINT_XFER_BULK,
304 .wMaxPacketSize = cpu_to_le16(512),
307 static struct usb_endpoint_descriptor hs_ncm_out_desc = {
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
311 .bEndpointAddress = USB_DIR_OUT,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 .wMaxPacketSize = cpu_to_le16(512),
316 static struct usb_descriptor_header *ncm_hs_function[] = {
317 (struct usb_descriptor_header *) &ncm_iad_desc,
318 /* CDC NCM control descriptors */
319 (struct usb_descriptor_header *) &ncm_control_intf,
320 (struct usb_descriptor_header *) &ncm_header_desc,
321 (struct usb_descriptor_header *) &ncm_union_desc,
322 (struct usb_descriptor_header *) &ecm_desc,
323 (struct usb_descriptor_header *) &ncm_desc,
324 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
325 /* data interface, altsettings 0 and 1 */
326 (struct usb_descriptor_header *) &ncm_data_nop_intf,
327 (struct usb_descriptor_header *) &ncm_data_intf,
328 (struct usb_descriptor_header *) &hs_ncm_in_desc,
329 (struct usb_descriptor_header *) &hs_ncm_out_desc,
330 NULL,
334 /* super speed support: */
336 static struct usb_endpoint_descriptor ss_ncm_notify_desc = {
337 .bLength = USB_DT_ENDPOINT_SIZE,
338 .bDescriptorType = USB_DT_ENDPOINT,
340 .bEndpointAddress = USB_DIR_IN,
341 .bmAttributes = USB_ENDPOINT_XFER_INT,
342 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
343 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS)
346 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = {
347 .bLength = sizeof(ss_ncm_notify_comp_desc),
348 .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 ss_ncm_in_desc = {
357 .bLength = USB_DT_ENDPOINT_SIZE,
358 .bDescriptorType = USB_DT_ENDPOINT,
360 .bEndpointAddress = USB_DIR_IN,
361 .bmAttributes = USB_ENDPOINT_XFER_BULK,
362 .wMaxPacketSize = cpu_to_le16(1024),
365 static struct usb_endpoint_descriptor ss_ncm_out_desc = {
366 .bLength = USB_DT_ENDPOINT_SIZE,
367 .bDescriptorType = USB_DT_ENDPOINT,
369 .bEndpointAddress = USB_DIR_OUT,
370 .bmAttributes = USB_ENDPOINT_XFER_BULK,
371 .wMaxPacketSize = cpu_to_le16(1024),
374 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = {
375 .bLength = sizeof(ss_ncm_bulk_comp_desc),
376 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
378 /* the following 2 values can be tweaked if necessary */
379 /* .bMaxBurst = 0, */
380 /* .bmAttributes = 0, */
383 static struct usb_descriptor_header *ncm_ss_function[] = {
384 (struct usb_descriptor_header *) &ncm_iad_desc,
385 /* CDC NCM control descriptors */
386 (struct usb_descriptor_header *) &ncm_control_intf,
387 (struct usb_descriptor_header *) &ncm_header_desc,
388 (struct usb_descriptor_header *) &ncm_union_desc,
389 (struct usb_descriptor_header *) &ecm_desc,
390 (struct usb_descriptor_header *) &ncm_desc,
391 (struct usb_descriptor_header *) &ss_ncm_notify_desc,
392 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc,
393 /* data interface, altsettings 0 and 1 */
394 (struct usb_descriptor_header *) &ncm_data_nop_intf,
395 (struct usb_descriptor_header *) &ncm_data_intf,
396 (struct usb_descriptor_header *) &ss_ncm_in_desc,
397 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
398 (struct usb_descriptor_header *) &ss_ncm_out_desc,
399 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc,
400 NULL,
403 /* string descriptors: */
405 #define STRING_CTRL_IDX 0
406 #define STRING_MAC_IDX 1
407 #define STRING_DATA_IDX 2
408 #define STRING_IAD_IDX 3
410 static struct usb_string ncm_string_defs[] = {
411 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
412 [STRING_MAC_IDX].s = "",
413 [STRING_DATA_IDX].s = "CDC Network Data",
414 [STRING_IAD_IDX].s = "CDC NCM",
415 { } /* end of list */
418 static struct usb_gadget_strings ncm_string_table = {
419 .language = 0x0409, /* en-us */
420 .strings = ncm_string_defs,
423 static struct usb_gadget_strings *ncm_strings[] = {
424 &ncm_string_table,
425 NULL,
429 * Here are options for NCM Datagram Pointer table (NDP) parser.
430 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
431 * in NDP16 offsets and sizes fields are 1 16bit word wide,
432 * in NDP32 -- 2 16bit words wide. Also signatures are different.
433 * To make the parser code the same, put the differences in the structure,
434 * and switch pointers to the structures when the format is changed.
437 struct ndp_parser_opts {
438 u32 nth_sign;
439 u32 ndp_sign;
440 unsigned nth_size;
441 unsigned ndp_size;
442 unsigned dpe_size;
443 unsigned ndplen_align;
444 /* sizes in u16 units */
445 unsigned dgram_item_len; /* index or length */
446 unsigned block_length;
447 unsigned ndp_index;
448 unsigned reserved1;
449 unsigned reserved2;
450 unsigned next_ndp_index;
453 #define INIT_NDP16_OPTS { \
454 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
455 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
456 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
457 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
458 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), \
459 .ndplen_align = 4, \
460 .dgram_item_len = 1, \
461 .block_length = 1, \
462 .ndp_index = 1, \
463 .reserved1 = 0, \
464 .reserved2 = 0, \
465 .next_ndp_index = 1, \
469 #define INIT_NDP32_OPTS { \
470 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
471 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
472 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
473 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
474 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), \
475 .ndplen_align = 8, \
476 .dgram_item_len = 2, \
477 .block_length = 2, \
478 .ndp_index = 2, \
479 .reserved1 = 1, \
480 .reserved2 = 2, \
481 .next_ndp_index = 2, \
484 static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
485 static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
487 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
489 switch (size) {
490 case 1:
491 put_unaligned_le16((u16)val, *p);
492 break;
493 case 2:
494 put_unaligned_le32((u32)val, *p);
496 break;
497 default:
498 BUG();
501 *p += size;
504 static inline unsigned get_ncm(__le16 **p, unsigned size)
506 unsigned tmp;
508 switch (size) {
509 case 1:
510 tmp = get_unaligned_le16(*p);
511 break;
512 case 2:
513 tmp = get_unaligned_le32(*p);
514 break;
515 default:
516 BUG();
519 *p += size;
520 return tmp;
523 /*-------------------------------------------------------------------------*/
525 static inline void ncm_reset_values(struct f_ncm *ncm)
527 ncm->parser_opts = &ndp16_opts;
528 ncm->is_crc = false;
529 ncm->port.cdc_filter = DEFAULT_FILTER;
531 /* doesn't make sense for ncm, fixed size used */
532 ncm->port.header_len = 0;
534 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
535 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
539 * Context: ncm->lock held
541 static void ncm_do_notify(struct f_ncm *ncm)
543 struct usb_request *req = ncm->notify_req;
544 struct usb_cdc_notification *event;
545 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
546 __le32 *data;
547 int status;
549 /* notification already in flight? */
550 if (!req)
551 return;
553 event = req->buf;
554 switch (ncm->notify_state) {
555 case NCM_NOTIFY_NONE:
556 return;
558 case NCM_NOTIFY_CONNECT:
559 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
560 if (ncm->is_open)
561 event->wValue = cpu_to_le16(1);
562 else
563 event->wValue = cpu_to_le16(0);
564 event->wLength = 0;
565 req->length = sizeof *event;
567 DBG(cdev, "notify connect %s\n",
568 ncm->is_open ? "true" : "false");
569 ncm->notify_state = NCM_NOTIFY_NONE;
570 break;
572 case NCM_NOTIFY_SPEED:
573 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
574 event->wValue = cpu_to_le16(0);
575 event->wLength = cpu_to_le16(8);
576 req->length = NCM_STATUS_BYTECOUNT;
578 /* SPEED_CHANGE data is up/down speeds in bits/sec */
579 data = req->buf + sizeof *event;
580 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
581 data[1] = data[0];
583 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
584 ncm->notify_state = NCM_NOTIFY_CONNECT;
585 break;
587 event->bmRequestType = 0xA1;
588 event->wIndex = cpu_to_le16(ncm->ctrl_id);
590 ncm->notify_req = NULL;
592 * In double buffering if there is a space in FIFO,
593 * completion callback can be called right after the call,
594 * so unlocking
596 spin_unlock(&ncm->lock);
597 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
598 spin_lock(&ncm->lock);
599 if (status < 0) {
600 ncm->notify_req = req;
601 DBG(cdev, "notify --> %d\n", status);
606 * Context: ncm->lock held
608 static void ncm_notify(struct f_ncm *ncm)
611 * NOTE on most versions of Linux, host side cdc-ethernet
612 * won't listen for notifications until its netdevice opens.
613 * The first notification then sits in the FIFO for a long
614 * time, and the second one is queued.
616 * If ncm_notify() is called before the second (CONNECT)
617 * notification is sent, then it will reset to send the SPEED
618 * notificaion again (and again, and again), but it's not a problem
620 ncm->notify_state = NCM_NOTIFY_SPEED;
621 ncm_do_notify(ncm);
624 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
626 struct f_ncm *ncm = req->context;
627 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
628 struct usb_cdc_notification *event = req->buf;
630 spin_lock(&ncm->lock);
631 switch (req->status) {
632 case 0:
633 VDBG(cdev, "Notification %02x sent\n",
634 event->bNotificationType);
635 break;
636 case -ECONNRESET:
637 case -ESHUTDOWN:
638 ncm->notify_state = NCM_NOTIFY_NONE;
639 break;
640 default:
641 DBG(cdev, "event %02x --> %d\n",
642 event->bNotificationType, req->status);
643 break;
645 ncm->notify_req = req;
646 ncm_do_notify(ncm);
647 spin_unlock(&ncm->lock);
650 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
652 /* now for SET_NTB_INPUT_SIZE only */
653 unsigned in_size;
654 struct usb_function *f = req->context;
655 struct f_ncm *ncm = func_to_ncm(f);
656 struct usb_composite_dev *cdev = f->config->cdev;
658 req->context = NULL;
659 if (req->status || req->actual != req->length) {
660 DBG(cdev, "Bad control-OUT transfer\n");
661 goto invalid;
664 in_size = get_unaligned_le32(req->buf);
665 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
666 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
667 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
668 goto invalid;
671 ncm->port.fixed_in_len = in_size;
672 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
673 return;
675 invalid:
676 usb_ep_set_halt(ep);
677 return;
680 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
682 struct f_ncm *ncm = func_to_ncm(f);
683 struct usb_composite_dev *cdev = f->config->cdev;
684 struct usb_request *req = cdev->req;
685 int value = -EOPNOTSUPP;
686 u16 w_index = le16_to_cpu(ctrl->wIndex);
687 u16 w_value = le16_to_cpu(ctrl->wValue);
688 u16 w_length = le16_to_cpu(ctrl->wLength);
691 * composite driver infrastructure handles everything except
692 * CDC class messages; interface activation uses set_alt().
694 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
695 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
696 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
698 * see 6.2.30: no data, wIndex = interface,
699 * wValue = packet filter bitmap
701 if (w_length != 0 || w_index != ncm->ctrl_id)
702 goto invalid;
703 DBG(cdev, "packet filter %02x\n", w_value);
705 * REVISIT locking of cdc_filter. This assumes the UDC
706 * driver won't have a concurrent packet TX irq running on
707 * another CPU; or that if it does, this write is atomic...
709 ncm->port.cdc_filter = w_value;
710 value = 0;
711 break;
713 * and optionally:
714 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
715 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
716 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
717 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
718 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
719 * case USB_CDC_GET_ETHERNET_STATISTIC:
722 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
723 | USB_CDC_GET_NTB_PARAMETERS:
725 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
726 goto invalid;
727 value = w_length > sizeof ntb_parameters ?
728 sizeof ntb_parameters : w_length;
729 memcpy(req->buf, &ntb_parameters, value);
730 VDBG(cdev, "Host asked NTB parameters\n");
731 break;
733 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
734 | USB_CDC_GET_NTB_INPUT_SIZE:
736 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
737 goto invalid;
738 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
739 value = 4;
740 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
741 ncm->port.fixed_in_len);
742 break;
744 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
745 | USB_CDC_SET_NTB_INPUT_SIZE:
747 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
748 goto invalid;
749 req->complete = ncm_ep0out_complete;
750 req->length = w_length;
751 req->context = f;
753 value = req->length;
754 break;
757 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
758 | USB_CDC_GET_NTB_FORMAT:
760 uint16_t format;
762 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
763 goto invalid;
764 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
765 put_unaligned_le16(format, req->buf);
766 value = 2;
767 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
768 break;
771 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
772 | USB_CDC_SET_NTB_FORMAT:
774 if (w_length != 0 || w_index != ncm->ctrl_id)
775 goto invalid;
776 switch (w_value) {
777 case 0x0000:
778 ncm->parser_opts = &ndp16_opts;
779 DBG(cdev, "NCM16 selected\n");
780 break;
781 case 0x0001:
782 ncm->parser_opts = &ndp32_opts;
783 DBG(cdev, "NCM32 selected\n");
784 break;
785 default:
786 goto invalid;
788 value = 0;
789 break;
791 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
792 | USB_CDC_GET_CRC_MODE:
794 uint16_t is_crc;
796 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
797 goto invalid;
798 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
799 put_unaligned_le16(is_crc, req->buf);
800 value = 2;
801 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
802 break;
805 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
806 | USB_CDC_SET_CRC_MODE:
808 int ndp_hdr_crc = 0;
810 if (w_length != 0 || w_index != ncm->ctrl_id)
811 goto invalid;
812 switch (w_value) {
813 case 0x0000:
814 ncm->is_crc = false;
815 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
816 DBG(cdev, "non-CRC mode selected\n");
817 break;
818 case 0x0001:
819 ncm->is_crc = true;
820 ndp_hdr_crc = NCM_NDP_HDR_CRC;
821 DBG(cdev, "CRC mode selected\n");
822 break;
823 default:
824 goto invalid;
826 ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
827 value = 0;
828 break;
831 /* and disabled in ncm descriptor: */
832 /* case USB_CDC_GET_NET_ADDRESS: */
833 /* case USB_CDC_SET_NET_ADDRESS: */
834 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
835 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
837 default:
838 invalid:
839 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
840 ctrl->bRequestType, ctrl->bRequest,
841 w_value, w_index, w_length);
844 /* respond with data transfer or status phase? */
845 if (value >= 0) {
846 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
847 ctrl->bRequestType, ctrl->bRequest,
848 w_value, w_index, w_length);
849 req->zero = 0;
850 req->length = value;
851 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
852 if (value < 0)
853 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
854 ctrl->bRequestType, ctrl->bRequest,
855 value);
858 /* device either stalls (value < 0) or reports success */
859 return value;
863 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
865 struct f_ncm *ncm = func_to_ncm(f);
866 struct usb_composite_dev *cdev = f->config->cdev;
868 /* Control interface has only altsetting 0 */
869 if (intf == ncm->ctrl_id) {
870 if (alt != 0)
871 goto fail;
873 DBG(cdev, "reset ncm control %d\n", intf);
874 usb_ep_disable(ncm->notify);
876 if (!(ncm->notify->desc)) {
877 DBG(cdev, "init ncm ctrl %d\n", intf);
878 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
879 goto fail;
881 usb_ep_enable(ncm->notify);
883 /* Data interface has two altsettings, 0 and 1 */
884 } else if (intf == ncm->data_id) {
885 if (alt > 1)
886 goto fail;
888 if (ncm->port.in_ep->enabled) {
889 DBG(cdev, "reset ncm\n");
890 ncm->timer_stopping = true;
891 ncm->netdev = NULL;
892 gether_disconnect(&ncm->port);
893 ncm_reset_values(ncm);
897 * CDC Network only sends data in non-default altsettings.
898 * Changing altsettings resets filters, statistics, etc.
900 if (alt == 1) {
901 struct net_device *net;
903 if (!ncm->port.in_ep->desc ||
904 !ncm->port.out_ep->desc) {
905 DBG(cdev, "init ncm\n");
906 if (config_ep_by_speed(cdev->gadget, f,
907 ncm->port.in_ep) ||
908 config_ep_by_speed(cdev->gadget, f,
909 ncm->port.out_ep)) {
910 ncm->port.in_ep->desc = NULL;
911 ncm->port.out_ep->desc = NULL;
912 goto fail;
916 /* TODO */
917 /* Enable zlps by default for NCM conformance;
918 * override for musb_hdrc (avoids txdma ovhead)
920 ncm->port.is_zlp_ok =
921 gadget_is_zlp_supported(cdev->gadget);
922 ncm->port.cdc_filter = DEFAULT_FILTER;
923 DBG(cdev, "activate ncm\n");
924 net = gether_connect(&ncm->port);
925 if (IS_ERR(net))
926 return PTR_ERR(net);
927 ncm->netdev = net;
928 ncm->timer_stopping = false;
931 spin_lock(&ncm->lock);
932 ncm_notify(ncm);
933 spin_unlock(&ncm->lock);
934 } else
935 goto fail;
937 return 0;
938 fail:
939 return -EINVAL;
943 * Because the data interface supports multiple altsettings,
944 * this NCM function *MUST* implement a get_alt() method.
946 static int ncm_get_alt(struct usb_function *f, unsigned intf)
948 struct f_ncm *ncm = func_to_ncm(f);
950 if (intf == ncm->ctrl_id)
951 return 0;
952 return ncm->port.in_ep->enabled ? 1 : 0;
955 static struct sk_buff *package_for_tx(struct f_ncm *ncm)
957 __le16 *ntb_iter;
958 struct sk_buff *skb2 = NULL;
959 unsigned ndp_pad;
960 unsigned ndp_index;
961 unsigned new_len;
963 const struct ndp_parser_opts *opts = ncm->parser_opts;
964 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
965 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
967 /* Stop the timer */
968 hrtimer_try_to_cancel(&ncm->task_timer);
970 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) -
971 ncm->skb_tx_data->len;
972 ndp_index = ncm->skb_tx_data->len + ndp_pad;
973 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len;
975 /* Set the final BlockLength and wNdpIndex */
976 ntb_iter = (void *) ncm->skb_tx_data->data;
977 /* Increment pointer to BlockLength */
978 ntb_iter += 2 + 1 + 1;
979 put_ncm(&ntb_iter, opts->block_length, new_len);
980 put_ncm(&ntb_iter, opts->ndp_index, ndp_index);
982 /* Set the final NDP wLength */
983 new_len = opts->ndp_size +
984 (ncm->ndp_dgram_count * dgram_idx_len);
985 ncm->ndp_dgram_count = 0;
986 /* Increment from start to wLength */
987 ntb_iter = (void *) ncm->skb_tx_ndp->data;
988 ntb_iter += 2;
989 put_unaligned_le16(new_len, ntb_iter);
991 /* Merge the skbs */
992 swap(skb2, ncm->skb_tx_data);
993 if (ncm->skb_tx_data) {
994 dev_consume_skb_any(ncm->skb_tx_data);
995 ncm->skb_tx_data = NULL;
998 /* Insert NDP alignment. */
999 skb_put_zero(skb2, ndp_pad);
1001 /* Copy NTB across. */
1002 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len);
1003 dev_consume_skb_any(ncm->skb_tx_ndp);
1004 ncm->skb_tx_ndp = NULL;
1006 /* Insert zero'd datagram. */
1007 skb_put_zero(skb2, dgram_idx_len);
1009 return skb2;
1012 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
1013 struct sk_buff *skb)
1015 struct f_ncm *ncm = func_to_ncm(&port->func);
1016 struct sk_buff *skb2 = NULL;
1017 int ncb_len = 0;
1018 __le16 *ntb_data;
1019 __le16 *ntb_ndp;
1020 int dgram_pad;
1022 unsigned max_size = ncm->port.fixed_in_len;
1023 const struct ndp_parser_opts *opts = ncm->parser_opts;
1024 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
1025 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
1026 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
1027 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len;
1029 if (!skb && !ncm->skb_tx_data)
1030 return NULL;
1032 if (skb) {
1033 /* Add the CRC if required up front */
1034 if (ncm->is_crc) {
1035 uint32_t crc;
1036 __le16 *crc_pos;
1038 crc = ~crc32_le(~0,
1039 skb->data,
1040 skb->len);
1041 crc_pos = skb_put(skb, sizeof(uint32_t));
1042 put_unaligned_le32(crc, crc_pos);
1045 /* If the new skb is too big for the current NCM NTB then
1046 * set the current stored skb to be sent now and clear it
1047 * ready for new data.
1048 * NOTE: Assume maximum align for speed of calculation.
1050 if (ncm->skb_tx_data
1051 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
1052 || (ncm->skb_tx_data->len +
1053 div + rem + skb->len +
1054 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
1055 > max_size)) {
1056 skb2 = package_for_tx(ncm);
1057 if (!skb2)
1058 goto err;
1061 if (!ncm->skb_tx_data) {
1062 ncb_len = opts->nth_size;
1063 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1064 ncb_len += dgram_pad;
1066 /* Create a new skb for the NTH and datagrams. */
1067 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
1068 if (!ncm->skb_tx_data)
1069 goto err;
1071 ncm->skb_tx_data->dev = ncm->netdev;
1072 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len);
1073 /* dwSignature */
1074 put_unaligned_le32(opts->nth_sign, ntb_data);
1075 ntb_data += 2;
1076 /* wHeaderLength */
1077 put_unaligned_le16(opts->nth_size, ntb_data++);
1079 /* Allocate an skb for storing the NDP,
1080 * TX_MAX_NUM_DPE should easily suffice for a
1081 * 16k packet.
1083 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size
1084 + opts->dpe_size
1085 * TX_MAX_NUM_DPE),
1086 GFP_ATOMIC);
1087 if (!ncm->skb_tx_ndp)
1088 goto err;
1090 ncm->skb_tx_ndp->dev = ncm->netdev;
1091 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size);
1092 memset(ntb_ndp, 0, ncb_len);
1093 /* dwSignature */
1094 put_unaligned_le32(ncm->ndp_sign, ntb_ndp);
1095 ntb_ndp += 2;
1097 /* There is always a zeroed entry */
1098 ncm->ndp_dgram_count = 1;
1100 /* Note: we skip opts->next_ndp_index */
1103 /* Delay the timer. */
1104 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS,
1105 HRTIMER_MODE_REL_SOFT);
1107 /* Add the datagram position entries */
1108 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len);
1110 ncb_len = ncm->skb_tx_data->len;
1111 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len;
1112 ncb_len += dgram_pad;
1114 /* (d)wDatagramIndex */
1115 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len);
1116 /* (d)wDatagramLength */
1117 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len);
1118 ncm->ndp_dgram_count++;
1120 /* Add the new data to the skb */
1121 skb_put_zero(ncm->skb_tx_data, dgram_pad);
1122 skb_put_data(ncm->skb_tx_data, skb->data, skb->len);
1123 dev_consume_skb_any(skb);
1124 skb = NULL;
1126 } else if (ncm->skb_tx_data && ncm->timer_force_tx) {
1127 /* If the tx was requested because of a timeout then send */
1128 skb2 = package_for_tx(ncm);
1129 if (!skb2)
1130 goto err;
1133 return skb2;
1135 err:
1136 ncm->netdev->stats.tx_dropped++;
1138 if (skb)
1139 dev_kfree_skb_any(skb);
1140 if (ncm->skb_tx_data)
1141 dev_kfree_skb_any(ncm->skb_tx_data);
1142 if (ncm->skb_tx_ndp)
1143 dev_kfree_skb_any(ncm->skb_tx_ndp);
1145 return NULL;
1149 * The transmit should only be run if no skb data has been sent
1150 * for a certain duration.
1152 static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
1154 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
1156 /* Only send if data is available. */
1157 if (!ncm->timer_stopping && ncm->skb_tx_data) {
1158 ncm->timer_force_tx = true;
1160 /* XXX This allowance of a NULL skb argument to ndo_start_xmit
1161 * XXX is not sane. The gadget layer should be redesigned so
1162 * XXX that the dev->wrap() invocations to build SKBs is transparent
1163 * XXX and performed in some way outside of the ndo_start_xmit
1164 * XXX interface.
1166 ncm->netdev->netdev_ops->ndo_start_xmit(NULL, ncm->netdev);
1168 ncm->timer_force_tx = false;
1170 return HRTIMER_NORESTART;
1173 static int ncm_unwrap_ntb(struct gether *port,
1174 struct sk_buff *skb,
1175 struct sk_buff_head *list)
1177 struct f_ncm *ncm = func_to_ncm(&port->func);
1178 __le16 *tmp = (void *) skb->data;
1179 unsigned index, index2;
1180 int ndp_index;
1181 unsigned dg_len, dg_len2;
1182 unsigned ndp_len;
1183 struct sk_buff *skb2;
1184 int ret = -EINVAL;
1185 unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
1186 const struct ndp_parser_opts *opts = ncm->parser_opts;
1187 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
1188 int dgram_counter;
1190 /* dwSignature */
1191 if (get_unaligned_le32(tmp) != opts->nth_sign) {
1192 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
1193 skb->len);
1194 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
1195 skb->data, 32, false);
1197 goto err;
1199 tmp += 2;
1200 /* wHeaderLength */
1201 if (get_unaligned_le16(tmp++) != opts->nth_size) {
1202 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
1203 goto err;
1205 tmp++; /* skip wSequence */
1207 /* (d)wBlockLength */
1208 if (get_ncm(&tmp, opts->block_length) > max_size) {
1209 INFO(port->func.config->cdev, "OUT size exceeded\n");
1210 goto err;
1213 ndp_index = get_ncm(&tmp, opts->ndp_index);
1215 /* Run through all the NDP's in the NTB */
1216 do {
1217 /* NCM 3.2 */
1218 if (((ndp_index % 4) != 0) &&
1219 (ndp_index < opts->nth_size)) {
1220 INFO(port->func.config->cdev, "Bad index: %#X\n",
1221 ndp_index);
1222 goto err;
1225 /* walk through NDP */
1226 tmp = (void *)(skb->data + ndp_index);
1227 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1228 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1229 goto err;
1231 tmp += 2;
1233 ndp_len = get_unaligned_le16(tmp++);
1235 * NCM 3.3.1
1236 * entry is 2 items
1237 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1238 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1239 * Each entry is a dgram index and a dgram length.
1241 if ((ndp_len < opts->ndp_size
1242 + 2 * 2 * (opts->dgram_item_len * 2))
1243 || (ndp_len % opts->ndplen_align != 0)) {
1244 INFO(port->func.config->cdev, "Bad NDP length: %#X\n",
1245 ndp_len);
1246 goto err;
1248 tmp += opts->reserved1;
1249 /* Check for another NDP (d)wNextNdpIndex */
1250 ndp_index = get_ncm(&tmp, opts->next_ndp_index);
1251 tmp += opts->reserved2;
1253 ndp_len -= opts->ndp_size;
1254 index2 = get_ncm(&tmp, opts->dgram_item_len);
1255 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1256 dgram_counter = 0;
1258 do {
1259 index = index2;
1260 dg_len = dg_len2;
1261 if (dg_len < 14 + crc_len) { /* ethernet hdr + crc */
1262 INFO(port->func.config->cdev,
1263 "Bad dgram length: %#X\n", dg_len);
1264 goto err;
1266 if (ncm->is_crc) {
1267 uint32_t crc, crc2;
1269 crc = get_unaligned_le32(skb->data +
1270 index + dg_len -
1271 crc_len);
1272 crc2 = ~crc32_le(~0,
1273 skb->data + index,
1274 dg_len - crc_len);
1275 if (crc != crc2) {
1276 INFO(port->func.config->cdev,
1277 "Bad CRC\n");
1278 goto err;
1282 index2 = get_ncm(&tmp, opts->dgram_item_len);
1283 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1286 * Copy the data into a new skb.
1287 * This ensures the truesize is correct
1289 skb2 = netdev_alloc_skb_ip_align(ncm->netdev,
1290 dg_len - crc_len);
1291 if (skb2 == NULL)
1292 goto err;
1293 skb_put_data(skb2, skb->data + index,
1294 dg_len - crc_len);
1296 skb_queue_tail(list, skb2);
1298 ndp_len -= 2 * (opts->dgram_item_len * 2);
1300 dgram_counter++;
1302 if (index2 == 0 || dg_len2 == 0)
1303 break;
1304 } while (ndp_len > 2 * (opts->dgram_item_len * 2));
1305 } while (ndp_index);
1307 dev_consume_skb_any(skb);
1309 VDBG(port->func.config->cdev,
1310 "Parsed NTB with %d frames\n", dgram_counter);
1311 return 0;
1312 err:
1313 skb_queue_purge(list);
1314 dev_kfree_skb_any(skb);
1315 return ret;
1318 static void ncm_disable(struct usb_function *f)
1320 struct f_ncm *ncm = func_to_ncm(f);
1321 struct usb_composite_dev *cdev = f->config->cdev;
1323 DBG(cdev, "ncm deactivated\n");
1325 if (ncm->port.in_ep->enabled) {
1326 ncm->timer_stopping = true;
1327 ncm->netdev = NULL;
1328 gether_disconnect(&ncm->port);
1331 if (ncm->notify->enabled) {
1332 usb_ep_disable(ncm->notify);
1333 ncm->notify->desc = NULL;
1337 /*-------------------------------------------------------------------------*/
1340 * Callbacks let us notify the host about connect/disconnect when the
1341 * net device is opened or closed.
1343 * For testing, note that link states on this side include both opened
1344 * and closed variants of:
1346 * - disconnected/unconfigured
1347 * - configured but inactive (data alt 0)
1348 * - configured and active (data alt 1)
1350 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1351 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1352 * imply the host is actually polling the notification endpoint, and
1353 * likewise that "active" doesn't imply it's actually using the data
1354 * endpoints for traffic.
1357 static void ncm_open(struct gether *geth)
1359 struct f_ncm *ncm = func_to_ncm(&geth->func);
1361 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1363 spin_lock(&ncm->lock);
1364 ncm->is_open = true;
1365 ncm_notify(ncm);
1366 spin_unlock(&ncm->lock);
1369 static void ncm_close(struct gether *geth)
1371 struct f_ncm *ncm = func_to_ncm(&geth->func);
1373 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1375 spin_lock(&ncm->lock);
1376 ncm->is_open = false;
1377 ncm_notify(ncm);
1378 spin_unlock(&ncm->lock);
1381 /*-------------------------------------------------------------------------*/
1383 /* ethernet function driver setup/binding */
1385 static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
1387 struct usb_composite_dev *cdev = c->cdev;
1388 struct f_ncm *ncm = func_to_ncm(f);
1389 struct usb_string *us;
1390 int status;
1391 struct usb_ep *ep;
1392 struct f_ncm_opts *ncm_opts;
1394 if (!can_support_ecm(cdev->gadget))
1395 return -EINVAL;
1397 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1399 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
1400 * configurations are bound in sequence with list_for_each_entry,
1401 * in each configuration its functions are bound in sequence
1402 * with list_for_each_entry, so we assume no race condition
1403 * with regard to ncm_opts->bound access
1405 if (!ncm_opts->bound) {
1406 mutex_lock(&ncm_opts->lock);
1407 gether_set_gadget(ncm_opts->net, cdev->gadget);
1408 status = gether_register_netdev(ncm_opts->net);
1409 mutex_unlock(&ncm_opts->lock);
1410 if (status)
1411 return status;
1412 ncm_opts->bound = true;
1414 us = usb_gstrings_attach(cdev, ncm_strings,
1415 ARRAY_SIZE(ncm_string_defs));
1416 if (IS_ERR(us))
1417 return PTR_ERR(us);
1418 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id;
1419 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id;
1420 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id;
1421 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id;
1422 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id;
1424 /* allocate instance-specific interface IDs */
1425 status = usb_interface_id(c, f);
1426 if (status < 0)
1427 goto fail;
1428 ncm->ctrl_id = status;
1429 ncm_iad_desc.bFirstInterface = status;
1431 ncm_control_intf.bInterfaceNumber = status;
1432 ncm_union_desc.bMasterInterface0 = status;
1434 status = usb_interface_id(c, f);
1435 if (status < 0)
1436 goto fail;
1437 ncm->data_id = status;
1439 ncm_data_nop_intf.bInterfaceNumber = status;
1440 ncm_data_intf.bInterfaceNumber = status;
1441 ncm_union_desc.bSlaveInterface0 = status;
1443 status = -ENODEV;
1445 /* allocate instance-specific endpoints */
1446 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1447 if (!ep)
1448 goto fail;
1449 ncm->port.in_ep = ep;
1451 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1452 if (!ep)
1453 goto fail;
1454 ncm->port.out_ep = ep;
1456 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1457 if (!ep)
1458 goto fail;
1459 ncm->notify = ep;
1461 status = -ENOMEM;
1463 /* allocate notification request and buffer */
1464 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1465 if (!ncm->notify_req)
1466 goto fail;
1467 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1468 if (!ncm->notify_req->buf)
1469 goto fail;
1470 ncm->notify_req->context = ncm;
1471 ncm->notify_req->complete = ncm_notify_complete;
1474 * support all relevant hardware speeds... we expect that when
1475 * hardware is dual speed, all bulk-capable endpoints work at
1476 * both speeds
1478 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1479 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1480 hs_ncm_notify_desc.bEndpointAddress =
1481 fs_ncm_notify_desc.bEndpointAddress;
1483 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1484 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1485 ss_ncm_notify_desc.bEndpointAddress =
1486 fs_ncm_notify_desc.bEndpointAddress;
1488 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1489 ncm_ss_function, NULL);
1490 if (status)
1491 goto fail;
1494 * NOTE: all that is done without knowing or caring about
1495 * the network link ... which is unavailable to this code
1496 * until we're activated via set_alt().
1499 ncm->port.open = ncm_open;
1500 ncm->port.close = ncm_close;
1502 hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1503 ncm->task_timer.function = ncm_tx_timeout;
1505 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1506 gadget_is_superspeed(c->cdev->gadget) ? "super" :
1507 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1508 ncm->port.in_ep->name, ncm->port.out_ep->name,
1509 ncm->notify->name);
1510 return 0;
1512 fail:
1513 if (ncm->notify_req) {
1514 kfree(ncm->notify_req->buf);
1515 usb_ep_free_request(ncm->notify, ncm->notify_req);
1518 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1520 return status;
1523 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item)
1525 return container_of(to_config_group(item), struct f_ncm_opts,
1526 func_inst.group);
1529 /* f_ncm_item_ops */
1530 USB_ETHERNET_CONFIGFS_ITEM(ncm);
1532 /* f_ncm_opts_dev_addr */
1533 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm);
1535 /* f_ncm_opts_host_addr */
1536 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm);
1538 /* f_ncm_opts_qmult */
1539 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm);
1541 /* f_ncm_opts_ifname */
1542 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm);
1544 static struct configfs_attribute *ncm_attrs[] = {
1545 &ncm_opts_attr_dev_addr,
1546 &ncm_opts_attr_host_addr,
1547 &ncm_opts_attr_qmult,
1548 &ncm_opts_attr_ifname,
1549 NULL,
1552 static const struct config_item_type ncm_func_type = {
1553 .ct_item_ops = &ncm_item_ops,
1554 .ct_attrs = ncm_attrs,
1555 .ct_owner = THIS_MODULE,
1558 static void ncm_free_inst(struct usb_function_instance *f)
1560 struct f_ncm_opts *opts;
1562 opts = container_of(f, struct f_ncm_opts, func_inst);
1563 if (opts->bound)
1564 gether_cleanup(netdev_priv(opts->net));
1565 else
1566 free_netdev(opts->net);
1567 kfree(opts);
1570 static struct usb_function_instance *ncm_alloc_inst(void)
1572 struct f_ncm_opts *opts;
1574 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1575 if (!opts)
1576 return ERR_PTR(-ENOMEM);
1577 mutex_init(&opts->lock);
1578 opts->func_inst.free_func_inst = ncm_free_inst;
1579 opts->net = gether_setup_default();
1580 if (IS_ERR(opts->net)) {
1581 struct net_device *net = opts->net;
1582 kfree(opts);
1583 return ERR_CAST(net);
1586 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type);
1588 return &opts->func_inst;
1591 static void ncm_free(struct usb_function *f)
1593 struct f_ncm *ncm;
1594 struct f_ncm_opts *opts;
1596 ncm = func_to_ncm(f);
1597 opts = container_of(f->fi, struct f_ncm_opts, func_inst);
1598 kfree(ncm);
1599 mutex_lock(&opts->lock);
1600 opts->refcnt--;
1601 mutex_unlock(&opts->lock);
1604 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1606 struct f_ncm *ncm = func_to_ncm(f);
1608 DBG(c->cdev, "ncm unbind\n");
1610 hrtimer_cancel(&ncm->task_timer);
1612 ncm_string_defs[0].id = 0;
1613 usb_free_all_descriptors(f);
1615 kfree(ncm->notify_req->buf);
1616 usb_ep_free_request(ncm->notify, ncm->notify_req);
1619 static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
1621 struct f_ncm *ncm;
1622 struct f_ncm_opts *opts;
1623 int status;
1625 /* allocate and initialize one new instance */
1626 ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
1627 if (!ncm)
1628 return ERR_PTR(-ENOMEM);
1630 opts = container_of(fi, struct f_ncm_opts, func_inst);
1631 mutex_lock(&opts->lock);
1632 opts->refcnt++;
1634 /* export host's Ethernet address in CDC format */
1635 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr,
1636 sizeof(ncm->ethaddr));
1637 if (status < 12) { /* strlen("01234567890a") */
1638 kfree(ncm);
1639 mutex_unlock(&opts->lock);
1640 return ERR_PTR(-EINVAL);
1642 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1644 spin_lock_init(&ncm->lock);
1645 ncm_reset_values(ncm);
1646 ncm->port.ioport = netdev_priv(opts->net);
1647 mutex_unlock(&opts->lock);
1648 ncm->port.is_fixed = true;
1649 ncm->port.supports_multi_frame = true;
1651 ncm->port.func.name = "cdc_network";
1652 /* descriptors are per-instance copies */
1653 ncm->port.func.bind = ncm_bind;
1654 ncm->port.func.unbind = ncm_unbind;
1655 ncm->port.func.set_alt = ncm_set_alt;
1656 ncm->port.func.get_alt = ncm_get_alt;
1657 ncm->port.func.setup = ncm_setup;
1658 ncm->port.func.disable = ncm_disable;
1659 ncm->port.func.free_func = ncm_free;
1661 ncm->port.wrap = ncm_wrap_ntb;
1662 ncm->port.unwrap = ncm_unwrap_ntb;
1664 return &ncm->port.func;
1667 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc);
1668 MODULE_LICENSE("GPL");
1669 MODULE_AUTHOR("Yauheni Kaliuta");