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