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