1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_ncm.c -- USB CDC Network (NCM) link function driver 4 * 5 * Copyright (C) 2010 Nokia Corporation 6 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com> 7 * 8 * The driver borrows from f_ecm.c which is: 9 * 10 * Copyright (C) 2003-2005,2008 David Brownell 11 * Copyright (C) 2008 Nokia Corporation 12 */ 13 14 #include <linux/cleanup.h> 15 #include <linux/kernel.h> 16 #include <linux/interrupt.h> 17 #include <linux/module.h> 18 #include <linux/device.h> 19 #include <linux/etherdevice.h> 20 #include <linux/crc32.h> 21 #include <linux/string_choices.h> 22 23 #include <linux/usb/cdc.h> 24 #include <linux/usb/gadget.h> 25 26 #include "u_ether.h" 27 #include "u_ether_configfs.h" 28 #include "u_ncm.h" 29 #include "configfs.h" 30 31 /* 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. 34 * 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. 38 */ 39 40 /* to trigger crc/non-crc ndp signature */ 41 42 #define NCM_NDP_HDR_CRC 0x01000000 43 44 enum ncm_notify_state { 45 NCM_NOTIFY_NONE, /* don't notify */ 46 NCM_NOTIFY_CONNECT, /* issue CONNECT next */ 47 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */ 48 }; 49 50 struct f_ncm { 51 struct gether port; 52 u8 ctrl_id, data_id; 53 54 char ethaddr[14]; 55 56 struct usb_ep *notify; 57 struct usb_request *notify_req; 58 u8 notify_state; 59 atomic_t notify_count; 60 bool is_open; 61 62 const struct ndp_parser_opts *parser_opts; 63 bool is_crc; 64 u32 ndp_sign; 65 66 /* 67 * for notification, it is accessed from both 68 * callback and ethernet open/close 69 */ 70 spinlock_t lock; 71 72 struct net_device *netdev; 73 74 /* For multi-frame NDP TX */ 75 struct sk_buff *skb_tx_data; 76 struct sk_buff *skb_tx_ndp; 77 u16 ndp_dgram_count; 78 struct hrtimer task_timer; 79 }; 80 81 static inline struct f_ncm *func_to_ncm(struct usb_function *f) 82 { 83 return container_of(f, struct f_ncm, port.func); 84 } 85 86 /*-------------------------------------------------------------------------*/ 87 88 /* 89 * We cannot group frames so use just the minimal size which ok to put 90 * one max-size ethernet frame. 91 * If the host can group frames, allow it to do that, 16K is selected, 92 * because it's used by default by the current linux host driver 93 */ 94 #define NTB_DEFAULT_IN_SIZE 16384 95 #define NTB_OUT_SIZE 16384 96 97 /* Allocation for storing the NDP, 32 should suffice for a 98 * 16k packet. This allows a maximum of 32 * 507 Byte packets to 99 * be transmitted in a single 16kB skb, though when sending full size 100 * packets this limit will be plenty. 101 * Smaller packets are not likely to be trying to maximize the 102 * throughput and will be mstly sending smaller infrequent frames. 103 */ 104 #define TX_MAX_NUM_DPE 32 105 106 /* Delay for the transmit to wait before sending an unfilled NTB frame. */ 107 #define TX_TIMEOUT_NSECS 300000 108 109 /* 110 * Although max mtu as dictated by u_ether is 15412 bytes, setting 111 * max_segment_size to 15426 would not be efficient. If user chooses segment 112 * size to be (>= 8192), then we can't aggregate more than one buffer in each 113 * NTB (assuming each packet coming from network layer is >= 8192 bytes) as ep 114 * maxpacket limit is 16384. So let max_segment_size be limited to 8000 to allow 115 * at least 2 packets to be aggregated reducing wastage of NTB buffer space 116 */ 117 #define MAX_DATAGRAM_SIZE 8000 118 119 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \ 120 USB_CDC_NCM_NTB32_SUPPORTED) 121 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), 129 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), 134 }; 135 136 /* 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. 140 */ 141 142 #define NCM_STATUS_INTERVAL_MS 32 143 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */ 144 145 static struct usb_interface_assoc_descriptor ncm_iad_desc = { 146 .bLength = sizeof ncm_iad_desc, 147 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 148 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 */ 155 }; 156 157 /* interface descriptor: */ 158 159 static struct usb_interface_descriptor ncm_control_intf = { 160 .bLength = sizeof ncm_control_intf, 161 .bDescriptorType = USB_DT_INTERFACE, 162 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 */ 169 }; 170 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, 175 176 .bcdCDC = cpu_to_le16(0x0110), 177 }; 178 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 */ 185 }; 186 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, 191 192 /* this descriptor actually adds value, surprise! */ 193 /* .iMACAddress = DYNAMIC */ 194 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */ 195 .wNumberMCFilters = cpu_to_le16(0), 196 .bNumberPowerFilters = 0, 197 }; 198 199 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE) 200 201 static struct usb_cdc_ncm_desc ncm_desc = { 202 .bLength = sizeof ncm_desc, 203 .bDescriptorType = USB_DT_CS_INTERFACE, 204 .bDescriptorSubType = USB_CDC_NCM_TYPE, 205 206 .bcdNcmVersion = cpu_to_le16(0x0100), 207 /* can process SetEthernetPacketFilter */ 208 .bmNetworkCapabilities = NCAPS, 209 }; 210 211 /* the default data interface has no endpoints ... */ 212 213 static struct usb_interface_descriptor ncm_data_nop_intf = { 214 .bLength = sizeof ncm_data_nop_intf, 215 .bDescriptorType = USB_DT_INTERFACE, 216 217 .bInterfaceNumber = 1, 218 .bAlternateSetting = 0, 219 .bNumEndpoints = 0, 220 .bInterfaceClass = USB_CLASS_CDC_DATA, 221 .bInterfaceSubClass = 0, 222 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB, 223 /* .iInterface = DYNAMIC */ 224 }; 225 226 /* ... but the "real" data interface has two bulk endpoints */ 227 228 static struct usb_interface_descriptor ncm_data_intf = { 229 .bLength = sizeof ncm_data_intf, 230 .bDescriptorType = USB_DT_INTERFACE, 231 232 .bInterfaceNumber = 1, 233 .bAlternateSetting = 1, 234 .bNumEndpoints = 2, 235 .bInterfaceClass = USB_CLASS_CDC_DATA, 236 .bInterfaceSubClass = 0, 237 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB, 238 /* .iInterface = DYNAMIC */ 239 }; 240 241 /* full speed support: */ 242 243 static struct usb_endpoint_descriptor fs_ncm_notify_desc = { 244 .bLength = USB_DT_ENDPOINT_SIZE, 245 .bDescriptorType = USB_DT_ENDPOINT, 246 247 .bEndpointAddress = USB_DIR_IN, 248 .bmAttributes = USB_ENDPOINT_XFER_INT, 249 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT), 250 .bInterval = NCM_STATUS_INTERVAL_MS, 251 }; 252 253 static struct usb_endpoint_descriptor fs_ncm_in_desc = { 254 .bLength = USB_DT_ENDPOINT_SIZE, 255 .bDescriptorType = USB_DT_ENDPOINT, 256 257 .bEndpointAddress = USB_DIR_IN, 258 .bmAttributes = USB_ENDPOINT_XFER_BULK, 259 }; 260 261 static struct usb_endpoint_descriptor fs_ncm_out_desc = { 262 .bLength = USB_DT_ENDPOINT_SIZE, 263 .bDescriptorType = USB_DT_ENDPOINT, 264 265 .bEndpointAddress = USB_DIR_OUT, 266 .bmAttributes = USB_ENDPOINT_XFER_BULK, 267 }; 268 269 static struct usb_descriptor_header *ncm_fs_function[] = { 270 (struct usb_descriptor_header *) &ncm_iad_desc, 271 /* CDC NCM control descriptors */ 272 (struct usb_descriptor_header *) &ncm_control_intf, 273 (struct usb_descriptor_header *) &ncm_header_desc, 274 (struct usb_descriptor_header *) &ncm_union_desc, 275 (struct usb_descriptor_header *) &ecm_desc, 276 (struct usb_descriptor_header *) &ncm_desc, 277 (struct usb_descriptor_header *) &fs_ncm_notify_desc, 278 /* data interface, altsettings 0 and 1 */ 279 (struct usb_descriptor_header *) &ncm_data_nop_intf, 280 (struct usb_descriptor_header *) &ncm_data_intf, 281 (struct usb_descriptor_header *) &fs_ncm_in_desc, 282 (struct usb_descriptor_header *) &fs_ncm_out_desc, 283 NULL, 284 }; 285 286 /* high speed support: */ 287 288 static struct usb_endpoint_descriptor hs_ncm_notify_desc = { 289 .bLength = USB_DT_ENDPOINT_SIZE, 290 .bDescriptorType = USB_DT_ENDPOINT, 291 292 .bEndpointAddress = USB_DIR_IN, 293 .bmAttributes = USB_ENDPOINT_XFER_INT, 294 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT), 295 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS), 296 }; 297 static struct usb_endpoint_descriptor hs_ncm_in_desc = { 298 .bLength = USB_DT_ENDPOINT_SIZE, 299 .bDescriptorType = USB_DT_ENDPOINT, 300 301 .bEndpointAddress = USB_DIR_IN, 302 .bmAttributes = USB_ENDPOINT_XFER_BULK, 303 .wMaxPacketSize = cpu_to_le16(512), 304 }; 305 306 static struct usb_endpoint_descriptor hs_ncm_out_desc = { 307 .bLength = USB_DT_ENDPOINT_SIZE, 308 .bDescriptorType = USB_DT_ENDPOINT, 309 310 .bEndpointAddress = USB_DIR_OUT, 311 .bmAttributes = USB_ENDPOINT_XFER_BULK, 312 .wMaxPacketSize = cpu_to_le16(512), 313 }; 314 315 static struct usb_descriptor_header *ncm_hs_function[] = { 316 (struct usb_descriptor_header *) &ncm_iad_desc, 317 /* CDC NCM control descriptors */ 318 (struct usb_descriptor_header *) &ncm_control_intf, 319 (struct usb_descriptor_header *) &ncm_header_desc, 320 (struct usb_descriptor_header *) &ncm_union_desc, 321 (struct usb_descriptor_header *) &ecm_desc, 322 (struct usb_descriptor_header *) &ncm_desc, 323 (struct usb_descriptor_header *) &hs_ncm_notify_desc, 324 /* data interface, altsettings 0 and 1 */ 325 (struct usb_descriptor_header *) &ncm_data_nop_intf, 326 (struct usb_descriptor_header *) &ncm_data_intf, 327 (struct usb_descriptor_header *) &hs_ncm_in_desc, 328 (struct usb_descriptor_header *) &hs_ncm_out_desc, 329 NULL, 330 }; 331 332 333 /* super speed support: */ 334 335 static struct usb_endpoint_descriptor ss_ncm_notify_desc = { 336 .bLength = USB_DT_ENDPOINT_SIZE, 337 .bDescriptorType = USB_DT_ENDPOINT, 338 339 .bEndpointAddress = USB_DIR_IN, 340 .bmAttributes = USB_ENDPOINT_XFER_INT, 341 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT), 342 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS) 343 }; 344 345 static struct usb_ss_ep_comp_descriptor ss_ncm_notify_comp_desc = { 346 .bLength = sizeof(ss_ncm_notify_comp_desc), 347 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 348 349 /* the following 3 values can be tweaked if necessary */ 350 /* .bMaxBurst = 0, */ 351 /* .bmAttributes = 0, */ 352 .wBytesPerInterval = cpu_to_le16(NCM_STATUS_BYTECOUNT), 353 }; 354 355 static struct usb_endpoint_descriptor ss_ncm_in_desc = { 356 .bLength = USB_DT_ENDPOINT_SIZE, 357 .bDescriptorType = USB_DT_ENDPOINT, 358 359 .bEndpointAddress = USB_DIR_IN, 360 .bmAttributes = USB_ENDPOINT_XFER_BULK, 361 .wMaxPacketSize = cpu_to_le16(1024), 362 }; 363 364 static struct usb_endpoint_descriptor ss_ncm_out_desc = { 365 .bLength = USB_DT_ENDPOINT_SIZE, 366 .bDescriptorType = USB_DT_ENDPOINT, 367 368 .bEndpointAddress = USB_DIR_OUT, 369 .bmAttributes = USB_ENDPOINT_XFER_BULK, 370 .wMaxPacketSize = cpu_to_le16(1024), 371 }; 372 373 static struct usb_ss_ep_comp_descriptor ss_ncm_bulk_comp_desc = { 374 .bLength = sizeof(ss_ncm_bulk_comp_desc), 375 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 376 377 /* the following 2 values can be tweaked if necessary */ 378 .bMaxBurst = 15, 379 /* .bmAttributes = 0, */ 380 }; 381 382 static struct usb_descriptor_header *ncm_ss_function[] = { 383 (struct usb_descriptor_header *) &ncm_iad_desc, 384 /* CDC NCM control descriptors */ 385 (struct usb_descriptor_header *) &ncm_control_intf, 386 (struct usb_descriptor_header *) &ncm_header_desc, 387 (struct usb_descriptor_header *) &ncm_union_desc, 388 (struct usb_descriptor_header *) &ecm_desc, 389 (struct usb_descriptor_header *) &ncm_desc, 390 (struct usb_descriptor_header *) &ss_ncm_notify_desc, 391 (struct usb_descriptor_header *) &ss_ncm_notify_comp_desc, 392 /* data interface, altsettings 0 and 1 */ 393 (struct usb_descriptor_header *) &ncm_data_nop_intf, 394 (struct usb_descriptor_header *) &ncm_data_intf, 395 (struct usb_descriptor_header *) &ss_ncm_in_desc, 396 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc, 397 (struct usb_descriptor_header *) &ss_ncm_out_desc, 398 (struct usb_descriptor_header *) &ss_ncm_bulk_comp_desc, 399 NULL, 400 }; 401 402 /* string descriptors: */ 403 404 #define STRING_CTRL_IDX 0 405 #define STRING_MAC_IDX 1 406 #define STRING_DATA_IDX 2 407 #define STRING_IAD_IDX 3 408 409 static struct usb_string ncm_string_defs[] = { 410 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)", 411 [STRING_MAC_IDX].s = "", 412 [STRING_DATA_IDX].s = "CDC Network Data", 413 [STRING_IAD_IDX].s = "CDC NCM", 414 { } /* end of list */ 415 }; 416 417 static struct usb_gadget_strings ncm_string_table = { 418 .language = 0x0409, /* en-us */ 419 .strings = ncm_string_defs, 420 }; 421 422 static struct usb_gadget_strings *ncm_strings[] = { 423 &ncm_string_table, 424 NULL, 425 }; 426 427 /* 428 * Here are options for NCM Datagram Pointer table (NDP) parser. 429 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3), 430 * in NDP16 offsets and sizes fields are 1 16bit word wide, 431 * in NDP32 -- 2 16bit words wide. Also signatures are different. 432 * To make the parser code the same, put the differences in the structure, 433 * and switch pointers to the structures when the format is changed. 434 */ 435 436 struct ndp_parser_opts { 437 u32 nth_sign; 438 u32 ndp_sign; 439 unsigned nth_size; 440 unsigned ndp_size; 441 unsigned dpe_size; 442 unsigned ndplen_align; 443 /* sizes in u16 units */ 444 unsigned dgram_item_len; /* index or length */ 445 unsigned block_length; 446 unsigned ndp_index; 447 unsigned reserved1; 448 unsigned reserved2; 449 unsigned next_ndp_index; 450 }; 451 452 static const struct ndp_parser_opts ndp16_opts = { 453 .nth_sign = USB_CDC_NCM_NTH16_SIGN, 454 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, 455 .nth_size = sizeof(struct usb_cdc_ncm_nth16), 456 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), 457 .dpe_size = sizeof(struct usb_cdc_ncm_dpe16), 458 .ndplen_align = 4, 459 .dgram_item_len = 1, 460 .block_length = 1, 461 .ndp_index = 1, 462 .reserved1 = 0, 463 .reserved2 = 0, 464 .next_ndp_index = 1, 465 }; 466 467 static const struct ndp_parser_opts ndp32_opts = { 468 .nth_sign = USB_CDC_NCM_NTH32_SIGN, 469 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, 470 .nth_size = sizeof(struct usb_cdc_ncm_nth32), 471 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), 472 .dpe_size = sizeof(struct usb_cdc_ncm_dpe32), 473 .ndplen_align = 8, 474 .dgram_item_len = 2, 475 .block_length = 2, 476 .ndp_index = 2, 477 .reserved1 = 1, 478 .reserved2 = 2, 479 .next_ndp_index = 2, 480 }; 481 482 static inline void put_ncm(__le16 **p, unsigned size, unsigned val) 483 { 484 switch (size) { 485 case 1: 486 put_unaligned_le16((u16)val, *p); 487 break; 488 case 2: 489 put_unaligned_le32((u32)val, *p); 490 491 break; 492 default: 493 BUG(); 494 } 495 496 *p += size; 497 } 498 499 static inline unsigned get_ncm(__le16 **p, unsigned size) 500 { 501 unsigned tmp; 502 503 switch (size) { 504 case 1: 505 tmp = get_unaligned_le16(*p); 506 break; 507 case 2: 508 tmp = get_unaligned_le32(*p); 509 break; 510 default: 511 BUG(); 512 } 513 514 *p += size; 515 return tmp; 516 } 517 518 /*-------------------------------------------------------------------------*/ 519 520 static inline void ncm_reset_values(struct f_ncm *ncm) 521 { 522 ncm->parser_opts = &ndp16_opts; 523 ncm->is_crc = false; 524 ncm->ndp_sign = ncm->parser_opts->ndp_sign; 525 ncm->port.cdc_filter = DEFAULT_FILTER; 526 527 /* doesn't make sense for ncm, fixed size used */ 528 ncm->port.header_len = 0; 529 530 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize); 531 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE; 532 } 533 534 /* 535 * Context: ncm->lock held 536 */ 537 static void ncm_do_notify(struct f_ncm *ncm) 538 { 539 struct usb_request *req = ncm->notify_req; 540 struct usb_cdc_notification *event; 541 struct usb_composite_dev *cdev = ncm->port.func.config->cdev; 542 __le32 *data; 543 int status; 544 545 /* notification already in flight? */ 546 if (atomic_read(&ncm->notify_count)) 547 return; 548 549 event = req->buf; 550 switch (ncm->notify_state) { 551 case NCM_NOTIFY_NONE: 552 return; 553 554 case NCM_NOTIFY_CONNECT: 555 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; 556 if (ncm->is_open) 557 event->wValue = cpu_to_le16(1); 558 else 559 event->wValue = cpu_to_le16(0); 560 event->wLength = 0; 561 req->length = sizeof *event; 562 563 DBG(cdev, "notify connect %s\n", 564 str_true_false(ncm->is_open)); 565 ncm->notify_state = NCM_NOTIFY_NONE; 566 break; 567 568 case NCM_NOTIFY_SPEED: 569 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; 570 event->wValue = cpu_to_le16(0); 571 event->wLength = cpu_to_le16(8); 572 req->length = NCM_STATUS_BYTECOUNT; 573 574 /* SPEED_CHANGE data is up/down speeds in bits/sec */ 575 data = req->buf + sizeof *event; 576 data[0] = cpu_to_le32(gether_bitrate(cdev->gadget)); 577 data[1] = data[0]; 578 579 DBG(cdev, "notify speed %u\n", gether_bitrate(cdev->gadget)); 580 ncm->notify_state = NCM_NOTIFY_CONNECT; 581 break; 582 } 583 event->bmRequestType = 0xA1; 584 event->wIndex = cpu_to_le16(ncm->ctrl_id); 585 586 atomic_inc(&ncm->notify_count); 587 588 /* 589 * In double buffering if there is a space in FIFO, 590 * completion callback can be called right after the call, 591 * so unlocking 592 */ 593 spin_unlock(&ncm->lock); 594 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC); 595 spin_lock(&ncm->lock); 596 if (status < 0) { 597 atomic_dec(&ncm->notify_count); 598 DBG(cdev, "notify --> %d\n", status); 599 } 600 } 601 602 /* 603 * Context: ncm->lock held 604 */ 605 static void ncm_notify(struct f_ncm *ncm) 606 { 607 /* 608 * NOTE on most versions of Linux, host side cdc-ethernet 609 * won't listen for notifications until its netdevice opens. 610 * The first notification then sits in the FIFO for a long 611 * time, and the second one is queued. 612 * 613 * If ncm_notify() is called before the second (CONNECT) 614 * notification is sent, then it will reset to send the SPEED 615 * notificaion again (and again, and again), but it's not a problem 616 */ 617 ncm->notify_state = NCM_NOTIFY_SPEED; 618 ncm_do_notify(ncm); 619 } 620 621 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req) 622 { 623 struct f_ncm *ncm = req->context; 624 struct usb_composite_dev *cdev = ncm->port.func.config->cdev; 625 struct usb_cdc_notification *event = req->buf; 626 627 spin_lock(&ncm->lock); 628 switch (req->status) { 629 case 0: 630 VDBG(cdev, "Notification %02x sent\n", 631 event->bNotificationType); 632 atomic_dec(&ncm->notify_count); 633 break; 634 case -ECONNRESET: 635 case -ESHUTDOWN: 636 atomic_set(&ncm->notify_count, 0); 637 ncm->notify_state = NCM_NOTIFY_NONE; 638 break; 639 default: 640 DBG(cdev, "event %02x --> %d\n", 641 event->bNotificationType, req->status); 642 atomic_dec(&ncm->notify_count); 643 break; 644 } 645 ncm_do_notify(ncm); 646 spin_unlock(&ncm->lock); 647 } 648 649 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req) 650 { 651 /* now for SET_NTB_INPUT_SIZE only */ 652 unsigned in_size; 653 struct usb_function *f = req->context; 654 struct f_ncm *ncm = func_to_ncm(f); 655 struct usb_composite_dev *cdev = f->config->cdev; 656 657 req->context = NULL; 658 if (req->status || req->actual != req->length) { 659 DBG(cdev, "Bad control-OUT transfer\n"); 660 goto invalid; 661 } 662 663 in_size = get_unaligned_le32(req->buf); 664 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE || 665 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) { 666 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size); 667 goto invalid; 668 } 669 670 ncm->port.fixed_in_len = in_size; 671 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size); 672 return; 673 674 invalid: 675 usb_ep_set_halt(ep); 676 return; 677 } 678 679 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 680 { 681 struct f_ncm *ncm = func_to_ncm(f); 682 struct usb_composite_dev *cdev = f->config->cdev; 683 struct usb_request *req = cdev->req; 684 int value = -EOPNOTSUPP; 685 u16 w_index = le16_to_cpu(ctrl->wIndex); 686 u16 w_value = le16_to_cpu(ctrl->wValue); 687 u16 w_length = le16_to_cpu(ctrl->wLength); 688 689 /* 690 * composite driver infrastructure handles everything except 691 * CDC class messages; interface activation uses set_alt(). 692 */ 693 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 694 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 695 | USB_CDC_SET_ETHERNET_PACKET_FILTER: 696 /* 697 * see 6.2.30: no data, wIndex = interface, 698 * wValue = packet filter bitmap 699 */ 700 if (w_length != 0 || w_index != ncm->ctrl_id) 701 goto invalid; 702 DBG(cdev, "packet filter %02x\n", w_value); 703 /* 704 * REVISIT locking of cdc_filter. This assumes the UDC 705 * driver won't have a concurrent packet TX irq running on 706 * another CPU; or that if it does, this write is atomic... 707 */ 708 ncm->port.cdc_filter = w_value; 709 value = 0; 710 break; 711 /* 712 * and optionally: 713 * case USB_CDC_SEND_ENCAPSULATED_COMMAND: 714 * case USB_CDC_GET_ENCAPSULATED_RESPONSE: 715 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: 716 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: 717 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: 718 * case USB_CDC_GET_ETHERNET_STATISTIC: 719 */ 720 721 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 722 | USB_CDC_GET_NTB_PARAMETERS: 723 724 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id) 725 goto invalid; 726 value = w_length > sizeof ntb_parameters ? 727 sizeof ntb_parameters : w_length; 728 memcpy(req->buf, &ntb_parameters, value); 729 VDBG(cdev, "Host asked NTB parameters\n"); 730 break; 731 732 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 733 | USB_CDC_GET_NTB_INPUT_SIZE: 734 735 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id) 736 goto invalid; 737 put_unaligned_le32(ncm->port.fixed_in_len, req->buf); 738 value = 4; 739 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n", 740 ncm->port.fixed_in_len); 741 break; 742 743 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 744 | USB_CDC_SET_NTB_INPUT_SIZE: 745 { 746 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id) 747 goto invalid; 748 req->complete = ncm_ep0out_complete; 749 req->length = w_length; 750 req->context = f; 751 752 value = req->length; 753 break; 754 } 755 756 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 757 | USB_CDC_GET_NTB_FORMAT: 758 { 759 uint16_t format; 760 761 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id) 762 goto invalid; 763 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001; 764 put_unaligned_le16(format, req->buf); 765 value = 2; 766 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format); 767 break; 768 } 769 770 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 771 | USB_CDC_SET_NTB_FORMAT: 772 { 773 if (w_length != 0 || w_index != ncm->ctrl_id) 774 goto invalid; 775 switch (w_value) { 776 case 0x0000: 777 ncm->parser_opts = &ndp16_opts; 778 DBG(cdev, "NCM16 selected\n"); 779 break; 780 case 0x0001: 781 ncm->parser_opts = &ndp32_opts; 782 DBG(cdev, "NCM32 selected\n"); 783 break; 784 default: 785 goto invalid; 786 } 787 value = 0; 788 break; 789 } 790 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 791 | USB_CDC_GET_CRC_MODE: 792 { 793 uint16_t is_crc; 794 795 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id) 796 goto invalid; 797 is_crc = ncm->is_crc ? 0x0001 : 0x0000; 798 put_unaligned_le16(is_crc, req->buf); 799 value = 2; 800 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc); 801 break; 802 } 803 804 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 805 | USB_CDC_SET_CRC_MODE: 806 { 807 if (w_length != 0 || w_index != ncm->ctrl_id) 808 goto invalid; 809 switch (w_value) { 810 case 0x0000: 811 ncm->is_crc = false; 812 DBG(cdev, "non-CRC mode selected\n"); 813 break; 814 case 0x0001: 815 ncm->is_crc = true; 816 DBG(cdev, "CRC mode selected\n"); 817 break; 818 default: 819 goto invalid; 820 } 821 value = 0; 822 break; 823 } 824 825 /* and disabled in ncm descriptor: */ 826 /* case USB_CDC_GET_NET_ADDRESS: */ 827 /* case USB_CDC_SET_NET_ADDRESS: */ 828 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */ 829 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */ 830 831 default: 832 invalid: 833 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 834 ctrl->bRequestType, ctrl->bRequest, 835 w_value, w_index, w_length); 836 } 837 ncm->ndp_sign = ncm->parser_opts->ndp_sign | 838 (ncm->is_crc ? NCM_NDP_HDR_CRC : 0); 839 840 /* respond with data transfer or status phase? */ 841 if (value >= 0) { 842 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n", 843 ctrl->bRequestType, ctrl->bRequest, 844 w_value, w_index, w_length); 845 req->zero = 0; 846 req->length = value; 847 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 848 if (value < 0) 849 ERROR(cdev, "ncm req %02x.%02x response err %d\n", 850 ctrl->bRequestType, ctrl->bRequest, 851 value); 852 } 853 854 /* device either stalls (value < 0) or reports success */ 855 return value; 856 } 857 858 859 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 860 { 861 struct f_ncm *ncm = func_to_ncm(f); 862 struct usb_composite_dev *cdev = f->config->cdev; 863 864 /* Control interface has only altsetting 0 */ 865 if (intf == ncm->ctrl_id) { 866 if (alt != 0) 867 goto fail; 868 869 DBG(cdev, "reset ncm control %d\n", intf); 870 usb_ep_disable(ncm->notify); 871 872 if (!(ncm->notify->desc)) { 873 DBG(cdev, "init ncm ctrl %d\n", intf); 874 if (config_ep_by_speed(cdev->gadget, f, ncm->notify)) 875 goto fail; 876 } 877 usb_ep_enable(ncm->notify); 878 879 /* Data interface has two altsettings, 0 and 1 */ 880 } else if (intf == ncm->data_id) { 881 if (alt > 1) 882 goto fail; 883 884 if (ncm->netdev) { 885 DBG(cdev, "reset ncm\n"); 886 ncm->netdev = NULL; 887 gether_disconnect(&ncm->port); 888 ncm_reset_values(ncm); 889 } 890 891 /* 892 * CDC Network only sends data in non-default altsettings. 893 * Changing altsettings resets filters, statistics, etc. 894 */ 895 if (alt == 1) { 896 struct net_device *net; 897 898 if (!ncm->port.in_ep->desc || 899 !ncm->port.out_ep->desc) { 900 DBG(cdev, "init ncm\n"); 901 if (config_ep_by_speed(cdev->gadget, f, 902 ncm->port.in_ep) || 903 config_ep_by_speed(cdev->gadget, f, 904 ncm->port.out_ep)) { 905 ncm->port.in_ep->desc = NULL; 906 ncm->port.out_ep->desc = NULL; 907 goto fail; 908 } 909 } 910 911 /* TODO */ 912 /* Enable zlps by default for NCM conformance; 913 * override for musb_hdrc (avoids txdma ovhead) 914 */ 915 ncm->port.is_zlp_ok = 916 gadget_is_zlp_supported(cdev->gadget); 917 ncm->port.cdc_filter = DEFAULT_FILTER; 918 DBG(cdev, "activate ncm\n"); 919 net = gether_connect(&ncm->port); 920 if (IS_ERR(net)) 921 return PTR_ERR(net); 922 ncm->netdev = net; 923 } 924 } else 925 goto fail; 926 927 return 0; 928 fail: 929 return -EINVAL; 930 } 931 932 /* 933 * Because the data interface supports multiple altsettings, 934 * this NCM function *MUST* implement a get_alt() method. 935 */ 936 static int ncm_get_alt(struct usb_function *f, unsigned intf) 937 { 938 struct f_ncm *ncm = func_to_ncm(f); 939 940 if (intf == ncm->ctrl_id) 941 return 0; 942 return ncm->port.in_ep->enabled ? 1 : 0; 943 } 944 945 static struct sk_buff *package_for_tx(struct f_ncm *ncm) 946 { 947 __le16 *ntb_iter; 948 struct sk_buff *skb2 = NULL; 949 unsigned ndp_pad; 950 unsigned ndp_index; 951 unsigned new_len; 952 953 const struct ndp_parser_opts *opts = ncm->parser_opts; 954 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment); 955 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len; 956 957 /* Stop the timer */ 958 hrtimer_try_to_cancel(&ncm->task_timer); 959 960 ndp_pad = ALIGN(ncm->skb_tx_data->len, ndp_align) - 961 ncm->skb_tx_data->len; 962 ndp_index = ncm->skb_tx_data->len + ndp_pad; 963 new_len = ndp_index + dgram_idx_len + ncm->skb_tx_ndp->len; 964 965 /* Set the final BlockLength and wNdpIndex */ 966 ntb_iter = (void *) ncm->skb_tx_data->data; 967 /* Increment pointer to BlockLength */ 968 ntb_iter += 2 + 1 + 1; 969 put_ncm(&ntb_iter, opts->block_length, new_len); 970 put_ncm(&ntb_iter, opts->ndp_index, ndp_index); 971 972 /* Set the final NDP wLength */ 973 new_len = opts->ndp_size + 974 (ncm->ndp_dgram_count * dgram_idx_len); 975 ncm->ndp_dgram_count = 0; 976 /* Increment from start to wLength */ 977 ntb_iter = (void *) ncm->skb_tx_ndp->data; 978 ntb_iter += 2; 979 put_unaligned_le16(new_len, ntb_iter); 980 981 /* Merge the skbs */ 982 swap(skb2, ncm->skb_tx_data); 983 if (ncm->skb_tx_data) { 984 dev_consume_skb_any(ncm->skb_tx_data); 985 ncm->skb_tx_data = NULL; 986 } 987 988 /* Insert NDP alignment. */ 989 skb_put_zero(skb2, ndp_pad); 990 991 /* Copy NTB across. */ 992 skb_put_data(skb2, ncm->skb_tx_ndp->data, ncm->skb_tx_ndp->len); 993 dev_consume_skb_any(ncm->skb_tx_ndp); 994 ncm->skb_tx_ndp = NULL; 995 996 /* Insert zero'd datagram. */ 997 skb_put_zero(skb2, dgram_idx_len); 998 999 return skb2; 1000 } 1001 1002 static struct sk_buff *ncm_wrap_ntb(struct gether *port, 1003 struct sk_buff *skb) 1004 { 1005 struct f_ncm *ncm = func_to_ncm(&port->func); 1006 struct sk_buff *skb2 = NULL; 1007 1008 if (skb) { 1009 int ncb_len = 0; 1010 __le16 *ntb_data; 1011 __le16 *ntb_ndp; 1012 int dgram_pad; 1013 1014 unsigned max_size = ncm->port.fixed_in_len; 1015 const struct ndp_parser_opts *opts = ncm->parser_opts; 1016 const int ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment); 1017 const int div = le16_to_cpu(ntb_parameters.wNdpInDivisor); 1018 const int rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder); 1019 const int dgram_idx_len = 2 * 2 * opts->dgram_item_len; 1020 1021 /* Add the CRC if required up front */ 1022 if (ncm->is_crc) { 1023 uint32_t crc; 1024 __le16 *crc_pos; 1025 1026 crc = ~crc32_le(~0, 1027 skb->data, 1028 skb->len); 1029 crc_pos = skb_put(skb, sizeof(uint32_t)); 1030 put_unaligned_le32(crc, crc_pos); 1031 } 1032 1033 /* If the new skb is too big for the current NCM NTB then 1034 * set the current stored skb to be sent now and clear it 1035 * ready for new data. 1036 * NOTE: Assume maximum align for speed of calculation. 1037 */ 1038 if (ncm->skb_tx_data 1039 && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE 1040 || (ncm->skb_tx_data->len + 1041 div + rem + skb->len + 1042 ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len)) 1043 > max_size)) { 1044 skb2 = package_for_tx(ncm); 1045 if (!skb2) 1046 goto err; 1047 } 1048 1049 if (!ncm->skb_tx_data) { 1050 ncb_len = opts->nth_size; 1051 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len; 1052 ncb_len += dgram_pad; 1053 1054 /* Create a new skb for the NTH and datagrams. */ 1055 ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC); 1056 if (!ncm->skb_tx_data) 1057 goto err; 1058 1059 ncm->skb_tx_data->dev = ncm->netdev; 1060 ntb_data = skb_put_zero(ncm->skb_tx_data, ncb_len); 1061 /* dwSignature */ 1062 put_unaligned_le32(opts->nth_sign, ntb_data); 1063 ntb_data += 2; 1064 /* wHeaderLength */ 1065 put_unaligned_le16(opts->nth_size, ntb_data++); 1066 1067 /* Allocate an skb for storing the NDP, 1068 * TX_MAX_NUM_DPE should easily suffice for a 1069 * 16k packet. 1070 */ 1071 ncm->skb_tx_ndp = alloc_skb((int)(opts->ndp_size 1072 + opts->dpe_size 1073 * TX_MAX_NUM_DPE), 1074 GFP_ATOMIC); 1075 if (!ncm->skb_tx_ndp) 1076 goto err; 1077 1078 ncm->skb_tx_ndp->dev = ncm->netdev; 1079 ntb_ndp = skb_put(ncm->skb_tx_ndp, opts->ndp_size); 1080 memset(ntb_ndp, 0, ncb_len); 1081 /* dwSignature */ 1082 put_unaligned_le32(ncm->ndp_sign, ntb_ndp); 1083 ntb_ndp += 2; 1084 1085 /* There is always a zeroed entry */ 1086 ncm->ndp_dgram_count = 1; 1087 1088 /* Note: we skip opts->next_ndp_index */ 1089 1090 /* Start the timer. */ 1091 hrtimer_start(&ncm->task_timer, TX_TIMEOUT_NSECS, 1092 HRTIMER_MODE_REL_SOFT); 1093 } 1094 1095 /* Add the datagram position entries */ 1096 ntb_ndp = skb_put_zero(ncm->skb_tx_ndp, dgram_idx_len); 1097 1098 ncb_len = ncm->skb_tx_data->len; 1099 dgram_pad = ALIGN(ncb_len, div) + rem - ncb_len; 1100 ncb_len += dgram_pad; 1101 1102 /* (d)wDatagramIndex */ 1103 put_ncm(&ntb_ndp, opts->dgram_item_len, ncb_len); 1104 /* (d)wDatagramLength */ 1105 put_ncm(&ntb_ndp, opts->dgram_item_len, skb->len); 1106 ncm->ndp_dgram_count++; 1107 1108 /* Add the new data to the skb */ 1109 skb_put_zero(ncm->skb_tx_data, dgram_pad); 1110 skb_put_data(ncm->skb_tx_data, skb->data, skb->len); 1111 dev_consume_skb_any(skb); 1112 skb = NULL; 1113 1114 } else if (ncm->skb_tx_data) { 1115 /* If we get here ncm_wrap_ntb() was called with NULL skb, 1116 * because eth_start_xmit() was called with NULL skb by 1117 * ncm_tx_timeout() - hence, this is our signal to flush/send. 1118 */ 1119 skb2 = package_for_tx(ncm); 1120 if (!skb2) 1121 goto err; 1122 } 1123 1124 return skb2; 1125 1126 err: 1127 ncm->netdev->stats.tx_dropped++; 1128 1129 if (skb) 1130 dev_kfree_skb_any(skb); 1131 if (ncm->skb_tx_data) { 1132 dev_kfree_skb_any(ncm->skb_tx_data); 1133 ncm->skb_tx_data = NULL; 1134 } 1135 if (ncm->skb_tx_ndp) { 1136 dev_kfree_skb_any(ncm->skb_tx_ndp); 1137 ncm->skb_tx_ndp = NULL; 1138 } 1139 1140 return NULL; 1141 } 1142 1143 /* 1144 * The transmit should only be run if no skb data has been sent 1145 * for a certain duration. 1146 */ 1147 static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data) 1148 { 1149 struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer); 1150 struct net_device *netdev = READ_ONCE(ncm->netdev); 1151 netdev_tx_t ret; 1152 1153 if (netdev) { 1154 /* XXX This allowance of a NULL skb argument to ndo_start_xmit 1155 * XXX is not sane. The gadget layer should be redesigned so 1156 * XXX that the dev->wrap() invocations to build SKBs is transparent 1157 * XXX and performed in some way outside of the ndo_start_xmit 1158 * XXX interface. 1159 * 1160 * This will call directly into u_ether's eth_start_xmit() 1161 */ 1162 ret = netdev->netdev_ops->ndo_start_xmit(NULL, netdev); 1163 if (ret == NETDEV_TX_BUSY) { 1164 hrtimer_forward_now(data, TX_TIMEOUT_NSECS); 1165 return HRTIMER_RESTART; 1166 } 1167 } 1168 return HRTIMER_NORESTART; 1169 } 1170 1171 static int ncm_unwrap_ntb(struct gether *port, 1172 struct sk_buff *skb, 1173 struct sk_buff_head *list) 1174 { 1175 struct f_ncm *ncm = func_to_ncm(&port->func); 1176 unsigned char *ntb_ptr = skb->data; 1177 __le16 *tmp; 1178 unsigned index, index2; 1179 unsigned int ndp_index; 1180 unsigned dg_len, dg_len2; 1181 unsigned ndp_len; 1182 unsigned block_len; 1183 unsigned int ndp_count, next_ndp_index; 1184 struct sk_buff *skb2; 1185 int ret = -EINVAL; 1186 unsigned ntb_max = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize); 1187 unsigned frame_max; 1188 const struct ndp_parser_opts *opts = ncm->parser_opts; 1189 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0; 1190 int dgram_counter; 1191 int to_process = skb->len; 1192 struct f_ncm_opts *ncm_opts; 1193 1194 ncm_opts = container_of(port->func.fi, struct f_ncm_opts, func_inst); 1195 frame_max = ncm_opts->max_segment_size; 1196 1197 parse_ntb: 1198 if (to_process < (int)opts->nth_size) { 1199 INFO(port->func.config->cdev, "Packet too small for headers\n"); 1200 goto err; 1201 } 1202 tmp = (__le16 *)ntb_ptr; 1203 1204 /* dwSignature */ 1205 if (get_unaligned_le32(tmp) != opts->nth_sign) { 1206 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n", 1207 skb->len); 1208 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1, 1209 skb->data, 32, false); 1210 1211 goto err; 1212 } 1213 tmp += 2; 1214 /* wHeaderLength */ 1215 if (get_unaligned_le16(tmp++) != opts->nth_size) { 1216 INFO(port->func.config->cdev, "Wrong NTB headersize\n"); 1217 goto err; 1218 } 1219 tmp++; /* skip wSequence */ 1220 1221 block_len = get_ncm(&tmp, opts->block_length); 1222 if (block_len == 0) 1223 block_len = to_process; 1224 1225 /* (d)wBlockLength */ 1226 if ((block_len < opts->nth_size + opts->ndp_size) || (block_len > ntb_max) || 1227 (block_len > to_process)) { 1228 INFO(port->func.config->cdev, "Bad block length: %#X\n", block_len); 1229 goto err; 1230 } 1231 1232 ndp_index = get_ncm(&tmp, opts->ndp_index); 1233 next_ndp_index = ndp_index; 1234 ndp_count = 0; 1235 1236 /* Validate the NDP chain before allocating datagram skbs. */ 1237 while (next_ndp_index) { 1238 if (next_ndp_index % 4 || 1239 next_ndp_index < opts->nth_size || 1240 next_ndp_index > block_len - opts->ndp_size) { 1241 INFO(port->func.config->cdev, "Bad index: %#X\n", 1242 next_ndp_index); 1243 goto err; 1244 } 1245 1246 /* More aligned offsets than fit in the NTB imply a cycle. */ 1247 if (++ndp_count > block_len / 4) { 1248 INFO(port->func.config->cdev, "NDP chain cycle\n"); 1249 goto err; 1250 } 1251 1252 tmp = (__le16 *)(ntb_ptr + next_ndp_index); 1253 tmp += 3; /* skip the signature and length */ 1254 tmp += opts->reserved1; 1255 next_ndp_index = get_ncm(&tmp, opts->next_ndp_index); 1256 } 1257 1258 /* Run through all the NDP's in the NTB */ 1259 do { 1260 /* 1261 * NCM 3.2 1262 * dwNdpIndex 1263 */ 1264 if (((ndp_index % 4) != 0) || 1265 (ndp_index < opts->nth_size) || 1266 (ndp_index > (block_len - 1267 opts->ndp_size))) { 1268 INFO(port->func.config->cdev, "Bad index: %#X\n", 1269 ndp_index); 1270 goto err; 1271 } 1272 1273 /* 1274 * walk through NDP 1275 * dwSignature 1276 */ 1277 tmp = (__le16 *)(ntb_ptr + ndp_index); 1278 if (get_unaligned_le32(tmp) != ncm->ndp_sign) { 1279 INFO(port->func.config->cdev, "Wrong NDP SIGN\n"); 1280 goto err; 1281 } 1282 tmp += 2; 1283 1284 ndp_len = get_unaligned_le16(tmp++); 1285 /* 1286 * NCM 3.3.1 1287 * wLength 1288 * entry is 2 items 1289 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes 1290 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry 1291 * Each entry is a dgram index and a dgram length. 1292 */ 1293 if ((ndp_len < opts->ndp_size 1294 + 2 * 2 * (opts->dgram_item_len * 2)) || 1295 (ndp_len % opts->ndplen_align != 0)) { 1296 INFO(port->func.config->cdev, "Bad NDP length: %#X\n", 1297 ndp_len); 1298 goto err; 1299 } 1300 tmp += opts->reserved1; 1301 /* Check for another NDP (d)wNextNdpIndex */ 1302 ndp_index = get_ncm(&tmp, opts->next_ndp_index); 1303 tmp += opts->reserved2; 1304 1305 ndp_len -= opts->ndp_size; 1306 index2 = get_ncm(&tmp, opts->dgram_item_len); 1307 dg_len2 = get_ncm(&tmp, opts->dgram_item_len); 1308 dgram_counter = 0; 1309 1310 do { 1311 index = index2; 1312 /* wDatagramIndex[0] */ 1313 if ((index < opts->nth_size) || 1314 (index > block_len)) { 1315 INFO(port->func.config->cdev, 1316 "Bad index: %#X\n", index); 1317 goto err; 1318 } 1319 1320 dg_len = dg_len2; 1321 /* 1322 * wDatagramLength[0] 1323 * ethernet hdr + crc or larger than max frame size 1324 */ 1325 if ((dg_len < 14 + crc_len) || 1326 (dg_len > frame_max) || 1327 (dg_len > block_len - index)) { 1328 INFO(port->func.config->cdev, 1329 "Bad dgram length: %#X\n", dg_len); 1330 goto err; 1331 } 1332 if (ncm->is_crc) { 1333 uint32_t crc, crc2; 1334 1335 crc = get_unaligned_le32(ntb_ptr + 1336 index + dg_len - 1337 crc_len); 1338 crc2 = ~crc32_le(~0, 1339 ntb_ptr + index, 1340 dg_len - crc_len); 1341 if (crc != crc2) { 1342 INFO(port->func.config->cdev, 1343 "Bad CRC\n"); 1344 goto err; 1345 } 1346 } 1347 1348 index2 = get_ncm(&tmp, opts->dgram_item_len); 1349 dg_len2 = get_ncm(&tmp, opts->dgram_item_len); 1350 1351 /* wDatagramIndex[1] */ 1352 if (index2 > block_len) { 1353 INFO(port->func.config->cdev, 1354 "Bad index: %#X\n", index2); 1355 goto err; 1356 } 1357 1358 /* 1359 * Copy the data into a new skb. 1360 * This ensures the truesize is correct 1361 */ 1362 skb2 = netdev_alloc_skb_ip_align(ncm->netdev, 1363 dg_len - crc_len); 1364 if (skb2 == NULL) 1365 goto err; 1366 skb_put_data(skb2, ntb_ptr + index, 1367 dg_len - crc_len); 1368 1369 skb_queue_tail(list, skb2); 1370 1371 ndp_len -= 2 * (opts->dgram_item_len * 2); 1372 1373 dgram_counter++; 1374 if (index2 == 0 || dg_len2 == 0) 1375 break; 1376 } while (ndp_len > 2 * (opts->dgram_item_len * 2)); 1377 } while (ndp_index); 1378 1379 VDBG(port->func.config->cdev, 1380 "Parsed NTB with %d frames\n", dgram_counter); 1381 1382 to_process -= block_len; 1383 1384 /* 1385 * Windows NCM driver avoids USB ZLPs by adding a 1-byte 1386 * zero pad as needed. 1387 */ 1388 if (to_process == 1 && 1389 (*(unsigned char *)(ntb_ptr + block_len) == 0x00)) { 1390 to_process--; 1391 } else if ((to_process > 0) && (block_len != 0)) { 1392 ntb_ptr = (unsigned char *)(ntb_ptr + block_len); 1393 goto parse_ntb; 1394 } 1395 1396 dev_consume_skb_any(skb); 1397 1398 return 0; 1399 err: 1400 skb_queue_purge(list); 1401 dev_kfree_skb_any(skb); 1402 return ret; 1403 } 1404 1405 static void ncm_disable(struct usb_function *f) 1406 { 1407 struct f_ncm *ncm = func_to_ncm(f); 1408 struct usb_composite_dev *cdev = f->config->cdev; 1409 1410 DBG(cdev, "ncm deactivated\n"); 1411 1412 if (ncm->netdev) { 1413 ncm->netdev = NULL; 1414 gether_disconnect(&ncm->port); 1415 } 1416 1417 if (ncm->notify->enabled) { 1418 usb_ep_disable(ncm->notify); 1419 ncm->notify->desc = NULL; 1420 } 1421 } 1422 1423 /*-------------------------------------------------------------------------*/ 1424 1425 /* 1426 * Callbacks let us notify the host about connect/disconnect when the 1427 * net device is opened or closed. 1428 * 1429 * For testing, note that link states on this side include both opened 1430 * and closed variants of: 1431 * 1432 * - disconnected/unconfigured 1433 * - configured but inactive (data alt 0) 1434 * - configured and active (data alt 1) 1435 * 1436 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and 1437 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't 1438 * imply the host is actually polling the notification endpoint, and 1439 * likewise that "active" doesn't imply it's actually using the data 1440 * endpoints for traffic. 1441 */ 1442 1443 static void ncm_open(struct gether *geth) 1444 { 1445 struct f_ncm *ncm = func_to_ncm(&geth->func); 1446 1447 DBG(ncm->port.func.config->cdev, "%s\n", __func__); 1448 1449 spin_lock(&ncm->lock); 1450 ncm->is_open = true; 1451 ncm_notify(ncm); 1452 spin_unlock(&ncm->lock); 1453 } 1454 1455 static void ncm_close(struct gether *geth) 1456 { 1457 struct f_ncm *ncm = func_to_ncm(&geth->func); 1458 1459 DBG(ncm->port.func.config->cdev, "%s\n", __func__); 1460 1461 spin_lock(&ncm->lock); 1462 ncm->is_open = false; 1463 ncm_notify(ncm); 1464 spin_unlock(&ncm->lock); 1465 } 1466 1467 /*-------------------------------------------------------------------------*/ 1468 1469 /* ethernet function driver setup/binding */ 1470 1471 static int ncm_bind(struct usb_configuration *c, struct usb_function *f) 1472 { 1473 struct usb_composite_dev *cdev = c->cdev; 1474 struct f_ncm *ncm = func_to_ncm(f); 1475 struct usb_string *us; 1476 int status = 0; 1477 struct usb_ep *ep; 1478 struct f_ncm_opts *ncm_opts; 1479 1480 struct usb_os_desc_table *os_desc_table __free(kfree) = NULL; 1481 struct net_device *net __free(detach_gadget) = NULL; 1482 struct usb_request *request __free(free_usb_request) = NULL; 1483 1484 if (!can_support_ecm(cdev->gadget)) 1485 return -EINVAL; 1486 1487 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); 1488 1489 if (cdev->use_os_string) { 1490 os_desc_table = kzalloc_obj(*os_desc_table); 1491 if (!os_desc_table) 1492 return -ENOMEM; 1493 } 1494 1495 scoped_guard(mutex, &ncm_opts->lock) 1496 if (ncm_opts->bind_count == 0) { 1497 if (!device_is_registered(&ncm_opts->net->dev)) { 1498 ncm_opts->net->mtu = (ncm_opts->max_segment_size - ETH_HLEN); 1499 gether_set_gadget(ncm_opts->net, cdev->gadget); 1500 status = gether_register_netdev(ncm_opts->net); 1501 } else 1502 status = gether_attach_gadget(ncm_opts->net, cdev->gadget); 1503 1504 if (status) 1505 return status; 1506 net = ncm_opts->net; 1507 } 1508 1509 ncm_string_defs[1].s = ncm->ethaddr; 1510 1511 us = usb_gstrings_attach(cdev, ncm_strings, 1512 ARRAY_SIZE(ncm_string_defs)); 1513 if (IS_ERR(us)) 1514 return PTR_ERR(us); 1515 1516 ncm_control_intf.iInterface = us[STRING_CTRL_IDX].id; 1517 ncm_data_nop_intf.iInterface = us[STRING_DATA_IDX].id; 1518 ncm_data_intf.iInterface = us[STRING_DATA_IDX].id; 1519 ecm_desc.iMACAddress = us[STRING_MAC_IDX].id; 1520 ncm_iad_desc.iFunction = us[STRING_IAD_IDX].id; 1521 1522 /* allocate instance-specific interface IDs */ 1523 status = usb_interface_id(c, f); 1524 if (status < 0) 1525 return status; 1526 ncm->ctrl_id = status; 1527 ncm_iad_desc.bFirstInterface = status; 1528 1529 ncm_control_intf.bInterfaceNumber = status; 1530 ncm_union_desc.bMasterInterface0 = status; 1531 1532 status = usb_interface_id(c, f); 1533 if (status < 0) 1534 return status; 1535 ncm->data_id = status; 1536 1537 ncm_data_nop_intf.bInterfaceNumber = status; 1538 ncm_data_intf.bInterfaceNumber = status; 1539 ncm_union_desc.bSlaveInterface0 = status; 1540 1541 ecm_desc.wMaxSegmentSize = cpu_to_le16(ncm_opts->max_segment_size); 1542 1543 /* allocate instance-specific endpoints */ 1544 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc); 1545 if (!ep) 1546 return -ENODEV; 1547 ncm->port.in_ep = ep; 1548 1549 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc); 1550 if (!ep) 1551 return -ENODEV; 1552 ncm->port.out_ep = ep; 1553 1554 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc); 1555 if (!ep) 1556 return -ENODEV; 1557 ncm->notify = ep; 1558 1559 /* allocate notification request and buffer */ 1560 request = usb_ep_alloc_request(ep, GFP_KERNEL); 1561 if (!request) 1562 return -ENOMEM; 1563 request->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL); 1564 if (!request->buf) 1565 return -ENOMEM; 1566 request->context = ncm; 1567 request->complete = ncm_notify_complete; 1568 1569 /* 1570 * support all relevant hardware speeds... we expect that when 1571 * hardware is dual speed, all bulk-capable endpoints work at 1572 * both speeds 1573 */ 1574 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress; 1575 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress; 1576 hs_ncm_notify_desc.bEndpointAddress = 1577 fs_ncm_notify_desc.bEndpointAddress; 1578 1579 ss_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress; 1580 ss_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress; 1581 ss_ncm_notify_desc.bEndpointAddress = 1582 fs_ncm_notify_desc.bEndpointAddress; 1583 1584 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function, 1585 ncm_ss_function, ncm_ss_function); 1586 if (status) 1587 return status; 1588 1589 /* 1590 * NOTE: all that is done without knowing or caring about 1591 * the network link ... which is unavailable to this code 1592 * until we're activated via set_alt(). 1593 */ 1594 1595 ncm->port.open = ncm_open; 1596 ncm->port.close = ncm_close; 1597 1598 hrtimer_setup(&ncm->task_timer, ncm_tx_timeout, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); 1599 1600 if (cdev->use_os_string) { 1601 os_desc_table[0].os_desc = &ncm_opts->ncm_os_desc; 1602 os_desc_table[0].if_id = ncm_iad_desc.bFirstInterface; 1603 f->os_desc_table = no_free_ptr(os_desc_table); 1604 f->os_desc_n = 1; 1605 } 1606 ncm->notify_req = no_free_ptr(request); 1607 1608 ncm_opts->bind_count++; 1609 retain_and_null_ptr(net); 1610 1611 DBG(cdev, "CDC Network: IN/%s OUT/%s NOTIFY/%s\n", 1612 ncm->port.in_ep->name, ncm->port.out_ep->name, 1613 ncm->notify->name); 1614 return 0; 1615 } 1616 1617 static inline struct f_ncm_opts *to_f_ncm_opts(struct config_item *item) 1618 { 1619 return container_of(to_config_group(item), struct f_ncm_opts, 1620 func_inst.group); 1621 } 1622 1623 /* f_ncm_item_ops */ 1624 USB_ETHERNET_CONFIGFS_ITEM(ncm); 1625 1626 /* f_ncm_opts_dev_addr */ 1627 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ncm); 1628 1629 /* f_ncm_opts_host_addr */ 1630 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ncm); 1631 1632 /* f_ncm_opts_qmult */ 1633 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ncm); 1634 1635 /* f_ncm_opts_ifname */ 1636 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ncm); 1637 1638 static ssize_t ncm_opts_max_segment_size_show(struct config_item *item, 1639 char *page) 1640 { 1641 struct f_ncm_opts *opts = to_f_ncm_opts(item); 1642 u16 segment_size; 1643 1644 mutex_lock(&opts->lock); 1645 segment_size = opts->max_segment_size; 1646 mutex_unlock(&opts->lock); 1647 1648 return sysfs_emit(page, "%u\n", segment_size); 1649 } 1650 1651 static ssize_t ncm_opts_max_segment_size_store(struct config_item *item, 1652 const char *page, size_t len) 1653 { 1654 struct f_ncm_opts *opts = to_f_ncm_opts(item); 1655 u16 segment_size; 1656 int ret; 1657 1658 mutex_lock(&opts->lock); 1659 if (opts->refcnt) { 1660 ret = -EBUSY; 1661 goto out; 1662 } 1663 1664 ret = kstrtou16(page, 0, &segment_size); 1665 if (ret) 1666 goto out; 1667 1668 if (segment_size > MAX_DATAGRAM_SIZE) { 1669 ret = -EINVAL; 1670 goto out; 1671 } 1672 1673 opts->max_segment_size = segment_size; 1674 ret = len; 1675 out: 1676 mutex_unlock(&opts->lock); 1677 return ret; 1678 } 1679 1680 CONFIGFS_ATTR(ncm_opts_, max_segment_size); 1681 1682 static struct configfs_attribute *ncm_attrs[] = { 1683 &ncm_opts_attr_dev_addr, 1684 &ncm_opts_attr_host_addr, 1685 &ncm_opts_attr_qmult, 1686 &ncm_opts_attr_ifname, 1687 &ncm_opts_attr_max_segment_size, 1688 NULL, 1689 }; 1690 1691 static const struct config_item_type ncm_func_type = { 1692 .ct_item_ops = &ncm_item_ops, 1693 .ct_attrs = ncm_attrs, 1694 .ct_owner = THIS_MODULE, 1695 }; 1696 1697 static void ncm_free_inst(struct usb_function_instance *f) 1698 { 1699 struct f_ncm_opts *opts; 1700 1701 opts = container_of(f, struct f_ncm_opts, func_inst); 1702 if (device_is_registered(&opts->net->dev)) 1703 gether_cleanup(netdev_priv(opts->net)); 1704 else 1705 free_netdev(opts->net); 1706 kfree(opts->ncm_interf_group); 1707 kfree(opts); 1708 } 1709 1710 static struct usb_function_instance *ncm_alloc_inst(void) 1711 { 1712 struct f_ncm_opts *opts; 1713 struct usb_os_desc *descs[1]; 1714 char *names[1]; 1715 struct config_group *ncm_interf_group; 1716 1717 opts = kzalloc_obj(*opts); 1718 if (!opts) 1719 return ERR_PTR(-ENOMEM); 1720 opts->ncm_os_desc.ext_compat_id = opts->ncm_ext_compat_id; 1721 1722 mutex_init(&opts->lock); 1723 opts->func_inst.free_func_inst = ncm_free_inst; 1724 opts->net = gether_setup_default(); 1725 if (IS_ERR(opts->net)) { 1726 struct net_device *net = opts->net; 1727 kfree(opts); 1728 return ERR_CAST(net); 1729 } 1730 opts->max_segment_size = ETH_FRAME_LEN; 1731 INIT_LIST_HEAD(&opts->ncm_os_desc.ext_prop); 1732 1733 descs[0] = &opts->ncm_os_desc; 1734 names[0] = "ncm"; 1735 1736 config_group_init_type_name(&opts->func_inst.group, "", &ncm_func_type); 1737 ncm_interf_group = 1738 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs, 1739 names, THIS_MODULE); 1740 if (IS_ERR(ncm_interf_group)) { 1741 ncm_free_inst(&opts->func_inst); 1742 return ERR_CAST(ncm_interf_group); 1743 } 1744 opts->ncm_interf_group = ncm_interf_group; 1745 1746 return &opts->func_inst; 1747 } 1748 1749 static void ncm_free(struct usb_function *f) 1750 { 1751 struct f_ncm *ncm; 1752 struct f_ncm_opts *opts; 1753 1754 ncm = func_to_ncm(f); 1755 opts = container_of(f->fi, struct f_ncm_opts, func_inst); 1756 kfree(ncm); 1757 mutex_lock(&opts->lock); 1758 opts->refcnt--; 1759 mutex_unlock(&opts->lock); 1760 } 1761 1762 static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) 1763 { 1764 struct f_ncm *ncm = func_to_ncm(f); 1765 struct f_ncm_opts *ncm_opts; 1766 1767 DBG(c->cdev, "ncm unbind\n"); 1768 1769 ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); 1770 1771 hrtimer_cancel(&ncm->task_timer); 1772 1773 kfree(f->os_desc_table); 1774 f->os_desc_table = NULL; 1775 f->os_desc_n = 0; 1776 1777 ncm_string_defs[0].id = 0; 1778 usb_free_all_descriptors(f); 1779 1780 if (atomic_read(&ncm->notify_count)) { 1781 usb_ep_dequeue(ncm->notify, ncm->notify_req); 1782 atomic_set(&ncm->notify_count, 0); 1783 } 1784 1785 kfree(ncm->notify_req->buf); 1786 usb_ep_free_request(ncm->notify, ncm->notify_req); 1787 1788 ncm_opts->bind_count--; 1789 if (ncm_opts->bind_count == 0) 1790 gether_detach_gadget(ncm_opts->net); 1791 } 1792 1793 static struct usb_function *ncm_alloc(struct usb_function_instance *fi) 1794 { 1795 struct f_ncm *ncm; 1796 struct f_ncm_opts *opts; 1797 int status; 1798 1799 /* allocate and initialize one new instance */ 1800 ncm = kzalloc_obj(*ncm); 1801 if (!ncm) 1802 return ERR_PTR(-ENOMEM); 1803 1804 opts = container_of(fi, struct f_ncm_opts, func_inst); 1805 mutex_lock(&opts->lock); 1806 opts->refcnt++; 1807 1808 /* export host's Ethernet address in CDC format */ 1809 status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr, 1810 sizeof(ncm->ethaddr)); 1811 if (status < 12) { /* strlen("01234567890a") */ 1812 kfree(ncm); 1813 mutex_unlock(&opts->lock); 1814 return ERR_PTR(-EINVAL); 1815 } 1816 1817 spin_lock_init(&ncm->lock); 1818 ncm_reset_values(ncm); 1819 ncm->port.ioport = netdev_priv(opts->net); 1820 mutex_unlock(&opts->lock); 1821 ncm->port.is_fixed = true; 1822 ncm->port.supports_multi_frame = true; 1823 1824 ncm->port.func.name = "cdc_network"; 1825 /* descriptors are per-instance copies */ 1826 ncm->port.func.bind = ncm_bind; 1827 ncm->port.func.unbind = ncm_unbind; 1828 ncm->port.func.set_alt = ncm_set_alt; 1829 ncm->port.func.get_alt = ncm_get_alt; 1830 ncm->port.func.setup = ncm_setup; 1831 ncm->port.func.disable = ncm_disable; 1832 ncm->port.func.free_func = ncm_free; 1833 1834 ncm->port.wrap = ncm_wrap_ntb; 1835 ncm->port.unwrap = ncm_unwrap_ntb; 1836 1837 return &ncm->port.func; 1838 } 1839 1840 DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc); 1841 MODULE_DESCRIPTION("USB CDC Network (NCM) link function driver"); 1842 MODULE_LICENSE("GPL"); 1843 MODULE_AUTHOR("Yauheni Kaliuta"); 1844