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/device.h>
20 #include <linux/etherdevice.h>
21 #include <linux/crc32.h>
23 #include <linux/usb/cdc.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 */
54 struct usb_ep *notify;
55 struct usb_request *notify_req;
59 const struct ndp_parser_opts *parser_opts;
64 * for notification, it is accessed from both
65 * callback and ethernet open/close
70 static inline struct f_ncm *func_to_ncm(struct usb_function *f)
72 return container_of(f, struct f_ncm, port.func);
75 /* peak (theoretical) bulk transfer rate in bits-per-second */
76 static inline unsigned ncm_bitrate(struct usb_gadget *g)
78 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
79 return 13 * 512 * 8 * 1000 * 8;
81 return 19 * 64 * 1 * 1000 * 8;
84 /*-------------------------------------------------------------------------*/
87 * We cannot group frames so use just the minimal size which ok to put
88 * one max-size ethernet frame.
89 * If the host can group frames, allow it to do that, 16K is selected,
90 * because it's used by default by the current linux host driver
92 #define NTB_DEFAULT_IN_SIZE USB_CDC_NCM_NTB_MIN_IN_SIZE
93 #define NTB_OUT_SIZE 16384
96 * skbs of size less than that will not be aligned
97 * to NCM's dwNtbInMaxSize to save bus bandwidth
100 #define MAX_TX_NONFIXED (512 * 3)
102 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
103 USB_CDC_NCM_NTB32_SUPPORTED)
105 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
106 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
107 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
108 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
109 .wNdpInDivisor = cpu_to_le16(4),
110 .wNdpInPayloadRemainder = cpu_to_le16(0),
111 .wNdpInAlignment = cpu_to_le16(4),
113 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
114 .wNdpOutDivisor = cpu_to_le16(4),
115 .wNdpOutPayloadRemainder = cpu_to_le16(0),
116 .wNdpOutAlignment = cpu_to_le16(4),
120 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
121 * packet, to simplify cancellation; and a big transfer interval, to
122 * waste less bandwidth.
125 #define NCM_STATUS_INTERVAL_MS 32
126 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
128 static struct usb_interface_assoc_descriptor ncm_iad_desc __initdata = {
129 .bLength = sizeof ncm_iad_desc,
130 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
132 /* .bFirstInterface = DYNAMIC, */
133 .bInterfaceCount = 2, /* control + data */
134 .bFunctionClass = USB_CLASS_COMM,
135 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
136 .bFunctionProtocol = USB_CDC_PROTO_NONE,
137 /* .iFunction = DYNAMIC */
140 /* interface descriptor: */
142 static struct usb_interface_descriptor ncm_control_intf __initdata = {
143 .bLength = sizeof ncm_control_intf,
144 .bDescriptorType = USB_DT_INTERFACE,
146 /* .bInterfaceNumber = DYNAMIC */
148 .bInterfaceClass = USB_CLASS_COMM,
149 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
150 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
151 /* .iInterface = DYNAMIC */
154 static struct usb_cdc_header_desc ncm_header_desc __initdata = {
155 .bLength = sizeof ncm_header_desc,
156 .bDescriptorType = USB_DT_CS_INTERFACE,
157 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
159 .bcdCDC = cpu_to_le16(0x0110),
162 static struct usb_cdc_union_desc ncm_union_desc __initdata = {
163 .bLength = sizeof(ncm_union_desc),
164 .bDescriptorType = USB_DT_CS_INTERFACE,
165 .bDescriptorSubType = USB_CDC_UNION_TYPE,
166 /* .bMasterInterface0 = DYNAMIC */
167 /* .bSlaveInterface0 = DYNAMIC */
170 static struct usb_cdc_ether_desc ecm_desc __initdata = {
171 .bLength = sizeof ecm_desc,
172 .bDescriptorType = USB_DT_CS_INTERFACE,
173 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
175 /* this descriptor actually adds value, surprise! */
176 /* .iMACAddress = DYNAMIC */
177 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
178 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
179 .wNumberMCFilters = cpu_to_le16(0),
180 .bNumberPowerFilters = 0,
183 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
185 static struct usb_cdc_ncm_desc ncm_desc __initdata = {
186 .bLength = sizeof ncm_desc,
187 .bDescriptorType = USB_DT_CS_INTERFACE,
188 .bDescriptorSubType = USB_CDC_NCM_TYPE,
190 .bcdNcmVersion = cpu_to_le16(0x0100),
191 /* can process SetEthernetPacketFilter */
192 .bmNetworkCapabilities = NCAPS,
195 /* the default data interface has no endpoints ... */
197 static struct usb_interface_descriptor ncm_data_nop_intf __initdata = {
198 .bLength = sizeof ncm_data_nop_intf,
199 .bDescriptorType = USB_DT_INTERFACE,
201 .bInterfaceNumber = 1,
202 .bAlternateSetting = 0,
204 .bInterfaceClass = USB_CLASS_CDC_DATA,
205 .bInterfaceSubClass = 0,
206 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
207 /* .iInterface = DYNAMIC */
210 /* ... but the "real" data interface has two bulk endpoints */
212 static struct usb_interface_descriptor ncm_data_intf __initdata = {
213 .bLength = sizeof ncm_data_intf,
214 .bDescriptorType = USB_DT_INTERFACE,
216 .bInterfaceNumber = 1,
217 .bAlternateSetting = 1,
219 .bInterfaceClass = USB_CLASS_CDC_DATA,
220 .bInterfaceSubClass = 0,
221 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
222 /* .iInterface = DYNAMIC */
225 /* full speed support: */
227 static struct usb_endpoint_descriptor fs_ncm_notify_desc __initdata = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
231 .bEndpointAddress = USB_DIR_IN,
232 .bmAttributes = USB_ENDPOINT_XFER_INT,
233 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
234 .bInterval = NCM_STATUS_INTERVAL_MS,
237 static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata = {
238 .bLength = USB_DT_ENDPOINT_SIZE,
239 .bDescriptorType = USB_DT_ENDPOINT,
241 .bEndpointAddress = USB_DIR_IN,
242 .bmAttributes = USB_ENDPOINT_XFER_BULK,
245 static struct usb_endpoint_descriptor fs_ncm_out_desc __initdata = {
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
249 .bEndpointAddress = USB_DIR_OUT,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
253 static struct usb_descriptor_header *ncm_fs_function[] __initdata = {
254 (struct usb_descriptor_header *) &ncm_iad_desc,
255 /* CDC NCM control descriptors */
256 (struct usb_descriptor_header *) &ncm_control_intf,
257 (struct usb_descriptor_header *) &ncm_header_desc,
258 (struct usb_descriptor_header *) &ncm_union_desc,
259 (struct usb_descriptor_header *) &ecm_desc,
260 (struct usb_descriptor_header *) &ncm_desc,
261 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
262 /* data interface, altsettings 0 and 1 */
263 (struct usb_descriptor_header *) &ncm_data_nop_intf,
264 (struct usb_descriptor_header *) &ncm_data_intf,
265 (struct usb_descriptor_header *) &fs_ncm_in_desc,
266 (struct usb_descriptor_header *) &fs_ncm_out_desc,
270 /* high speed support: */
272 static struct usb_endpoint_descriptor hs_ncm_notify_desc __initdata = {
273 .bLength = USB_DT_ENDPOINT_SIZE,
274 .bDescriptorType = USB_DT_ENDPOINT,
276 .bEndpointAddress = USB_DIR_IN,
277 .bmAttributes = USB_ENDPOINT_XFER_INT,
278 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
279 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
281 static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata = {
282 .bLength = USB_DT_ENDPOINT_SIZE,
283 .bDescriptorType = USB_DT_ENDPOINT,
285 .bEndpointAddress = USB_DIR_IN,
286 .bmAttributes = USB_ENDPOINT_XFER_BULK,
287 .wMaxPacketSize = cpu_to_le16(512),
290 static struct usb_endpoint_descriptor hs_ncm_out_desc __initdata = {
291 .bLength = USB_DT_ENDPOINT_SIZE,
292 .bDescriptorType = USB_DT_ENDPOINT,
294 .bEndpointAddress = USB_DIR_OUT,
295 .bmAttributes = USB_ENDPOINT_XFER_BULK,
296 .wMaxPacketSize = cpu_to_le16(512),
299 static struct usb_descriptor_header *ncm_hs_function[] __initdata = {
300 (struct usb_descriptor_header *) &ncm_iad_desc,
301 /* CDC NCM control descriptors */
302 (struct usb_descriptor_header *) &ncm_control_intf,
303 (struct usb_descriptor_header *) &ncm_header_desc,
304 (struct usb_descriptor_header *) &ncm_union_desc,
305 (struct usb_descriptor_header *) &ecm_desc,
306 (struct usb_descriptor_header *) &ncm_desc,
307 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
308 /* data interface, altsettings 0 and 1 */
309 (struct usb_descriptor_header *) &ncm_data_nop_intf,
310 (struct usb_descriptor_header *) &ncm_data_intf,
311 (struct usb_descriptor_header *) &hs_ncm_in_desc,
312 (struct usb_descriptor_header *) &hs_ncm_out_desc,
316 /* string descriptors: */
318 #define STRING_CTRL_IDX 0
319 #define STRING_MAC_IDX 1
320 #define STRING_DATA_IDX 2
321 #define STRING_IAD_IDX 3
323 static struct usb_string ncm_string_defs[] = {
324 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
325 [STRING_MAC_IDX].s = "",
326 [STRING_DATA_IDX].s = "CDC Network Data",
327 [STRING_IAD_IDX].s = "CDC NCM",
328 { } /* end of list */
331 static struct usb_gadget_strings ncm_string_table = {
332 .language = 0x0409, /* en-us */
333 .strings = ncm_string_defs,
336 static struct usb_gadget_strings *ncm_strings[] = {
342 * Here are options for NCM Datagram Pointer table (NDP) parser.
343 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
344 * in NDP16 offsets and sizes fields are 1 16bit word wide,
345 * in NDP32 -- 2 16bit words wide. Also signatures are different.
346 * To make the parser code the same, put the differences in the structure,
347 * and switch pointers to the structures when the format is changed.
350 struct ndp_parser_opts {
355 unsigned ndplen_align;
356 /* sizes in u16 units */
357 unsigned dgram_item_len; /* index or length */
358 unsigned block_length;
362 unsigned next_fp_index;
365 #define INIT_NDP16_OPTS { \
366 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
367 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
368 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
369 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
371 .dgram_item_len = 1, \
376 .next_fp_index = 1, \
380 #define INIT_NDP32_OPTS { \
381 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
382 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
383 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
384 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
386 .dgram_item_len = 2, \
391 .next_fp_index = 2, \
394 static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
395 static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
397 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
401 put_unaligned_le16((u16)val, *p);
404 put_unaligned_le32((u32)val, *p);
414 static inline unsigned get_ncm(__le16 **p, unsigned size)
420 tmp = get_unaligned_le16(*p);
423 tmp = get_unaligned_le32(*p);
433 /*-------------------------------------------------------------------------*/
435 static inline void ncm_reset_values(struct f_ncm *ncm)
437 ncm->parser_opts = &ndp16_opts;
439 ncm->port.cdc_filter = DEFAULT_FILTER;
441 /* doesn't make sense for ncm, fixed size used */
442 ncm->port.header_len = 0;
444 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
445 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
449 * Context: ncm->lock held
451 static void ncm_do_notify(struct f_ncm *ncm)
453 struct usb_request *req = ncm->notify_req;
454 struct usb_cdc_notification *event;
455 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
459 /* notification already in flight? */
464 switch (ncm->notify_state) {
465 case NCM_NOTIFY_NONE:
468 case NCM_NOTIFY_CONNECT:
469 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
471 event->wValue = cpu_to_le16(1);
473 event->wValue = cpu_to_le16(0);
475 req->length = sizeof *event;
477 DBG(cdev, "notify connect %s\n",
478 ncm->is_open ? "true" : "false");
479 ncm->notify_state = NCM_NOTIFY_NONE;
482 case NCM_NOTIFY_SPEED:
483 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
484 event->wValue = cpu_to_le16(0);
485 event->wLength = cpu_to_le16(8);
486 req->length = NCM_STATUS_BYTECOUNT;
488 /* SPEED_CHANGE data is up/down speeds in bits/sec */
489 data = req->buf + sizeof *event;
490 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
493 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
494 ncm->notify_state = NCM_NOTIFY_CONNECT;
497 event->bmRequestType = 0xA1;
498 event->wIndex = cpu_to_le16(ncm->ctrl_id);
500 ncm->notify_req = NULL;
502 * In double buffering if there is a space in FIFO,
503 * completion callback can be called right after the call,
506 spin_unlock(&ncm->lock);
507 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
508 spin_lock(&ncm->lock);
510 ncm->notify_req = req;
511 DBG(cdev, "notify --> %d\n", status);
516 * Context: ncm->lock held
518 static void ncm_notify(struct f_ncm *ncm)
521 * NOTE on most versions of Linux, host side cdc-ethernet
522 * won't listen for notifications until its netdevice opens.
523 * The first notification then sits in the FIFO for a long
524 * time, and the second one is queued.
526 * If ncm_notify() is called before the second (CONNECT)
527 * notification is sent, then it will reset to send the SPEED
528 * notificaion again (and again, and again), but it's not a problem
530 ncm->notify_state = NCM_NOTIFY_SPEED;
534 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
536 struct f_ncm *ncm = req->context;
537 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
538 struct usb_cdc_notification *event = req->buf;
540 spin_lock(&ncm->lock);
541 switch (req->status) {
543 VDBG(cdev, "Notification %02x sent\n",
544 event->bNotificationType);
548 ncm->notify_state = NCM_NOTIFY_NONE;
551 DBG(cdev, "event %02x --> %d\n",
552 event->bNotificationType, req->status);
555 ncm->notify_req = req;
557 spin_unlock(&ncm->lock);
560 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
562 /* now for SET_NTB_INPUT_SIZE only */
564 struct usb_function *f = req->context;
565 struct f_ncm *ncm = func_to_ncm(f);
566 struct usb_composite_dev *cdev = ep->driver_data;
569 if (req->status || req->actual != req->length) {
570 DBG(cdev, "Bad control-OUT transfer\n");
574 in_size = get_unaligned_le32(req->buf);
575 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
576 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
577 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
581 ncm->port.fixed_in_len = in_size;
582 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
590 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
592 struct f_ncm *ncm = func_to_ncm(f);
593 struct usb_composite_dev *cdev = f->config->cdev;
594 struct usb_request *req = cdev->req;
595 int value = -EOPNOTSUPP;
596 u16 w_index = le16_to_cpu(ctrl->wIndex);
597 u16 w_value = le16_to_cpu(ctrl->wValue);
598 u16 w_length = le16_to_cpu(ctrl->wLength);
601 * composite driver infrastructure handles everything except
602 * CDC class messages; interface activation uses set_alt().
604 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
605 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
606 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
608 * see 6.2.30: no data, wIndex = interface,
609 * wValue = packet filter bitmap
611 if (w_length != 0 || w_index != ncm->ctrl_id)
613 DBG(cdev, "packet filter %02x\n", w_value);
615 * REVISIT locking of cdc_filter. This assumes the UDC
616 * driver won't have a concurrent packet TX irq running on
617 * another CPU; or that if it does, this write is atomic...
619 ncm->port.cdc_filter = w_value;
624 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
625 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
626 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
627 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
628 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
629 * case USB_CDC_GET_ETHERNET_STATISTIC:
632 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
633 | USB_CDC_GET_NTB_PARAMETERS:
635 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
637 value = w_length > sizeof ntb_parameters ?
638 sizeof ntb_parameters : w_length;
639 memcpy(req->buf, &ntb_parameters, value);
640 VDBG(cdev, "Host asked NTB parameters\n");
643 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
644 | USB_CDC_GET_NTB_INPUT_SIZE:
646 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
648 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
650 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
651 ncm->port.fixed_in_len);
654 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
655 | USB_CDC_SET_NTB_INPUT_SIZE:
657 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
659 req->complete = ncm_ep0out_complete;
660 req->length = w_length;
667 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
668 | USB_CDC_GET_NTB_FORMAT:
672 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
674 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
675 put_unaligned_le16(format, req->buf);
677 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
681 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
682 | USB_CDC_SET_NTB_FORMAT:
684 if (w_length != 0 || w_index != ncm->ctrl_id)
688 ncm->parser_opts = &ndp16_opts;
689 DBG(cdev, "NCM16 selected\n");
692 ncm->parser_opts = &ndp32_opts;
693 DBG(cdev, "NCM32 selected\n");
701 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
702 | USB_CDC_GET_CRC_MODE:
706 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
708 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
709 put_unaligned_le16(is_crc, req->buf);
711 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
715 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
716 | USB_CDC_SET_CRC_MODE:
720 if (w_length != 0 || w_index != ncm->ctrl_id)
725 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
726 DBG(cdev, "non-CRC mode selected\n");
730 ndp_hdr_crc = NCM_NDP_HDR_CRC;
731 DBG(cdev, "CRC mode selected\n");
736 ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc;
741 /* and disabled in ncm descriptor: */
742 /* case USB_CDC_GET_NET_ADDRESS: */
743 /* case USB_CDC_SET_NET_ADDRESS: */
744 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
745 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
749 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
750 ctrl->bRequestType, ctrl->bRequest,
751 w_value, w_index, w_length);
754 /* respond with data transfer or status phase? */
756 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
757 ctrl->bRequestType, ctrl->bRequest,
758 w_value, w_index, w_length);
761 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
763 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
764 ctrl->bRequestType, ctrl->bRequest,
768 /* device either stalls (value < 0) or reports success */
773 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
775 struct f_ncm *ncm = func_to_ncm(f);
776 struct usb_composite_dev *cdev = f->config->cdev;
778 /* Control interface has only altsetting 0 */
779 if (intf == ncm->ctrl_id) {
783 if (ncm->notify->driver_data) {
784 DBG(cdev, "reset ncm control %d\n", intf);
785 usb_ep_disable(ncm->notify);
788 if (!(ncm->notify->desc)) {
789 DBG(cdev, "init ncm ctrl %d\n", intf);
790 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
793 usb_ep_enable(ncm->notify);
794 ncm->notify->driver_data = ncm;
796 /* Data interface has two altsettings, 0 and 1 */
797 } else if (intf == ncm->data_id) {
801 if (ncm->port.in_ep->driver_data) {
802 DBG(cdev, "reset ncm\n");
803 gether_disconnect(&ncm->port);
804 ncm_reset_values(ncm);
808 * CDC Network only sends data in non-default altsettings.
809 * Changing altsettings resets filters, statistics, etc.
812 struct net_device *net;
814 if (!ncm->port.in_ep->desc ||
815 !ncm->port.out_ep->desc) {
816 DBG(cdev, "init ncm\n");
817 if (config_ep_by_speed(cdev->gadget, f,
819 config_ep_by_speed(cdev->gadget, f,
821 ncm->port.in_ep->desc = NULL;
822 ncm->port.out_ep->desc = NULL;
828 /* Enable zlps by default for NCM conformance;
829 * override for musb_hdrc (avoids txdma ovhead)
831 ncm->port.is_zlp_ok = !(
832 gadget_is_musbhdrc(cdev->gadget)
834 ncm->port.cdc_filter = DEFAULT_FILTER;
835 DBG(cdev, "activate ncm\n");
836 net = gether_connect(&ncm->port);
841 spin_lock(&ncm->lock);
843 spin_unlock(&ncm->lock);
853 * Because the data interface supports multiple altsettings,
854 * this NCM function *MUST* implement a get_alt() method.
856 static int ncm_get_alt(struct usb_function *f, unsigned intf)
858 struct f_ncm *ncm = func_to_ncm(f);
860 if (intf == ncm->ctrl_id)
862 return ncm->port.in_ep->driver_data ? 1 : 0;
865 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
868 struct f_ncm *ncm = func_to_ncm(&port->func);
869 struct sk_buff *skb2;
877 unsigned max_size = ncm->port.fixed_in_len;
878 const struct ndp_parser_opts *opts = ncm->parser_opts;
879 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
881 div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
882 rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
883 ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
885 ncb_len += opts->nth_size;
886 ndp_pad = ALIGN(ncb_len, ndp_align) - ncb_len;
888 ncb_len += opts->ndp_size;
889 ncb_len += 2 * 2 * opts->dgram_item_len; /* Datagram entry */
890 ncb_len += 2 * 2 * opts->dgram_item_len; /* Zero datagram entry */
891 pad = ALIGN(ncb_len, div) + rem - ncb_len;
894 if (ncb_len + skb->len + crc_len > max_size) {
895 dev_kfree_skb_any(skb);
899 skb2 = skb_copy_expand(skb, ncb_len,
900 max_size - skb->len - ncb_len - crc_len,
902 dev_kfree_skb_any(skb);
908 tmp = (void *) skb_push(skb, ncb_len);
909 memset(tmp, 0, ncb_len);
911 put_unaligned_le32(opts->nth_sign, tmp); /* dwSignature */
914 put_unaligned_le16(opts->nth_size, tmp++);
915 tmp++; /* skip wSequence */
916 put_ncm(&tmp, opts->block_length, skb->len); /* (d)wBlockLength */
918 /* the first pointer is right after the NTH + align */
919 put_ncm(&tmp, opts->fp_index, opts->nth_size + ndp_pad);
921 tmp = (void *)tmp + ndp_pad;
924 put_unaligned_le32(ncm->ndp_sign, tmp); /* dwSignature */
927 put_unaligned_le16(ncb_len - opts->nth_size - pad, tmp++);
929 tmp += opts->reserved1;
930 tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
931 tmp += opts->reserved2;
939 put_unaligned_le32(crc, skb->data + skb->len);
940 skb_put(skb, crc_len);
943 /* (d)wDatagramIndex[0] */
944 put_ncm(&tmp, opts->dgram_item_len, ncb_len);
945 /* (d)wDatagramLength[0] */
946 put_ncm(&tmp, opts->dgram_item_len, skb->len - ncb_len);
947 /* (d)wDatagramIndex[1] and (d)wDatagramLength[1] already zeroed */
949 if (skb->len > MAX_TX_NONFIXED)
950 memset(skb_put(skb, max_size - skb->len),
951 0, max_size - skb->len);
956 static int ncm_unwrap_ntb(struct gether *port,
958 struct sk_buff_head *list)
960 struct f_ncm *ncm = func_to_ncm(&port->func);
961 __le16 *tmp = (void *) skb->data;
962 unsigned index, index2;
963 unsigned dg_len, dg_len2;
965 struct sk_buff *skb2;
967 unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
968 const struct ndp_parser_opts *opts = ncm->parser_opts;
969 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
973 if (get_unaligned_le32(tmp) != opts->nth_sign) {
974 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
976 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
977 skb->data, 32, false);
983 if (get_unaligned_le16(tmp++) != opts->nth_size) {
984 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
987 tmp++; /* skip wSequence */
989 /* (d)wBlockLength */
990 if (get_ncm(&tmp, opts->block_length) > max_size) {
991 INFO(port->func.config->cdev, "OUT size exceeded\n");
995 index = get_ncm(&tmp, opts->fp_index);
997 if (((index % 4) != 0) && (index < opts->nth_size)) {
998 INFO(port->func.config->cdev, "Bad index: %x\n",
1003 /* walk through NDP */
1004 tmp = ((void *)skb->data) + index;
1005 if (get_unaligned_le32(tmp) != ncm->ndp_sign) {
1006 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1011 ndp_len = get_unaligned_le16(tmp++);
1015 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1016 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1018 if ((ndp_len < opts->ndp_size + 2 * 2 * (opts->dgram_item_len * 2))
1019 || (ndp_len % opts->ndplen_align != 0)) {
1020 INFO(port->func.config->cdev, "Bad NDP length: %x\n", ndp_len);
1023 tmp += opts->reserved1;
1024 tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
1025 tmp += opts->reserved2;
1027 ndp_len -= opts->ndp_size;
1028 index2 = get_ncm(&tmp, opts->dgram_item_len);
1029 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1035 if (dg_len < 14 + crc_len) { /* ethernet header + crc */
1036 INFO(port->func.config->cdev, "Bad dgram length: %x\n",
1043 crc = get_unaligned_le32(skb->data +
1044 index + dg_len - crc_len);
1045 crc2 = ~crc32_le(~0,
1049 INFO(port->func.config->cdev, "Bad CRC\n");
1054 index2 = get_ncm(&tmp, opts->dgram_item_len);
1055 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1057 if (index2 == 0 || dg_len2 == 0) {
1060 skb2 = skb_clone(skb, GFP_ATOMIC);
1065 if (!skb_pull(skb2, index)) {
1070 skb_trim(skb2, dg_len - crc_len);
1071 skb_queue_tail(list, skb2);
1073 ndp_len -= 2 * (opts->dgram_item_len * 2);
1077 if (index2 == 0 || dg_len2 == 0)
1079 } while (ndp_len > 2 * (opts->dgram_item_len * 2)); /* zero entry */
1081 VDBG(port->func.config->cdev,
1082 "Parsed NTB with %d frames\n", dgram_counter);
1085 skb_queue_purge(list);
1086 dev_kfree_skb_any(skb);
1090 static void ncm_disable(struct usb_function *f)
1092 struct f_ncm *ncm = func_to_ncm(f);
1093 struct usb_composite_dev *cdev = f->config->cdev;
1095 DBG(cdev, "ncm deactivated\n");
1097 if (ncm->port.in_ep->driver_data)
1098 gether_disconnect(&ncm->port);
1100 if (ncm->notify->driver_data) {
1101 usb_ep_disable(ncm->notify);
1102 ncm->notify->driver_data = NULL;
1103 ncm->notify->desc = NULL;
1107 /*-------------------------------------------------------------------------*/
1110 * Callbacks let us notify the host about connect/disconnect when the
1111 * net device is opened or closed.
1113 * For testing, note that link states on this side include both opened
1114 * and closed variants of:
1116 * - disconnected/unconfigured
1117 * - configured but inactive (data alt 0)
1118 * - configured and active (data alt 1)
1120 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1121 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1122 * imply the host is actually polling the notification endpoint, and
1123 * likewise that "active" doesn't imply it's actually using the data
1124 * endpoints for traffic.
1127 static void ncm_open(struct gether *geth)
1129 struct f_ncm *ncm = func_to_ncm(&geth->func);
1131 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1133 spin_lock(&ncm->lock);
1134 ncm->is_open = true;
1136 spin_unlock(&ncm->lock);
1139 static void ncm_close(struct gether *geth)
1141 struct f_ncm *ncm = func_to_ncm(&geth->func);
1143 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1145 spin_lock(&ncm->lock);
1146 ncm->is_open = false;
1148 spin_unlock(&ncm->lock);
1151 /*-------------------------------------------------------------------------*/
1153 /* ethernet function driver setup/binding */
1156 ncm_bind(struct usb_configuration *c, struct usb_function *f)
1158 struct usb_composite_dev *cdev = c->cdev;
1159 struct f_ncm *ncm = func_to_ncm(f);
1163 /* allocate instance-specific interface IDs */
1164 status = usb_interface_id(c, f);
1167 ncm->ctrl_id = status;
1168 ncm_iad_desc.bFirstInterface = status;
1170 ncm_control_intf.bInterfaceNumber = status;
1171 ncm_union_desc.bMasterInterface0 = status;
1173 status = usb_interface_id(c, f);
1176 ncm->data_id = status;
1178 ncm_data_nop_intf.bInterfaceNumber = status;
1179 ncm_data_intf.bInterfaceNumber = status;
1180 ncm_union_desc.bSlaveInterface0 = status;
1184 /* allocate instance-specific endpoints */
1185 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1188 ncm->port.in_ep = ep;
1189 ep->driver_data = cdev; /* claim */
1191 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1194 ncm->port.out_ep = ep;
1195 ep->driver_data = cdev; /* claim */
1197 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1201 ep->driver_data = cdev; /* claim */
1205 /* allocate notification request and buffer */
1206 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1207 if (!ncm->notify_req)
1209 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1210 if (!ncm->notify_req->buf)
1212 ncm->notify_req->context = ncm;
1213 ncm->notify_req->complete = ncm_notify_complete;
1216 * support all relevant hardware speeds... we expect that when
1217 * hardware is dual speed, all bulk-capable endpoints work at
1220 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1221 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1222 hs_ncm_notify_desc.bEndpointAddress =
1223 fs_ncm_notify_desc.bEndpointAddress;
1225 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1228 * NOTE: all that is done without knowing or caring about
1229 * the network link ... which is unavailable to this code
1230 * until we're activated via set_alt().
1233 ncm->port.open = ncm_open;
1234 ncm->port.close = ncm_close;
1236 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1237 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1238 ncm->port.in_ep->name, ncm->port.out_ep->name,
1243 usb_free_all_descriptors(f);
1244 if (ncm->notify_req) {
1245 kfree(ncm->notify_req->buf);
1246 usb_ep_free_request(ncm->notify, ncm->notify_req);
1249 /* we might as well release our claims on endpoints */
1251 ncm->notify->driver_data = NULL;
1252 if (ncm->port.out_ep)
1253 ncm->port.out_ep->driver_data = NULL;
1254 if (ncm->port.in_ep)
1255 ncm->port.in_ep->driver_data = NULL;
1257 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1263 ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1265 struct f_ncm *ncm = func_to_ncm(f);
1267 DBG(c->cdev, "ncm unbind\n");
1269 ncm_string_defs[0].id = 0;
1270 usb_free_all_descriptors(f);
1272 kfree(ncm->notify_req->buf);
1273 usb_ep_free_request(ncm->notify, ncm->notify_req);
1279 * ncm_bind_config - add CDC Network link to a configuration
1280 * @c: the configuration to support the network link
1281 * @ethaddr: a buffer in which the ethernet address of the host side
1282 * side of the link was recorded
1283 * Context: single threaded during gadget setup
1285 * Returns zero on success, else negative errno.
1287 * Caller must have called @gether_setup(). Caller is also responsible
1288 * for calling @gether_cleanup() before module unload.
1290 int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
1291 struct eth_dev *dev)
1296 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
1299 if (ncm_string_defs[0].id == 0) {
1300 status = usb_string_ids_tab(c->cdev, ncm_string_defs);
1303 ncm_control_intf.iInterface =
1304 ncm_string_defs[STRING_CTRL_IDX].id;
1306 status = ncm_string_defs[STRING_DATA_IDX].id;
1307 ncm_data_nop_intf.iInterface = status;
1308 ncm_data_intf.iInterface = status;
1310 ecm_desc.iMACAddress = ncm_string_defs[STRING_MAC_IDX].id;
1311 ncm_iad_desc.iFunction = ncm_string_defs[STRING_IAD_IDX].id;
1314 /* allocate and initialize one new instance */
1315 ncm = kzalloc(sizeof *ncm, GFP_KERNEL);
1319 /* export host's Ethernet address in CDC format */
1320 snprintf(ncm->ethaddr, sizeof ncm->ethaddr, "%pm", ethaddr);
1321 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1323 spin_lock_init(&ncm->lock);
1324 ncm_reset_values(ncm);
1325 ncm->port.ioport = dev;
1326 ncm->port.is_fixed = true;
1328 ncm->port.func.name = "cdc_network";
1329 ncm->port.func.strings = ncm_strings;
1330 /* descriptors are per-instance copies */
1331 ncm->port.func.bind = ncm_bind;
1332 ncm->port.func.unbind = ncm_unbind;
1333 ncm->port.func.set_alt = ncm_set_alt;
1334 ncm->port.func.get_alt = ncm_get_alt;
1335 ncm->port.func.setup = ncm_setup;
1336 ncm->port.func.disable = ncm_disable;
1338 ncm->port.wrap = ncm_wrap_ntb;
1339 ncm->port.unwrap = ncm_unwrap_ntb;
1341 status = usb_add_function(c, &ncm->port.func);