1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver 4 * 5 * Copyright (C) 2003-2005,2008 David Brownell 6 * Copyright (C) 2008 Nokia Corporation 7 */ 8 9 /* #define VERBOSE_DEBUG */ 10 11 #include <linux/cleanup.h> 12 #include <linux/slab.h> 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/device.h> 16 #include <linux/etherdevice.h> 17 #include <linux/string_choices.h> 18 19 #include <linux/usb/gadget.h> 20 21 #include "u_ether.h" 22 #include "u_ether_configfs.h" 23 #include "u_ecm.h" 24 25 26 /* 27 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM) 28 * Ethernet link. The data transfer model is simple (packets sent and 29 * received over bulk endpoints using normal short packet termination), 30 * and the control model exposes various data and optional notifications. 31 * 32 * ECM is well standardized and (except for Microsoft) supported by most 33 * operating systems with USB host support. It's the preferred interop 34 * solution for Ethernet over USB, at least for firmware based solutions. 35 * (Hardware solutions tend to be more minimalist.) A newer and simpler 36 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on. 37 * 38 * Note that ECM requires the use of "alternate settings" for its data 39 * interface. This means that the set_alt() method has real work to do, 40 * and also means that a get_alt() method is required. 41 */ 42 43 44 enum ecm_notify_state { 45 ECM_NOTIFY_NONE, /* don't notify */ 46 ECM_NOTIFY_CONNECT, /* issue CONNECT next */ 47 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */ 48 }; 49 50 struct f_ecm { 51 struct gether port; 52 u8 ctrl_id, data_id; 53 54 char ethaddr[14]; 55 56 struct usb_ep *notify; 57 struct usb_request *notify_req; 58 u8 notify_state; 59 atomic_t notify_count; 60 bool is_open; 61 62 /* FIXME is_open needs some irq-ish locking 63 * ... possibly the same as port.ioport 64 */ 65 }; 66 67 static inline struct f_ecm *func_to_ecm(struct usb_function *f) 68 { 69 return container_of(f, struct f_ecm, port.func); 70 } 71 72 /*-------------------------------------------------------------------------*/ 73 74 /* 75 * Include the status endpoint if we can, even though it's optional. 76 * 77 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one 78 * packet, to simplify cancellation; and a big transfer interval, to 79 * waste less bandwidth. 80 * 81 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even 82 * if they ignore the connect/disconnect notifications that real aether 83 * can provide. More advanced cdc configurations might want to support 84 * encapsulated commands (vendor-specific, using control-OUT). 85 */ 86 87 #define ECM_STATUS_INTERVAL_MS 32 88 #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */ 89 90 91 /* interface descriptor: */ 92 93 static struct usb_interface_assoc_descriptor 94 ecm_iad_descriptor = { 95 .bLength = sizeof ecm_iad_descriptor, 96 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, 97 98 /* .bFirstInterface = DYNAMIC, */ 99 .bInterfaceCount = 2, /* control + data */ 100 .bFunctionClass = USB_CLASS_COMM, 101 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET, 102 .bFunctionProtocol = USB_CDC_PROTO_NONE, 103 /* .iFunction = DYNAMIC */ 104 }; 105 106 107 static struct usb_interface_descriptor ecm_control_intf = { 108 .bLength = sizeof ecm_control_intf, 109 .bDescriptorType = USB_DT_INTERFACE, 110 111 /* .bInterfaceNumber = DYNAMIC */ 112 /* status endpoint is optional; this could be patched later */ 113 .bNumEndpoints = 1, 114 .bInterfaceClass = USB_CLASS_COMM, 115 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, 116 .bInterfaceProtocol = USB_CDC_PROTO_NONE, 117 /* .iInterface = DYNAMIC */ 118 }; 119 120 static struct usb_cdc_header_desc ecm_header_desc = { 121 .bLength = sizeof ecm_header_desc, 122 .bDescriptorType = USB_DT_CS_INTERFACE, 123 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 124 125 .bcdCDC = cpu_to_le16(0x0110), 126 }; 127 128 static struct usb_cdc_union_desc ecm_union_desc = { 129 .bLength = sizeof(ecm_union_desc), 130 .bDescriptorType = USB_DT_CS_INTERFACE, 131 .bDescriptorSubType = USB_CDC_UNION_TYPE, 132 /* .bMasterInterface0 = DYNAMIC */ 133 /* .bSlaveInterface0 = DYNAMIC */ 134 }; 135 136 static struct usb_cdc_ether_desc ecm_desc = { 137 .bLength = sizeof ecm_desc, 138 .bDescriptorType = USB_DT_CS_INTERFACE, 139 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, 140 141 /* this descriptor actually adds value, surprise! */ 142 /* .iMACAddress = DYNAMIC */ 143 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */ 144 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN), 145 .wNumberMCFilters = cpu_to_le16(0), 146 .bNumberPowerFilters = 0, 147 }; 148 149 /* the default data interface has no endpoints ... */ 150 151 static struct usb_interface_descriptor ecm_data_nop_intf = { 152 .bLength = sizeof ecm_data_nop_intf, 153 .bDescriptorType = USB_DT_INTERFACE, 154 155 .bInterfaceNumber = 1, 156 .bAlternateSetting = 0, 157 .bNumEndpoints = 0, 158 .bInterfaceClass = USB_CLASS_CDC_DATA, 159 .bInterfaceSubClass = 0, 160 .bInterfaceProtocol = 0, 161 /* .iInterface = DYNAMIC */ 162 }; 163 164 /* ... but the "real" data interface has two bulk endpoints */ 165 166 static struct usb_interface_descriptor ecm_data_intf = { 167 .bLength = sizeof ecm_data_intf, 168 .bDescriptorType = USB_DT_INTERFACE, 169 170 .bInterfaceNumber = 1, 171 .bAlternateSetting = 1, 172 .bNumEndpoints = 2, 173 .bInterfaceClass = USB_CLASS_CDC_DATA, 174 .bInterfaceSubClass = 0, 175 .bInterfaceProtocol = 0, 176 /* .iInterface = DYNAMIC */ 177 }; 178 179 /* full speed support: */ 180 181 static struct usb_endpoint_descriptor fs_ecm_notify_desc = { 182 .bLength = USB_DT_ENDPOINT_SIZE, 183 .bDescriptorType = USB_DT_ENDPOINT, 184 185 .bEndpointAddress = USB_DIR_IN, 186 .bmAttributes = USB_ENDPOINT_XFER_INT, 187 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT), 188 .bInterval = ECM_STATUS_INTERVAL_MS, 189 }; 190 191 static struct usb_endpoint_descriptor fs_ecm_in_desc = { 192 .bLength = USB_DT_ENDPOINT_SIZE, 193 .bDescriptorType = USB_DT_ENDPOINT, 194 195 .bEndpointAddress = USB_DIR_IN, 196 .bmAttributes = USB_ENDPOINT_XFER_BULK, 197 }; 198 199 static struct usb_endpoint_descriptor fs_ecm_out_desc = { 200 .bLength = USB_DT_ENDPOINT_SIZE, 201 .bDescriptorType = USB_DT_ENDPOINT, 202 203 .bEndpointAddress = USB_DIR_OUT, 204 .bmAttributes = USB_ENDPOINT_XFER_BULK, 205 }; 206 207 static struct usb_descriptor_header *ecm_fs_function[] = { 208 /* CDC ECM control descriptors */ 209 (struct usb_descriptor_header *) &ecm_iad_descriptor, 210 (struct usb_descriptor_header *) &ecm_control_intf, 211 (struct usb_descriptor_header *) &ecm_header_desc, 212 (struct usb_descriptor_header *) &ecm_union_desc, 213 (struct usb_descriptor_header *) &ecm_desc, 214 215 /* NOTE: status endpoint might need to be removed */ 216 (struct usb_descriptor_header *) &fs_ecm_notify_desc, 217 218 /* data interface, altsettings 0 and 1 */ 219 (struct usb_descriptor_header *) &ecm_data_nop_intf, 220 (struct usb_descriptor_header *) &ecm_data_intf, 221 (struct usb_descriptor_header *) &fs_ecm_in_desc, 222 (struct usb_descriptor_header *) &fs_ecm_out_desc, 223 NULL, 224 }; 225 226 /* high speed support: */ 227 228 static struct usb_endpoint_descriptor hs_ecm_notify_desc = { 229 .bLength = USB_DT_ENDPOINT_SIZE, 230 .bDescriptorType = USB_DT_ENDPOINT, 231 232 .bEndpointAddress = USB_DIR_IN, 233 .bmAttributes = USB_ENDPOINT_XFER_INT, 234 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT), 235 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS), 236 }; 237 238 static struct usb_endpoint_descriptor hs_ecm_in_desc = { 239 .bLength = USB_DT_ENDPOINT_SIZE, 240 .bDescriptorType = USB_DT_ENDPOINT, 241 242 .bEndpointAddress = USB_DIR_IN, 243 .bmAttributes = USB_ENDPOINT_XFER_BULK, 244 .wMaxPacketSize = cpu_to_le16(512), 245 }; 246 247 static struct usb_endpoint_descriptor hs_ecm_out_desc = { 248 .bLength = USB_DT_ENDPOINT_SIZE, 249 .bDescriptorType = USB_DT_ENDPOINT, 250 251 .bEndpointAddress = USB_DIR_OUT, 252 .bmAttributes = USB_ENDPOINT_XFER_BULK, 253 .wMaxPacketSize = cpu_to_le16(512), 254 }; 255 256 static struct usb_descriptor_header *ecm_hs_function[] = { 257 /* CDC ECM control descriptors */ 258 (struct usb_descriptor_header *) &ecm_iad_descriptor, 259 (struct usb_descriptor_header *) &ecm_control_intf, 260 (struct usb_descriptor_header *) &ecm_header_desc, 261 (struct usb_descriptor_header *) &ecm_union_desc, 262 (struct usb_descriptor_header *) &ecm_desc, 263 264 /* NOTE: status endpoint might need to be removed */ 265 (struct usb_descriptor_header *) &hs_ecm_notify_desc, 266 267 /* data interface, altsettings 0 and 1 */ 268 (struct usb_descriptor_header *) &ecm_data_nop_intf, 269 (struct usb_descriptor_header *) &ecm_data_intf, 270 (struct usb_descriptor_header *) &hs_ecm_in_desc, 271 (struct usb_descriptor_header *) &hs_ecm_out_desc, 272 NULL, 273 }; 274 275 /* super speed support: */ 276 277 static struct usb_endpoint_descriptor ss_ecm_notify_desc = { 278 .bLength = USB_DT_ENDPOINT_SIZE, 279 .bDescriptorType = USB_DT_ENDPOINT, 280 281 .bEndpointAddress = USB_DIR_IN, 282 .bmAttributes = USB_ENDPOINT_XFER_INT, 283 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT), 284 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS), 285 }; 286 287 static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = { 288 .bLength = sizeof ss_ecm_intr_comp_desc, 289 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 290 291 /* the following 3 values can be tweaked if necessary */ 292 /* .bMaxBurst = 0, */ 293 /* .bmAttributes = 0, */ 294 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT), 295 }; 296 297 static struct usb_endpoint_descriptor ss_ecm_in_desc = { 298 .bLength = USB_DT_ENDPOINT_SIZE, 299 .bDescriptorType = USB_DT_ENDPOINT, 300 301 .bEndpointAddress = USB_DIR_IN, 302 .bmAttributes = USB_ENDPOINT_XFER_BULK, 303 .wMaxPacketSize = cpu_to_le16(1024), 304 }; 305 306 static struct usb_endpoint_descriptor ss_ecm_out_desc = { 307 .bLength = USB_DT_ENDPOINT_SIZE, 308 .bDescriptorType = USB_DT_ENDPOINT, 309 310 .bEndpointAddress = USB_DIR_OUT, 311 .bmAttributes = USB_ENDPOINT_XFER_BULK, 312 .wMaxPacketSize = cpu_to_le16(1024), 313 }; 314 315 static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = { 316 .bLength = sizeof ss_ecm_bulk_comp_desc, 317 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 318 319 /* the following 2 values can be tweaked if necessary */ 320 /* .bMaxBurst = 0, */ 321 /* .bmAttributes = 0, */ 322 }; 323 324 static struct usb_descriptor_header *ecm_ss_function[] = { 325 /* CDC ECM control descriptors */ 326 (struct usb_descriptor_header *) &ecm_iad_descriptor, 327 (struct usb_descriptor_header *) &ecm_control_intf, 328 (struct usb_descriptor_header *) &ecm_header_desc, 329 (struct usb_descriptor_header *) &ecm_union_desc, 330 (struct usb_descriptor_header *) &ecm_desc, 331 332 /* NOTE: status endpoint might need to be removed */ 333 (struct usb_descriptor_header *) &ss_ecm_notify_desc, 334 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc, 335 336 /* data interface, altsettings 0 and 1 */ 337 (struct usb_descriptor_header *) &ecm_data_nop_intf, 338 (struct usb_descriptor_header *) &ecm_data_intf, 339 (struct usb_descriptor_header *) &ss_ecm_in_desc, 340 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc, 341 (struct usb_descriptor_header *) &ss_ecm_out_desc, 342 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc, 343 NULL, 344 }; 345 346 /* string descriptors: */ 347 348 static struct usb_string ecm_string_defs[] = { 349 [0].s = "CDC Ethernet Control Model (ECM)", 350 [1].s = "", 351 [2].s = "CDC Ethernet Data", 352 [3].s = "CDC ECM", 353 { } /* end of list */ 354 }; 355 356 static struct usb_gadget_strings ecm_string_table = { 357 .language = 0x0409, /* en-us */ 358 .strings = ecm_string_defs, 359 }; 360 361 static struct usb_gadget_strings *ecm_strings[] = { 362 &ecm_string_table, 363 NULL, 364 }; 365 366 /*-------------------------------------------------------------------------*/ 367 368 static void ecm_do_notify(struct f_ecm *ecm) 369 { 370 struct usb_request *req = ecm->notify_req; 371 struct usb_cdc_notification *event; 372 struct usb_composite_dev *cdev = ecm->port.func.config->cdev; 373 __le32 *data; 374 int status; 375 376 /* notification already in flight? */ 377 if (atomic_read(&ecm->notify_count)) 378 return; 379 380 event = req->buf; 381 switch (ecm->notify_state) { 382 case ECM_NOTIFY_NONE: 383 return; 384 385 case ECM_NOTIFY_CONNECT: 386 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; 387 if (ecm->is_open) 388 event->wValue = cpu_to_le16(1); 389 else 390 event->wValue = cpu_to_le16(0); 391 event->wLength = 0; 392 req->length = sizeof *event; 393 394 DBG(cdev, "notify connect %s\n", str_true_false(ecm->is_open)); 395 ecm->notify_state = ECM_NOTIFY_SPEED; 396 break; 397 398 case ECM_NOTIFY_SPEED: 399 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; 400 event->wValue = cpu_to_le16(0); 401 event->wLength = cpu_to_le16(8); 402 req->length = ECM_STATUS_BYTECOUNT; 403 404 /* SPEED_CHANGE data is up/down speeds in bits/sec */ 405 data = req->buf + sizeof *event; 406 data[0] = cpu_to_le32(gether_bitrate(cdev->gadget)); 407 data[1] = data[0]; 408 409 DBG(cdev, "notify speed %d\n", gether_bitrate(cdev->gadget)); 410 ecm->notify_state = ECM_NOTIFY_NONE; 411 break; 412 } 413 event->bmRequestType = 0xA1; 414 event->wIndex = cpu_to_le16(ecm->ctrl_id); 415 416 atomic_inc(&ecm->notify_count); 417 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC); 418 if (status < 0) { 419 atomic_dec(&ecm->notify_count); 420 DBG(cdev, "notify --> %d\n", status); 421 } 422 } 423 424 static void ecm_notify(struct f_ecm *ecm) 425 { 426 /* NOTE on most versions of Linux, host side cdc-ethernet 427 * won't listen for notifications until its netdevice opens. 428 * The first notification then sits in the FIFO for a long 429 * time, and the second one is queued. 430 */ 431 ecm->notify_state = ECM_NOTIFY_CONNECT; 432 ecm_do_notify(ecm); 433 } 434 435 static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req) 436 { 437 struct f_ecm *ecm = req->context; 438 struct usb_composite_dev *cdev = ecm->port.func.config->cdev; 439 struct usb_cdc_notification *event = req->buf; 440 441 switch (req->status) { 442 case 0: 443 /* no fault */ 444 atomic_dec(&ecm->notify_count); 445 break; 446 case -ECONNRESET: 447 case -ESHUTDOWN: 448 atomic_set(&ecm->notify_count, 0); 449 ecm->notify_state = ECM_NOTIFY_NONE; 450 break; 451 default: 452 DBG(cdev, "event %02x --> %d\n", 453 event->bNotificationType, req->status); 454 atomic_dec(&ecm->notify_count); 455 break; 456 } 457 ecm_do_notify(ecm); 458 } 459 460 static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 461 { 462 struct f_ecm *ecm = func_to_ecm(f); 463 struct usb_composite_dev *cdev = f->config->cdev; 464 struct usb_request *req = cdev->req; 465 int value = -EOPNOTSUPP; 466 u16 w_index = le16_to_cpu(ctrl->wIndex); 467 u16 w_value = le16_to_cpu(ctrl->wValue); 468 u16 w_length = le16_to_cpu(ctrl->wLength); 469 470 /* composite driver infrastructure handles everything except 471 * CDC class messages; interface activation uses set_alt(). 472 */ 473 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { 474 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) 475 | USB_CDC_SET_ETHERNET_PACKET_FILTER: 476 /* see 6.2.30: no data, wIndex = interface, 477 * wValue = packet filter bitmap 478 */ 479 if (w_length != 0 || w_index != ecm->ctrl_id) 480 goto invalid; 481 DBG(cdev, "packet filter %02x\n", w_value); 482 /* REVISIT locking of cdc_filter. This assumes the UDC 483 * driver won't have a concurrent packet TX irq running on 484 * another CPU; or that if it does, this write is atomic... 485 */ 486 ecm->port.cdc_filter = w_value; 487 value = 0; 488 break; 489 490 /* and optionally: 491 * case USB_CDC_SEND_ENCAPSULATED_COMMAND: 492 * case USB_CDC_GET_ENCAPSULATED_RESPONSE: 493 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: 494 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: 495 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: 496 * case USB_CDC_GET_ETHERNET_STATISTIC: 497 */ 498 499 default: 500 invalid: 501 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 502 ctrl->bRequestType, ctrl->bRequest, 503 w_value, w_index, w_length); 504 } 505 506 /* respond with data transfer or status phase? */ 507 if (value >= 0) { 508 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n", 509 ctrl->bRequestType, ctrl->bRequest, 510 w_value, w_index, w_length); 511 req->zero = 0; 512 req->length = value; 513 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); 514 if (value < 0) 515 ERROR(cdev, "ecm req %02x.%02x response err %d\n", 516 ctrl->bRequestType, ctrl->bRequest, 517 value); 518 } 519 520 /* device either stalls (value < 0) or reports success */ 521 return value; 522 } 523 524 525 static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 526 { 527 struct f_ecm *ecm = func_to_ecm(f); 528 struct usb_composite_dev *cdev = f->config->cdev; 529 530 /* Control interface has only altsetting 0 */ 531 if (intf == ecm->ctrl_id) { 532 if (alt != 0) 533 goto fail; 534 535 VDBG(cdev, "reset ecm control %d\n", intf); 536 usb_ep_disable(ecm->notify); 537 if (!(ecm->notify->desc)) { 538 VDBG(cdev, "init ecm ctrl %d\n", intf); 539 if (config_ep_by_speed(cdev->gadget, f, ecm->notify)) 540 goto fail; 541 } 542 usb_ep_enable(ecm->notify); 543 544 /* Data interface has two altsettings, 0 and 1 */ 545 } else if (intf == ecm->data_id) { 546 if (alt > 1) 547 goto fail; 548 549 if (ecm->port.in_ep->enabled) { 550 DBG(cdev, "reset ecm\n"); 551 gether_disconnect(&ecm->port); 552 } 553 554 if (!ecm->port.in_ep->desc || 555 !ecm->port.out_ep->desc) { 556 DBG(cdev, "init ecm\n"); 557 if (config_ep_by_speed(cdev->gadget, f, 558 ecm->port.in_ep) || 559 config_ep_by_speed(cdev->gadget, f, 560 ecm->port.out_ep)) { 561 ecm->port.in_ep->desc = NULL; 562 ecm->port.out_ep->desc = NULL; 563 goto fail; 564 } 565 } 566 567 /* CDC Ethernet only sends data in non-default altsettings. 568 * Changing altsettings resets filters, statistics, etc. 569 */ 570 if (alt == 1) { 571 struct net_device *net; 572 573 /* Enable zlps by default for ECM conformance; 574 * override for musb_hdrc (avoids txdma ovhead). 575 */ 576 ecm->port.is_zlp_ok = 577 gadget_is_zlp_supported(cdev->gadget); 578 ecm->port.cdc_filter = DEFAULT_FILTER; 579 DBG(cdev, "activate ecm\n"); 580 net = gether_connect(&ecm->port); 581 if (IS_ERR(net)) 582 return PTR_ERR(net); 583 } 584 } else 585 goto fail; 586 587 return 0; 588 fail: 589 return -EINVAL; 590 } 591 592 /* Because the data interface supports multiple altsettings, 593 * this ECM function *MUST* implement a get_alt() method. 594 */ 595 static int ecm_get_alt(struct usb_function *f, unsigned intf) 596 { 597 struct f_ecm *ecm = func_to_ecm(f); 598 599 if (intf == ecm->ctrl_id) 600 return 0; 601 return ecm->port.in_ep->enabled ? 1 : 0; 602 } 603 604 static void ecm_disable(struct usb_function *f) 605 { 606 struct f_ecm *ecm = func_to_ecm(f); 607 struct usb_composite_dev *cdev = f->config->cdev; 608 609 DBG(cdev, "ecm deactivated\n"); 610 611 if (ecm->port.in_ep->enabled) { 612 gether_disconnect(&ecm->port); 613 } else { 614 ecm->port.in_ep->desc = NULL; 615 ecm->port.out_ep->desc = NULL; 616 } 617 618 usb_ep_disable(ecm->notify); 619 ecm->notify->desc = NULL; 620 } 621 622 /*-------------------------------------------------------------------------*/ 623 624 /* 625 * Callbacks let us notify the host about connect/disconnect when the 626 * net device is opened or closed. 627 * 628 * For testing, note that link states on this side include both opened 629 * and closed variants of: 630 * 631 * - disconnected/unconfigured 632 * - configured but inactive (data alt 0) 633 * - configured and active (data alt 1) 634 * 635 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and 636 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't 637 * imply the host is actually polling the notification endpoint, and 638 * likewise that "active" doesn't imply it's actually using the data 639 * endpoints for traffic. 640 */ 641 642 static void ecm_open(struct gether *geth) 643 { 644 struct f_ecm *ecm = func_to_ecm(&geth->func); 645 646 DBG(ecm->port.func.config->cdev, "%s\n", __func__); 647 648 ecm->is_open = true; 649 ecm_notify(ecm); 650 } 651 652 static void ecm_close(struct gether *geth) 653 { 654 struct f_ecm *ecm = func_to_ecm(&geth->func); 655 656 DBG(ecm->port.func.config->cdev, "%s\n", __func__); 657 658 ecm->is_open = false; 659 ecm_notify(ecm); 660 } 661 662 /*-------------------------------------------------------------------------*/ 663 664 /* ethernet function driver setup/binding */ 665 666 static int 667 ecm_bind(struct usb_configuration *c, struct usb_function *f) 668 { 669 struct usb_composite_dev *cdev = c->cdev; 670 struct f_ecm *ecm = func_to_ecm(f); 671 struct usb_string *us; 672 int status = 0; 673 struct usb_ep *ep; 674 675 struct f_ecm_opts *ecm_opts; 676 struct net_device *net __free(detach_gadget) = NULL; 677 struct usb_request *request __free(free_usb_request) = NULL; 678 679 if (!can_support_ecm(cdev->gadget)) 680 return -EINVAL; 681 682 ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst); 683 684 scoped_guard(mutex, &ecm_opts->lock) 685 if (ecm_opts->bind_count == 0 && !ecm_opts->bound) { 686 if (!device_is_registered(&ecm_opts->net->dev)) { 687 gether_set_gadget(ecm_opts->net, cdev->gadget); 688 status = gether_register_netdev(ecm_opts->net); 689 } else 690 status = gether_attach_gadget(ecm_opts->net, cdev->gadget); 691 692 if (status) 693 return status; 694 net = ecm_opts->net; 695 } 696 697 ecm_string_defs[1].s = ecm->ethaddr; 698 699 us = usb_gstrings_attach(cdev, ecm_strings, 700 ARRAY_SIZE(ecm_string_defs)); 701 if (IS_ERR(us)) 702 return PTR_ERR(us); 703 ecm_control_intf.iInterface = us[0].id; 704 ecm_data_intf.iInterface = us[2].id; 705 ecm_desc.iMACAddress = us[1].id; 706 ecm_iad_descriptor.iFunction = us[3].id; 707 708 /* allocate instance-specific interface IDs */ 709 status = usb_interface_id(c, f); 710 if (status < 0) 711 return status; 712 ecm->ctrl_id = status; 713 ecm_iad_descriptor.bFirstInterface = status; 714 715 ecm_control_intf.bInterfaceNumber = status; 716 ecm_union_desc.bMasterInterface0 = status; 717 718 status = usb_interface_id(c, f); 719 if (status < 0) 720 return status; 721 ecm->data_id = status; 722 723 ecm_data_nop_intf.bInterfaceNumber = status; 724 ecm_data_intf.bInterfaceNumber = status; 725 ecm_union_desc.bSlaveInterface0 = status; 726 727 /* allocate instance-specific endpoints */ 728 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc); 729 if (!ep) 730 return -ENODEV; 731 ecm->port.in_ep = ep; 732 733 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc); 734 if (!ep) 735 return -ENODEV; 736 ecm->port.out_ep = ep; 737 738 /* NOTE: a status/notification endpoint is *OPTIONAL* but we 739 * don't treat it that way. It's simpler, and some newer CDC 740 * profiles (wireless handsets) no longer treat it as optional. 741 */ 742 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc); 743 if (!ep) 744 return -ENODEV; 745 ecm->notify = ep; 746 747 /* allocate notification request and buffer */ 748 request = usb_ep_alloc_request(ep, GFP_KERNEL); 749 if (!request) 750 return -ENOMEM; 751 request->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL); 752 if (!request->buf) 753 return -ENOMEM; 754 request->context = ecm; 755 request->complete = ecm_notify_complete; 756 757 /* support all relevant hardware speeds... we expect that when 758 * hardware is dual speed, all bulk-capable endpoints work at 759 * both speeds 760 */ 761 hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress; 762 hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress; 763 hs_ecm_notify_desc.bEndpointAddress = 764 fs_ecm_notify_desc.bEndpointAddress; 765 766 ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress; 767 ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress; 768 ss_ecm_notify_desc.bEndpointAddress = 769 fs_ecm_notify_desc.bEndpointAddress; 770 771 status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function, 772 ecm_ss_function, ecm_ss_function); 773 if (status) 774 return status; 775 776 /* NOTE: all that is done without knowing or caring about 777 * the network link ... which is unavailable to this code 778 * until we're activated via set_alt(). 779 */ 780 781 ecm->port.open = ecm_open; 782 ecm->port.close = ecm_close; 783 784 ecm->notify_req = no_free_ptr(request); 785 786 ecm_opts->bind_count++; 787 retain_and_null_ptr(net); 788 789 DBG(cdev, "CDC Ethernet: IN/%s OUT/%s NOTIFY/%s\n", 790 ecm->port.in_ep->name, ecm->port.out_ep->name, 791 ecm->notify->name); 792 return 0; 793 } 794 795 static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item) 796 { 797 return container_of(to_config_group(item), struct f_ecm_opts, 798 func_inst.group); 799 } 800 801 /* f_ecm_item_ops */ 802 USB_ETHERNET_CONFIGFS_ITEM(ecm); 803 804 /* f_ecm_opts_dev_addr */ 805 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm); 806 807 /* f_ecm_opts_host_addr */ 808 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm); 809 810 /* f_ecm_opts_qmult */ 811 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm); 812 813 /* f_ecm_opts_ifname */ 814 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm); 815 816 static struct configfs_attribute *ecm_attrs[] = { 817 &ecm_opts_attr_dev_addr, 818 &ecm_opts_attr_host_addr, 819 &ecm_opts_attr_qmult, 820 &ecm_opts_attr_ifname, 821 NULL, 822 }; 823 824 static const struct config_item_type ecm_func_type = { 825 .ct_item_ops = &ecm_item_ops, 826 .ct_attrs = ecm_attrs, 827 .ct_owner = THIS_MODULE, 828 }; 829 830 static void ecm_free_inst(struct usb_function_instance *f) 831 { 832 struct f_ecm_opts *opts; 833 834 opts = container_of(f, struct f_ecm_opts, func_inst); 835 if (device_is_registered(&opts->net->dev)) 836 gether_cleanup(netdev_priv(opts->net)); 837 else 838 free_netdev(opts->net); 839 kfree(opts); 840 } 841 842 static struct usb_function_instance *ecm_alloc_inst(void) 843 { 844 struct f_ecm_opts *opts; 845 846 opts = kzalloc_obj(*opts); 847 if (!opts) 848 return ERR_PTR(-ENOMEM); 849 mutex_init(&opts->lock); 850 opts->func_inst.free_func_inst = ecm_free_inst; 851 opts->net = gether_setup_default(); 852 if (IS_ERR(opts->net)) { 853 struct net_device *net = opts->net; 854 kfree(opts); 855 return ERR_CAST(net); 856 } 857 858 config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type); 859 860 return &opts->func_inst; 861 } 862 863 static void ecm_suspend(struct usb_function *f) 864 { 865 struct f_ecm *ecm = func_to_ecm(f); 866 struct usb_composite_dev *cdev = ecm->port.func.config->cdev; 867 868 DBG(cdev, "ECM Suspend\n"); 869 870 gether_suspend(&ecm->port); 871 } 872 873 static void ecm_resume(struct usb_function *f) 874 { 875 struct f_ecm *ecm = func_to_ecm(f); 876 struct usb_composite_dev *cdev = ecm->port.func.config->cdev; 877 878 DBG(cdev, "ECM Resume\n"); 879 880 gether_resume(&ecm->port); 881 } 882 883 static int ecm_get_status(struct usb_function *f) 884 { 885 return (f->func_wakeup_armed ? USB_INTRF_STAT_FUNC_RW : 0) | 886 USB_INTRF_STAT_FUNC_RW_CAP; 887 } 888 889 static void ecm_free(struct usb_function *f) 890 { 891 struct f_ecm *ecm; 892 struct f_ecm_opts *opts; 893 894 ecm = func_to_ecm(f); 895 opts = container_of(f->fi, struct f_ecm_opts, func_inst); 896 kfree(ecm); 897 mutex_lock(&opts->lock); 898 opts->refcnt--; 899 mutex_unlock(&opts->lock); 900 } 901 902 static void ecm_unbind(struct usb_configuration *c, struct usb_function *f) 903 { 904 struct f_ecm *ecm = func_to_ecm(f); 905 struct f_ecm_opts *ecm_opts; 906 907 DBG(c->cdev, "ecm unbind\n"); 908 909 ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst); 910 911 usb_free_all_descriptors(f); 912 913 if (atomic_read(&ecm->notify_count)) { 914 usb_ep_dequeue(ecm->notify, ecm->notify_req); 915 atomic_set(&ecm->notify_count, 0); 916 } 917 918 kfree(ecm->notify_req->buf); 919 usb_ep_free_request(ecm->notify, ecm->notify_req); 920 921 ecm_opts->bind_count--; 922 if (ecm_opts->bind_count == 0 && !ecm_opts->bound) 923 gether_detach_gadget(ecm_opts->net); 924 } 925 926 static struct usb_function *ecm_alloc(struct usb_function_instance *fi) 927 { 928 struct f_ecm *ecm; 929 struct f_ecm_opts *opts; 930 int status; 931 932 /* allocate and initialize one new instance */ 933 ecm = kzalloc_obj(*ecm); 934 if (!ecm) 935 return ERR_PTR(-ENOMEM); 936 937 opts = container_of(fi, struct f_ecm_opts, func_inst); 938 mutex_lock(&opts->lock); 939 opts->refcnt++; 940 941 /* export host's Ethernet address in CDC format */ 942 status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr, 943 sizeof(ecm->ethaddr)); 944 if (status < 12) { 945 kfree(ecm); 946 mutex_unlock(&opts->lock); 947 return ERR_PTR(-EINVAL); 948 } 949 950 ecm->port.ioport = netdev_priv(opts->net); 951 mutex_unlock(&opts->lock); 952 ecm->port.cdc_filter = DEFAULT_FILTER; 953 954 ecm->port.func.name = "cdc_ethernet"; 955 /* descriptors are per-instance copies */ 956 ecm->port.func.bind = ecm_bind; 957 ecm->port.func.unbind = ecm_unbind; 958 ecm->port.func.set_alt = ecm_set_alt; 959 ecm->port.func.get_alt = ecm_get_alt; 960 ecm->port.func.setup = ecm_setup; 961 ecm->port.func.disable = ecm_disable; 962 ecm->port.func.free_func = ecm_free; 963 ecm->port.func.suspend = ecm_suspend; 964 ecm->port.func.get_status = ecm_get_status; 965 ecm->port.func.resume = ecm_resume; 966 967 return &ecm->port.func; 968 } 969 970 DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc); 971 MODULE_DESCRIPTION("USB CDC Ethernet (ECM) link function driver"); 972 MODULE_LICENSE("GPL"); 973 MODULE_AUTHOR("David Brownell"); 974