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