diff --git a/Documentation/ABI/testing/configfs-usb-gadget-acm b/Documentation/ABI/testing/configfs-usb-gadget-acm index 25e68be9eb6612..29b45e397501fc 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-acm +++ b/Documentation/ABI/testing/configfs-usb-gadget-acm @@ -13,3 +13,12 @@ KernelVersion: 6.13 Description: Reported bInterfaceProtocol for the ACM device. For legacy reasons, this defaults to 1 (USB_CDC_ACM_PROTO_AT_V25TER). + +What: /config/usb-gadget/gadget/functions/acm.name/ifunction +Date: Nov 2026 +KernelVersion: 6.18 +Description: + Optional iFunction string (max 95 characters) reported in the + Interface Association Descriptor. When set, it replaces the + built-in strings and the control and data interfaces report no + iInterface string. Empty (default) keeps the built-in strings. diff --git a/Documentation/ABI/testing/configfs-usb-gadget-rndis b/Documentation/ABI/testing/configfs-usb-gadget-rndis index 9416eda7fe9330..007d3cf03fa6a9 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-rndis +++ b/Documentation/ABI/testing/configfs-usb-gadget-rndis @@ -16,4 +16,12 @@ Description: class USB interface class, default is 02 (hex) subclass USB interface subclass, default is 06 (hex) protocol USB interface protocol, default is 00 (hex) + ifunction optional iFunction string (max 95 characters) + for the Interface Association Descriptor. + When set, it replaces the built-in strings and + the interfaces report no iInterface string ========= ============================================= + + If class, subclass or protocol differ from their defaults, + the values are also reported in the control interface + descriptor, not only in the Interface Association Descriptor. diff --git a/Documentation/usb/gadget-testing.rst b/Documentation/usb/gadget-testing.rst index edc13740ff6b70..217dd04b493242 100644 --- a/Documentation/usb/gadget-testing.rst +++ b/Documentation/usb/gadget-testing.rst @@ -39,11 +39,15 @@ Function-specific configfs interface ------------------------------------ The function name to use when creating the function directory is "acm". -The ACM function provides just one attribute in its function directory: +The ACM function provides these attributes in its function directory: - port_num - -The attribute is read-only. + =============== ================================================== + port_num port number of the /dev/ttyGS device (read-only) + protocol reported bInterfaceProtocol, default is 1 + ifunction optional iFunction string for the Interface + Association Descriptor; when set, it replaces the + built-in interface strings + =============== ================================================== There can be at most 4 ACM/generic serial/OBEX ports in the system. @@ -604,6 +608,12 @@ The RNDIS function provides these attributes in its function directory: Ethernet over USB link dev_addr MAC address of device's end of this Ethernet over USB link + class USB interface class, default is 02 (hex) + subclass USB interface subclass, default is 06 (hex) + protocol USB interface protocol, default is 00 (hex) + ifunction optional iFunction string for the Interface + Association Descriptor; when set, it replaces the + built-in interface strings =============== ================================================== and after creating the functions/rndis. they contain default diff --git a/drivers/usb/gadget/function/f_acm.c b/drivers/usb/gadget/function/f_acm.c index 106046e17c4e11..e58edd174ca42e 100644 --- a/drivers/usb/gadget/function/f_acm.c +++ b/drivers/usb/gadget/function/f_acm.c @@ -617,19 +617,56 @@ acm_bind(struct usb_configuration *c, struct usb_function *f) int status; struct usb_ep *ep; struct usb_request *request __free(free_usb_request) = NULL; + struct f_serial_opts *opts; + bool has_ifunction; /* REVISIT might want instance-specific strings to help * distinguish instances ... */ - /* maybe allocate device-global string IDs, and patch descriptors */ - us = usb_gstrings_attach(cdev, acm_strings, - ARRAY_SIZE(acm_string_defs)); + opts = container_of(f->fi, struct f_serial_opts, func_inst); + + mutex_lock(&opts->lock); + has_ifunction = opts->ifunction[0] != '\0'; + if (has_ifunction) { + /* + * iFunction configured. Let this string override the builtin + * strings, by setting it in the IAD only. The tables are + * copied by usb_gstrings_attach(), so stack storage is fine + * and the shared acm_string_defs[] is left untouched. + */ + struct usb_string ifunc_defs[] = { + [0].s = opts->ifunction, + { } /* end of list */ + }; + struct usb_gadget_strings ifunc_table = { + .language = 0x0409, /* en-us */ + .strings = ifunc_defs, + }; + struct usb_gadget_strings *ifunc_strings[] = { + &ifunc_table, + NULL, + }; + + us = usb_gstrings_attach(cdev, ifunc_strings, 1); + } else { + /* maybe allocate device-global string IDs, and patch descriptors */ + us = usb_gstrings_attach(cdev, acm_strings, + ARRAY_SIZE(acm_string_defs)); + } + mutex_unlock(&opts->lock); if (IS_ERR(us)) return PTR_ERR(us); - acm_control_interface_desc.iInterface = us[ACM_CTRL_IDX].id; - acm_data_interface_desc.iInterface = us[ACM_DATA_IDX].id; - acm_iad_descriptor.iFunction = us[ACM_IAD_IDX].id; + + if (has_ifunction) { + acm_control_interface_desc.iInterface = 0; + acm_data_interface_desc.iInterface = 0; + acm_iad_descriptor.iFunction = us[0].id; + } else { + acm_control_interface_desc.iInterface = us[ACM_CTRL_IDX].id; + acm_data_interface_desc.iInterface = us[ACM_DATA_IDX].id; + acm_iad_descriptor.iFunction = us[ACM_IAD_IDX].id; + } /* allocate instance-specific interface IDs, and patch descriptors */ status = usb_interface_id(c, f); @@ -852,12 +889,44 @@ static ssize_t f_acm_protocol_store(struct config_item *item, CONFIGFS_ATTR(f_acm_, protocol); +static ssize_t f_acm_ifunction_show(struct config_item *item, char *page) +{ + struct f_serial_opts *opts = to_f_serial_opts(item); + ssize_t ret; + + mutex_lock(&opts->lock); + ret = sysfs_emit(page, "%s\n", opts->ifunction); + mutex_unlock(&opts->lock); + + return ret; +} + +static ssize_t f_acm_ifunction_store(struct config_item *item, + const char *page, size_t count) +{ + struct f_serial_opts *opts = to_f_serial_opts(item); + ssize_t ret; + char *nl; + + mutex_lock(&opts->lock); + ret = strscpy(opts->ifunction, page, sizeof(opts->ifunction)); + nl = strchr(opts->ifunction, '\n'); + if (nl) + *nl = '\0'; + mutex_unlock(&opts->lock); + + return ret < 0 ? ret : count; +} + +CONFIGFS_ATTR(f_acm_, ifunction); + static struct configfs_attribute *acm_attrs[] = { #ifdef CONFIG_U_SERIAL_CONSOLE &f_acm_attr_console, #endif &f_acm_attr_port_num, &f_acm_attr_protocol, + &f_acm_attr_ifunction, NULL, }; diff --git a/drivers/usb/gadget/function/f_rndis.c b/drivers/usb/gadget/function/f_rndis.c index ec397291e40b18..dfc9ae48f8b678 100644 --- a/drivers/usb/gadget/function/f_rndis.c +++ b/drivers/usb/gadget/function/f_rndis.c @@ -668,6 +668,8 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f) struct usb_os_desc_table *os_desc_table __free(kfree) = NULL; struct net_device *net __free(detach_gadget) = NULL; struct usb_request *request __free(free_usb_request) = NULL; + bool custom_class, custom_subclass, custom_protocol; + bool has_ifunction; if (!can_support_rndis(c)) return -EINVAL; @@ -685,6 +687,51 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f) rndis_iad_descriptor.bFunctionSubClass = rndis_opts->subclass; rndis_iad_descriptor.bFunctionProtocol = rndis_opts->protocol; + /* + * If class/subclass/protocol differ from the defaults, also + * report them in the control interface descriptor. + */ + custom_class = rndis_opts->class != USB_CLASS_COMM; + custom_subclass = rndis_opts->subclass != USB_CDC_SUBCLASS_ETHERNET; + custom_protocol = rndis_opts->protocol != USB_CDC_PROTO_NONE; + + rndis_control_intf.bInterfaceClass = custom_class ? + rndis_opts->class : USB_CLASS_COMM; + rndis_control_intf.bInterfaceSubClass = custom_subclass ? + rndis_opts->subclass : USB_CDC_SUBCLASS_ACM; + rndis_control_intf.bInterfaceProtocol = custom_protocol ? + rndis_opts->protocol : USB_CDC_ACM_PROTO_VENDOR; + + has_ifunction = rndis_opts->ifunction[0] != '\0'; + if (has_ifunction) { + /* + * iFunction configured. Let this string override the + * builtin strings, by setting it in the IAD only. The + * tables are copied by usb_gstrings_attach(), so stack + * storage is fine and the shared rndis_string_defs[] + * is left untouched. + */ + struct usb_string ifunc_defs[] = { + [0].s = rndis_opts->ifunction, + { } /* end of list */ + }; + struct usb_gadget_strings ifunc_table = { + .language = 0x0409, /* en-us */ + .strings = ifunc_defs, + }; + struct usb_gadget_strings *ifunc_strings[] = { + &ifunc_table, + NULL, + }; + + us = usb_gstrings_attach(cdev, ifunc_strings, 1); + } else { + us = usb_gstrings_attach(cdev, rndis_strings, + ARRAY_SIZE(rndis_string_defs)); + } + if (IS_ERR(us)) + return PTR_ERR(us); + if (rndis_opts->bind_count == 0 && !rndis_opts->borrowed_net) { if (!device_is_registered(&rndis_opts->net->dev)) { gether_set_gadget(rndis_opts->net, cdev->gadget); @@ -698,13 +745,15 @@ rndis_bind(struct usb_configuration *c, struct usb_function *f) } } - us = usb_gstrings_attach(cdev, rndis_strings, - ARRAY_SIZE(rndis_string_defs)); - if (IS_ERR(us)) - return PTR_ERR(us); - rndis_control_intf.iInterface = us[0].id; - rndis_data_intf.iInterface = us[1].id; - rndis_iad_descriptor.iFunction = us[2].id; + if (has_ifunction) { + rndis_control_intf.iInterface = 0; + rndis_data_intf.iInterface = 0; + rndis_iad_descriptor.iFunction = us[0].id; + } else { + rndis_control_intf.iInterface = us[0].id; + rndis_data_intf.iInterface = us[1].id; + rndis_iad_descriptor.iFunction = us[2].id; + } /* allocate instance-specific interface IDs */ status = usb_interface_id(c, f); @@ -852,6 +901,9 @@ USB_ETHER_CONFIGFS_ITEM_ATTR_U8_RW(rndis, subclass); /* f_rndis_opts_protocol */ USB_ETHER_CONFIGFS_ITEM_ATTR_U8_RW(rndis, protocol); +/* f_rndis_opts_ifunction */ +USB_ETHER_CONFIGFS_ITEM_ATTR_STRING(rndis, ifunction); + static struct configfs_attribute *rndis_attrs[] = { &rndis_opts_attr_dev_addr, &rndis_opts_attr_host_addr, @@ -860,6 +912,7 @@ static struct configfs_attribute *rndis_attrs[] = { &rndis_opts_attr_class, &rndis_opts_attr_subclass, &rndis_opts_attr_protocol, + &rndis_opts_attr_ifunction, NULL, }; diff --git a/drivers/usb/gadget/function/u_ether_configfs.h b/drivers/usb/gadget/function/u_ether_configfs.h index f558c3139ebe50..9f72b0edfec239 100644 --- a/drivers/usb/gadget/function/u_ether_configfs.h +++ b/drivers/usb/gadget/function/u_ether_configfs.h @@ -197,4 +197,38 @@ out: \ \ CONFIGFS_ATTR(_f_##_opts_, _n_) +#define USB_ETHER_CONFIGFS_ITEM_ATTR_STRING(_f_, _n_) \ + static ssize_t _f_##_opts_##_n_##_show(struct config_item *item,\ + char *page) \ + { \ + struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ + int ret; \ + \ + mutex_lock(&opts->lock); \ + ret = sysfs_emit(page, "%s\n", opts->_n_); \ + mutex_unlock(&opts->lock); \ + \ + return ret; \ + } \ + \ + static ssize_t _f_##_opts_##_n_##_store(struct config_item *item,\ + const char *page, \ + size_t len) \ + { \ + struct f_##_f_##_opts *opts = to_f_##_f_##_opts(item); \ + ssize_t ret; \ + char *nl; \ + \ + mutex_lock(&opts->lock); \ + ret = strscpy(opts->_n_, page, sizeof(opts->_n_)); \ + nl = strchr(opts->_n_, '\n'); \ + if (nl) \ + *nl = '\0'; \ + mutex_unlock(&opts->lock); \ + \ + return ret < 0 ? ret : len; \ + } \ + \ + CONFIGFS_ATTR(_f_##_opts_, _n_) + #endif /* __U_ETHER_CONFIGFS_H */ diff --git a/drivers/usb/gadget/function/u_rndis.h b/drivers/usb/gadget/function/u_rndis.h index 4e64619714dc2d..a7c4a5ed48ad53 100644 --- a/drivers/usb/gadget/function/u_rndis.h +++ b/drivers/usb/gadget/function/u_rndis.h @@ -33,6 +33,8 @@ * @class: USB class. * @subclass: USB subclass. * @protocol: USB protocol. + * @ifunction: Optional iFunction string for the IAD. When set, it replaces + * the built-in strings and the interfaces get no iInterface. * @lock: Protects the data from concurrent access by configfs read/write * and create symlink/remove symlink operations. * @refcnt: Reference counter for the function instance. @@ -52,6 +54,7 @@ struct f_rndis_opts { u8 class; u8 subclass; u8 protocol; + char ifunction[96]; struct mutex lock; int refcnt; }; diff --git a/drivers/usb/gadget/function/u_serial.h b/drivers/usb/gadget/function/u_serial.h index e1274338ea6191..ea06d87b3847fd 100644 --- a/drivers/usb/gadget/function/u_serial.h +++ b/drivers/usb/gadget/function/u_serial.h @@ -18,6 +18,7 @@ struct f_serial_opts { struct usb_function_instance func_inst; u8 port_num; u8 protocol; + char ifunction[96]; /* optional iFunction string for the IAD */ struct mutex lock; /* protect instances */ int instances;