1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_eem.c -- USB CDC Ethernet (EEM) link function driver 4 * 5 * Copyright (C) 2003-2005,2008 David Brownell 6 * Copyright (C) 2008 Nokia Corporation 7 * Copyright (C) 2009 EF Johnson Technologies 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 */ 14 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/device.h> 18 #include <linux/etherdevice.h> 19 #include <linux/crc32.h> 20 #include <linux/slab.h> 21 22 #include "u_ether.h" 23 #include "u_ether_configfs.h" 24 #include "u_eem.h" 25 26 #define EEM_HLEN 2 27 28 /* 29 * This function is a "CDC Ethernet Emulation Model" (CDC EEM) 30 * Ethernet link. 31 */ 32 33 struct f_eem { 34 struct gether port; 35 u8 ctrl_id; 36 }; 37 38 static inline struct f_eem *func_to_eem(struct usb_function *f) 39 { 40 return container_of(f, struct f_eem, port.func); 41 } 42 43 /*-------------------------------------------------------------------------*/ 44 45 /* interface descriptor: */ 46 47 static struct usb_interface_descriptor eem_intf = { 48 .bLength = sizeof eem_intf, 49 .bDescriptorType = USB_DT_INTERFACE, 50 51 /* .bInterfaceNumber = DYNAMIC */ 52 .bNumEndpoints = 2, 53 .bInterfaceClass = USB_CLASS_COMM, 54 .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM, 55 .bInterfaceProtocol = USB_CDC_PROTO_EEM, 56 /* .iInterface = DYNAMIC */ 57 }; 58 59 /* full speed support: */ 60 61 static struct usb_endpoint_descriptor eem_fs_in_desc = { 62 .bLength = USB_DT_ENDPOINT_SIZE, 63 .bDescriptorType = USB_DT_ENDPOINT, 64 65 .bEndpointAddress = USB_DIR_IN, 66 .bmAttributes = USB_ENDPOINT_XFER_BULK, 67 }; 68 69 static struct usb_endpoint_descriptor eem_fs_out_desc = { 70 .bLength = USB_DT_ENDPOINT_SIZE, 71 .bDescriptorType = USB_DT_ENDPOINT, 72 73 .bEndpointAddress = USB_DIR_OUT, 74 .bmAttributes = USB_ENDPOINT_XFER_BULK, 75 }; 76 77 static struct usb_descriptor_header *eem_fs_function[] = { 78 /* CDC EEM control descriptors */ 79 (struct usb_descriptor_header *) &eem_intf, 80 (struct usb_descriptor_header *) &eem_fs_in_desc, 81 (struct usb_descriptor_header *) &eem_fs_out_desc, 82 NULL, 83 }; 84 85 /* high speed support: */ 86 87 static struct usb_endpoint_descriptor eem_hs_in_desc = { 88 .bLength = USB_DT_ENDPOINT_SIZE, 89 .bDescriptorType = USB_DT_ENDPOINT, 90 91 .bEndpointAddress = USB_DIR_IN, 92 .bmAttributes = USB_ENDPOINT_XFER_BULK, 93 .wMaxPacketSize = cpu_to_le16(512), 94 }; 95 96 static struct usb_endpoint_descriptor eem_hs_out_desc = { 97 .bLength = USB_DT_ENDPOINT_SIZE, 98 .bDescriptorType = USB_DT_ENDPOINT, 99 100 .bEndpointAddress = USB_DIR_OUT, 101 .bmAttributes = USB_ENDPOINT_XFER_BULK, 102 .wMaxPacketSize = cpu_to_le16(512), 103 }; 104 105 static struct usb_descriptor_header *eem_hs_function[] = { 106 /* CDC EEM control descriptors */ 107 (struct usb_descriptor_header *) &eem_intf, 108 (struct usb_descriptor_header *) &eem_hs_in_desc, 109 (struct usb_descriptor_header *) &eem_hs_out_desc, 110 NULL, 111 }; 112 113 /* super speed support: */ 114 115 static struct usb_endpoint_descriptor eem_ss_in_desc = { 116 .bLength = USB_DT_ENDPOINT_SIZE, 117 .bDescriptorType = USB_DT_ENDPOINT, 118 119 .bEndpointAddress = USB_DIR_IN, 120 .bmAttributes = USB_ENDPOINT_XFER_BULK, 121 .wMaxPacketSize = cpu_to_le16(1024), 122 }; 123 124 static struct usb_endpoint_descriptor eem_ss_out_desc = { 125 .bLength = USB_DT_ENDPOINT_SIZE, 126 .bDescriptorType = USB_DT_ENDPOINT, 127 128 .bEndpointAddress = USB_DIR_OUT, 129 .bmAttributes = USB_ENDPOINT_XFER_BULK, 130 .wMaxPacketSize = cpu_to_le16(1024), 131 }; 132 133 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc = { 134 .bLength = sizeof eem_ss_bulk_comp_desc, 135 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 136 137 /* the following 2 values can be tweaked if necessary */ 138 /* .bMaxBurst = 0, */ 139 /* .bmAttributes = 0, */ 140 }; 141 142 static struct usb_descriptor_header *eem_ss_function[] = { 143 /* CDC EEM control descriptors */ 144 (struct usb_descriptor_header *) &eem_intf, 145 (struct usb_descriptor_header *) &eem_ss_in_desc, 146 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc, 147 (struct usb_descriptor_header *) &eem_ss_out_desc, 148 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc, 149 NULL, 150 }; 151 152 /* string descriptors: */ 153 154 static struct usb_string eem_string_defs[] = { 155 [0].s = "CDC Ethernet Emulation Model (EEM)", 156 { } /* end of list */ 157 }; 158 159 static struct usb_gadget_strings eem_string_table = { 160 .language = 0x0409, /* en-us */ 161 .strings = eem_string_defs, 162 }; 163 164 static struct usb_gadget_strings *eem_strings[] = { 165 &eem_string_table, 166 NULL, 167 }; 168 169 /*-------------------------------------------------------------------------*/ 170 171 static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) 172 { 173 struct usb_composite_dev *cdev = f->config->cdev; 174 int value = -EOPNOTSUPP; 175 u16 w_index = le16_to_cpu(ctrl->wIndex); 176 u16 w_value = le16_to_cpu(ctrl->wValue); 177 u16 w_length = le16_to_cpu(ctrl->wLength); 178 179 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", 180 ctrl->bRequestType, ctrl->bRequest, 181 w_value, w_index, w_length); 182 183 /* device either stalls (value < 0) or reports success */ 184 return value; 185 } 186 187 188 static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 189 { 190 struct f_eem *eem = func_to_eem(f); 191 struct usb_composite_dev *cdev = f->config->cdev; 192 struct net_device *net; 193 194 /* we know alt == 0, so this is an activation or a reset */ 195 if (alt != 0) 196 goto fail; 197 198 if (intf == eem->ctrl_id) { 199 DBG(cdev, "reset eem\n"); 200 gether_disconnect(&eem->port); 201 202 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) { 203 DBG(cdev, "init eem\n"); 204 if (config_ep_by_speed(cdev->gadget, f, 205 eem->port.in_ep) || 206 config_ep_by_speed(cdev->gadget, f, 207 eem->port.out_ep)) { 208 eem->port.in_ep->desc = NULL; 209 eem->port.out_ep->desc = NULL; 210 goto fail; 211 } 212 } 213 214 /* zlps should not occur because zero-length EEM packets 215 * will be inserted in those cases where they would occur 216 */ 217 eem->port.is_zlp_ok = 1; 218 eem->port.cdc_filter = DEFAULT_FILTER; 219 DBG(cdev, "activate eem\n"); 220 net = gether_connect(&eem->port); 221 if (IS_ERR(net)) 222 return PTR_ERR(net); 223 } else 224 goto fail; 225 226 return 0; 227 fail: 228 return -EINVAL; 229 } 230 231 static void eem_disable(struct usb_function *f) 232 { 233 struct f_eem *eem = func_to_eem(f); 234 struct usb_composite_dev *cdev = f->config->cdev; 235 236 DBG(cdev, "eem deactivated\n"); 237 238 if (eem->port.in_ep->enabled) 239 gether_disconnect(&eem->port); 240 } 241 242 /*-------------------------------------------------------------------------*/ 243 244 /* EEM function driver setup/binding */ 245 246 static int eem_bind(struct usb_configuration *c, struct usb_function *f) 247 { 248 struct usb_composite_dev *cdev = c->cdev; 249 struct f_eem *eem = func_to_eem(f); 250 struct usb_string *us; 251 int status; 252 struct usb_ep *ep; 253 254 struct f_eem_opts *eem_opts; 255 256 eem_opts = container_of(f->fi, struct f_eem_opts, func_inst); 257 /* 258 * in drivers/usb/gadget/configfs.c:configfs_composite_bind() 259 * configurations are bound in sequence with list_for_each_entry, 260 * in each configuration its functions are bound in sequence 261 * with list_for_each_entry, so we assume no race condition 262 * with regard to eem_opts->bound access 263 */ 264 if (!eem_opts->bound) { 265 mutex_lock(&eem_opts->lock); 266 gether_set_gadget(eem_opts->net, cdev->gadget); 267 status = gether_register_netdev(eem_opts->net); 268 mutex_unlock(&eem_opts->lock); 269 if (status) 270 return status; 271 eem_opts->bound = true; 272 } 273 274 us = usb_gstrings_attach(cdev, eem_strings, 275 ARRAY_SIZE(eem_string_defs)); 276 if (IS_ERR(us)) 277 return PTR_ERR(us); 278 eem_intf.iInterface = us[0].id; 279 280 /* allocate instance-specific interface IDs */ 281 status = usb_interface_id(c, f); 282 if (status < 0) 283 goto fail; 284 eem->ctrl_id = status; 285 eem_intf.bInterfaceNumber = status; 286 287 status = -ENODEV; 288 289 /* allocate instance-specific endpoints */ 290 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc); 291 if (!ep) 292 goto fail; 293 eem->port.in_ep = ep; 294 295 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc); 296 if (!ep) 297 goto fail; 298 eem->port.out_ep = ep; 299 300 status = -ENOMEM; 301 302 /* support all relevant hardware speeds... we expect that when 303 * hardware is dual speed, all bulk-capable endpoints work at 304 * both speeds 305 */ 306 eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress; 307 eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress; 308 309 eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress; 310 eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress; 311 312 status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function, 313 eem_ss_function, NULL); 314 if (status) 315 goto fail; 316 317 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n", 318 gadget_is_superspeed(c->cdev->gadget) ? "super" : 319 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", 320 eem->port.in_ep->name, eem->port.out_ep->name); 321 return 0; 322 323 fail: 324 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); 325 326 return status; 327 } 328 329 static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req) 330 { 331 struct sk_buff *skb = (struct sk_buff *)req->context; 332 333 dev_kfree_skb_any(skb); 334 } 335 336 /* 337 * Add the EEM header and ethernet checksum. 338 * We currently do not attempt to put multiple ethernet frames 339 * into a single USB transfer 340 */ 341 static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb) 342 { 343 struct sk_buff *skb2 = NULL; 344 struct usb_ep *in = port->in_ep; 345 int headroom, tailroom, padlen = 0; 346 u16 len; 347 348 if (!skb) 349 return NULL; 350 351 len = skb->len; 352 headroom = skb_headroom(skb); 353 tailroom = skb_tailroom(skb); 354 355 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0, 356 * stick two bytes of zero-length EEM packet on the end. 357 */ 358 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0) 359 padlen += 2; 360 361 if ((tailroom >= (ETH_FCS_LEN + padlen)) && 362 (headroom >= EEM_HLEN) && !skb_cloned(skb)) 363 goto done; 364 365 skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC); 366 dev_kfree_skb_any(skb); 367 skb = skb2; 368 if (!skb) 369 return skb; 370 371 done: 372 /* use the "no CRC" option */ 373 put_unaligned_be32(0xdeadbeef, skb_put(skb, 4)); 374 375 /* EEM packet header format: 376 * b0..13: length of ethernet frame 377 * b14: bmCRC (0 == sentinel CRC) 378 * b15: bmType (0 == data) 379 */ 380 len = skb->len; 381 put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2)); 382 383 /* add a zero-length EEM packet, if needed */ 384 if (padlen) 385 put_unaligned_le16(0, skb_put(skb, 2)); 386 387 return skb; 388 } 389 390 /* 391 * Remove the EEM header. Note that there can be many EEM packets in a single 392 * USB transfer, so we need to break them out and handle them independently. 393 */ 394 static int eem_unwrap(struct gether *port, 395 struct sk_buff *skb, 396 struct sk_buff_head *list) 397 { 398 struct usb_composite_dev *cdev = port->func.config->cdev; 399 int status = 0; 400 401 do { 402 struct sk_buff *skb2; 403 u16 header; 404 u16 len = 0; 405 406 if (skb->len < EEM_HLEN) { 407 status = -EINVAL; 408 DBG(cdev, "invalid EEM header\n"); 409 goto error; 410 } 411 412 /* remove the EEM header */ 413 header = get_unaligned_le16(skb->data); 414 skb_pull(skb, EEM_HLEN); 415 416 /* EEM packet header format: 417 * b0..14: EEM type dependent (data or command) 418 * b15: bmType (0 == data, 1 == command) 419 */ 420 if (header & BIT(15)) { 421 struct usb_request *req = cdev->req; 422 u16 bmEEMCmd; 423 424 /* EEM command packet format: 425 * b0..10: bmEEMCmdParam 426 * b11..13: bmEEMCmd 427 * b14: reserved (must be zero) 428 * b15: bmType (1 == command) 429 */ 430 if (header & BIT(14)) 431 continue; 432 433 bmEEMCmd = (header >> 11) & 0x7; 434 switch (bmEEMCmd) { 435 case 0: /* echo */ 436 len = header & 0x7FF; 437 if (skb->len < len) { 438 status = -EOVERFLOW; 439 goto error; 440 } 441 442 skb2 = skb_clone(skb, GFP_ATOMIC); 443 if (unlikely(!skb2)) { 444 DBG(cdev, "EEM echo response error\n"); 445 goto next; 446 } 447 skb_trim(skb2, len); 448 put_unaligned_le16(BIT(15) | BIT(11) | len, 449 skb_push(skb2, 2)); 450 skb_copy_bits(skb2, 0, req->buf, skb2->len); 451 req->length = skb2->len; 452 req->complete = eem_cmd_complete; 453 req->zero = 1; 454 req->context = skb2; 455 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC)) 456 DBG(cdev, "echo response queue fail\n"); 457 break; 458 459 case 1: /* echo response */ 460 case 2: /* suspend hint */ 461 case 3: /* response hint */ 462 case 4: /* response complete hint */ 463 case 5: /* tickle */ 464 default: /* reserved */ 465 continue; 466 } 467 } else { 468 u32 crc, crc2; 469 struct sk_buff *skb3; 470 471 /* check for zero-length EEM packet */ 472 if (header == 0) 473 continue; 474 475 /* EEM data packet format: 476 * b0..13: length of ethernet frame 477 * b14: bmCRC (0 == sentinel, 1 == calculated) 478 * b15: bmType (0 == data) 479 */ 480 len = header & 0x3FFF; 481 if ((skb->len < len) 482 || (len < (ETH_HLEN + ETH_FCS_LEN))) { 483 status = -EINVAL; 484 goto error; 485 } 486 487 /* validate CRC */ 488 if (header & BIT(14)) { 489 crc = get_unaligned_le32(skb->data + len 490 - ETH_FCS_LEN); 491 crc2 = ~crc32_le(~0, 492 skb->data, len - ETH_FCS_LEN); 493 } else { 494 crc = get_unaligned_be32(skb->data + len 495 - ETH_FCS_LEN); 496 crc2 = 0xdeadbeef; 497 } 498 if (crc != crc2) { 499 DBG(cdev, "invalid EEM CRC\n"); 500 goto next; 501 } 502 503 skb2 = skb_clone(skb, GFP_ATOMIC); 504 if (unlikely(!skb2)) { 505 DBG(cdev, "unable to unframe EEM packet\n"); 506 continue; 507 } 508 skb_trim(skb2, len - ETH_FCS_LEN); 509 510 skb3 = skb_copy_expand(skb2, 511 NET_IP_ALIGN, 512 0, 513 GFP_ATOMIC); 514 if (unlikely(!skb3)) { 515 DBG(cdev, "unable to realign EEM packet\n"); 516 dev_kfree_skb_any(skb2); 517 continue; 518 } 519 dev_kfree_skb_any(skb2); 520 skb_queue_tail(list, skb3); 521 } 522 next: 523 skb_pull(skb, len); 524 } while (skb->len); 525 526 error: 527 dev_kfree_skb_any(skb); 528 return status; 529 } 530 531 static inline struct f_eem_opts *to_f_eem_opts(struct config_item *item) 532 { 533 return container_of(to_config_group(item), struct f_eem_opts, 534 func_inst.group); 535 } 536 537 /* f_eem_item_ops */ 538 USB_ETHERNET_CONFIGFS_ITEM(eem); 539 540 /* f_eem_opts_dev_addr */ 541 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(eem); 542 543 /* f_eem_opts_host_addr */ 544 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(eem); 545 546 /* f_eem_opts_qmult */ 547 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(eem); 548 549 /* f_eem_opts_ifname */ 550 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(eem); 551 552 static struct configfs_attribute *eem_attrs[] = { 553 &eem_opts_attr_dev_addr, 554 &eem_opts_attr_host_addr, 555 &eem_opts_attr_qmult, 556 &eem_opts_attr_ifname, 557 NULL, 558 }; 559 560 static struct config_item_type eem_func_type = { 561 .ct_item_ops = &eem_item_ops, 562 .ct_attrs = eem_attrs, 563 .ct_owner = THIS_MODULE, 564 }; 565 566 static void eem_free_inst(struct usb_function_instance *f) 567 { 568 struct f_eem_opts *opts; 569 570 opts = container_of(f, struct f_eem_opts, func_inst); 571 if (opts->bound) 572 gether_cleanup(netdev_priv(opts->net)); 573 else 574 free_netdev(opts->net); 575 kfree(opts); 576 } 577 578 static struct usb_function_instance *eem_alloc_inst(void) 579 { 580 struct f_eem_opts *opts; 581 582 opts = kzalloc(sizeof(*opts), GFP_KERNEL); 583 if (!opts) 584 return ERR_PTR(-ENOMEM); 585 mutex_init(&opts->lock); 586 opts->func_inst.free_func_inst = eem_free_inst; 587 opts->net = gether_setup_default(); 588 if (IS_ERR(opts->net)) { 589 struct net_device *net = opts->net; 590 kfree(opts); 591 return ERR_CAST(net); 592 } 593 594 config_group_init_type_name(&opts->func_inst.group, "", &eem_func_type); 595 596 return &opts->func_inst; 597 } 598 599 static void eem_free(struct usb_function *f) 600 { 601 struct f_eem *eem; 602 struct f_eem_opts *opts; 603 604 eem = func_to_eem(f); 605 opts = container_of(f->fi, struct f_eem_opts, func_inst); 606 kfree(eem); 607 mutex_lock(&opts->lock); 608 opts->refcnt--; 609 mutex_unlock(&opts->lock); 610 } 611 612 static void eem_unbind(struct usb_configuration *c, struct usb_function *f) 613 { 614 DBG(c->cdev, "eem unbind\n"); 615 616 usb_free_all_descriptors(f); 617 } 618 619 static struct usb_function *eem_alloc(struct usb_function_instance *fi) 620 { 621 struct f_eem *eem; 622 struct f_eem_opts *opts; 623 624 /* allocate and initialize one new instance */ 625 eem = kzalloc(sizeof(*eem), GFP_KERNEL); 626 if (!eem) 627 return ERR_PTR(-ENOMEM); 628 629 opts = container_of(fi, struct f_eem_opts, func_inst); 630 mutex_lock(&opts->lock); 631 opts->refcnt++; 632 633 eem->port.ioport = netdev_priv(opts->net); 634 mutex_unlock(&opts->lock); 635 eem->port.cdc_filter = DEFAULT_FILTER; 636 637 eem->port.func.name = "cdc_eem"; 638 /* descriptors are per-instance copies */ 639 eem->port.func.bind = eem_bind; 640 eem->port.func.unbind = eem_unbind; 641 eem->port.func.set_alt = eem_set_alt; 642 eem->port.func.setup = eem_setup; 643 eem->port.func.disable = eem_disable; 644 eem->port.func.free_func = eem_free; 645 eem->port.wrap = eem_wrap; 646 eem->port.unwrap = eem_unwrap; 647 eem->port.header_len = EEM_HLEN; 648 649 return &eem->port.func; 650 } 651 652 DECLARE_USB_FUNCTION_INIT(eem, eem_alloc_inst, eem_alloc); 653 MODULE_LICENSE("GPL"); 654 MODULE_AUTHOR("David Brownell"); 655