13d0407baSopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 23d0407baSopenharmony_ci/* 33d0407baSopenharmony_ci * composite.h -- framework for usb gadgets which are composite devices 43d0407baSopenharmony_ci * 53d0407baSopenharmony_ci * Copyright (C) 2006-2008 David Brownell 63d0407baSopenharmony_ci * 73d0407baSopenharmony_ci * This program is free software; you can redistribute it and/or modify 83d0407baSopenharmony_ci * it under the terms of the GNU General Public License as published by 93d0407baSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 103d0407baSopenharmony_ci * (at your option) any later version. 113d0407baSopenharmony_ci * 123d0407baSopenharmony_ci * This program is distributed in the hope that it will be useful, 133d0407baSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 143d0407baSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 153d0407baSopenharmony_ci * GNU General Public License for more details. 163d0407baSopenharmony_ci * 173d0407baSopenharmony_ci * You should have received a copy of the GNU General Public License 183d0407baSopenharmony_ci * along with this program; if not, write to the Free Software 193d0407baSopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 203d0407baSopenharmony_ci */ 213d0407baSopenharmony_ci 223d0407baSopenharmony_ci#ifndef __LINUX_USB_COMPOSITE_H 233d0407baSopenharmony_ci#define __LINUX_USB_COMPOSITE_H 243d0407baSopenharmony_ci 253d0407baSopenharmony_ci/* 263d0407baSopenharmony_ci * This framework is an optional layer on top of the USB Gadget interface, 273d0407baSopenharmony_ci * making it easier to build (a) Composite devices, supporting multiple 283d0407baSopenharmony_ci * functions within any single configuration, and (b) Multi-configuration 293d0407baSopenharmony_ci * devices, also supporting multiple functions but without necessarily 303d0407baSopenharmony_ci * having more than one function per configuration. 313d0407baSopenharmony_ci * 323d0407baSopenharmony_ci * Example: a device with a single configuration supporting both network 333d0407baSopenharmony_ci * link and mass storage functions is a composite device. Those functions 343d0407baSopenharmony_ci * might alternatively be packaged in individual configurations, but in 353d0407baSopenharmony_ci * the composite model the host can use both functions at the same time. 363d0407baSopenharmony_ci */ 373d0407baSopenharmony_ci 383d0407baSopenharmony_ci#include <linux/bcd.h> 393d0407baSopenharmony_ci#include <linux/version.h> 403d0407baSopenharmony_ci#include <linux/usb/ch9.h> 413d0407baSopenharmony_ci#include <linux/usb/gadget.h> 423d0407baSopenharmony_ci#include <linux/log2.h> 433d0407baSopenharmony_ci#include <linux/configfs.h> 443d0407baSopenharmony_ci 453d0407baSopenharmony_ci/* 463d0407baSopenharmony_ci * USB function drivers should return USB_GADGET_DELAYED_STATUS if they 473d0407baSopenharmony_ci * wish to delay the data/status stages of the control transfer till they 483d0407baSopenharmony_ci * are ready. The control transfer will then be kept from completing till 493d0407baSopenharmony_ci * all the function drivers that requested for USB_GADGET_DELAYED_STAUS 503d0407baSopenharmony_ci * invoke usb_composite_setup_continue(). 513d0407baSopenharmony_ci */ 523d0407baSopenharmony_ci#define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */ 533d0407baSopenharmony_ci 543d0407baSopenharmony_ci/* big enough to hold our biggest descriptor */ 553d0407baSopenharmony_ci#define USB_COMP_EP0_BUFSIZ 4096 563d0407baSopenharmony_ci 573d0407baSopenharmony_ci/* OS feature descriptor length <= 4kB */ 583d0407baSopenharmony_ci#define USB_COMP_EP0_OS_DESC_BUFSIZ 4096 593d0407baSopenharmony_ci 603d0407baSopenharmony_ci#define USB_MS_TO_HS_INTERVAL(x) (ilog2(((x)*1000 / 125)) + 1) 613d0407baSopenharmony_cistruct usb_configuration; 623d0407baSopenharmony_ci 633d0407baSopenharmony_ci/** 643d0407baSopenharmony_ci * struct usb_os_desc_ext_prop - describes one "Extended Property" 653d0407baSopenharmony_ci * @entry: used to keep a list of extended properties 663d0407baSopenharmony_ci * @type: Extended Property type 673d0407baSopenharmony_ci * @name_len: Extended Property unicode name length, including terminating '\0' 683d0407baSopenharmony_ci * @name: Extended Property name 693d0407baSopenharmony_ci * @data_len: Length of Extended Property blob (for unicode store double len) 703d0407baSopenharmony_ci * @data: Extended Property blob 713d0407baSopenharmony_ci * @item: Represents this Extended Property in configfs 723d0407baSopenharmony_ci */ 733d0407baSopenharmony_cistruct usb_os_desc_ext_prop { 743d0407baSopenharmony_ci struct list_head entry; 753d0407baSopenharmony_ci u8 type; 763d0407baSopenharmony_ci int name_len; 773d0407baSopenharmony_ci char *name; 783d0407baSopenharmony_ci int data_len; 793d0407baSopenharmony_ci char *data; 803d0407baSopenharmony_ci struct config_item item; 813d0407baSopenharmony_ci}; 823d0407baSopenharmony_ci 833d0407baSopenharmony_ci/** 843d0407baSopenharmony_ci * struct usb_os_desc - describes OS descriptors associated with one interface 853d0407baSopenharmony_ci * @ext_compat_id: 16 bytes of "Compatible ID" and "Subcompatible ID" 863d0407baSopenharmony_ci * @ext_prop: Extended Properties list 873d0407baSopenharmony_ci * @ext_prop_len: Total length of Extended Properties blobs 883d0407baSopenharmony_ci * @ext_prop_count: Number of Extended Properties 893d0407baSopenharmony_ci * @opts_mutex: Optional mutex protecting config data of a usb_function_instance 903d0407baSopenharmony_ci * @group: Represents OS descriptors associated with an interface in configfs 913d0407baSopenharmony_ci * @owner: Module associated with this OS descriptor 923d0407baSopenharmony_ci */ 933d0407baSopenharmony_cistruct usb_os_desc { 943d0407baSopenharmony_ci char *ext_compat_id; 953d0407baSopenharmony_ci struct list_head ext_prop; 963d0407baSopenharmony_ci int ext_prop_len; 973d0407baSopenharmony_ci int ext_prop_count; 983d0407baSopenharmony_ci struct mutex *opts_mutex; 993d0407baSopenharmony_ci struct config_group group; 1003d0407baSopenharmony_ci struct module *owner; 1013d0407baSopenharmony_ci}; 1023d0407baSopenharmony_ci 1033d0407baSopenharmony_ci/** 1043d0407baSopenharmony_ci * struct usb_os_desc_table - describes OS descriptors associated with one 1053d0407baSopenharmony_ci * interface of a usb_function 1063d0407baSopenharmony_ci * @if_id: Interface id 1073d0407baSopenharmony_ci * @os_desc: "Extended Compatibility ID" and "Extended Properties" of the 1083d0407baSopenharmony_ci * interface 1093d0407baSopenharmony_ci * 1103d0407baSopenharmony_ci * Each interface can have at most one "Extended Compatibility ID" and a 1113d0407baSopenharmony_ci * number of "Extended Properties". 1123d0407baSopenharmony_ci */ 1133d0407baSopenharmony_cistruct usb_os_desc_table { 1143d0407baSopenharmony_ci int if_id; 1153d0407baSopenharmony_ci struct usb_os_desc *os_desc; 1163d0407baSopenharmony_ci}; 1173d0407baSopenharmony_ci 1183d0407baSopenharmony_ci/** 1193d0407baSopenharmony_ci * struct usb_function - describes one function of a configuration 1203d0407baSopenharmony_ci * @name: For diagnostics, identifies the function. 1213d0407baSopenharmony_ci * @strings: tables of strings, keyed by identifiers assigned during bind() 1223d0407baSopenharmony_ci * and by language IDs provided in control requests 1233d0407baSopenharmony_ci * @fs_descriptors: Table of full (or low) speed descriptors, using interface and 1243d0407baSopenharmony_ci * string identifiers assigned during @bind(). If this pointer is null, 1253d0407baSopenharmony_ci * the function will not be available at full speed (or at low speed). 1263d0407baSopenharmony_ci * @hs_descriptors: Table of high speed descriptors, using interface and 1273d0407baSopenharmony_ci * string identifiers assigned during @bind(). If this pointer is null, 1283d0407baSopenharmony_ci * the function will not be available at high speed. 1293d0407baSopenharmony_ci * @ss_descriptors: Table of super speed descriptors, using interface and 1303d0407baSopenharmony_ci * string identifiers assigned during @bind(). If this 1313d0407baSopenharmony_ci * pointer is null after initiation, the function will not 1323d0407baSopenharmony_ci * be available at super speed. 1333d0407baSopenharmony_ci * @ssp_descriptors: Table of super speed plus descriptors, using 1343d0407baSopenharmony_ci * interface and string identifiers assigned during @bind(). If 1353d0407baSopenharmony_ci * this pointer is null after initiation, the function will not 1363d0407baSopenharmony_ci * be available at super speed plus. 1373d0407baSopenharmony_ci * @config: assigned when @usb_add_function() is called; this is the 1383d0407baSopenharmony_ci * configuration with which this function is associated. 1393d0407baSopenharmony_ci * @os_desc_table: Table of (interface id, os descriptors) pairs. The function 1403d0407baSopenharmony_ci * can expose more than one interface. If an interface is a member of 1413d0407baSopenharmony_ci * an IAD, only the first interface of IAD has its entry in the table. 1423d0407baSopenharmony_ci * @os_desc_n: Number of entries in os_desc_table 1433d0407baSopenharmony_ci * @bind: Before the gadget can register, all of its functions bind() to the 1443d0407baSopenharmony_ci * available resources including string and interface identifiers used 1453d0407baSopenharmony_ci * in interface or class descriptors; endpoints; I/O buffers; and so on. 1463d0407baSopenharmony_ci * @unbind: Reverses @bind; called as a side effect of unregistering the 1473d0407baSopenharmony_ci * driver which added this function. 1483d0407baSopenharmony_ci * @free_func: free the struct usb_function. 1493d0407baSopenharmony_ci * @mod: (internal) points to the module that created this structure. 1503d0407baSopenharmony_ci * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may 1513d0407baSopenharmony_ci * initialize usb_ep.driver data at this time (when it is used). 1523d0407baSopenharmony_ci * Note that setting an interface to its current altsetting resets 1533d0407baSopenharmony_ci * interface state, and that all interfaces have a disabled state. 1543d0407baSopenharmony_ci * @get_alt: Returns the active altsetting. If this is not provided, 1553d0407baSopenharmony_ci * then only altsetting zero is supported. 1563d0407baSopenharmony_ci * @disable: (REQUIRED) Indicates the function should be disabled. Reasons 1573d0407baSopenharmony_ci * include host resetting or reconfiguring the gadget, and disconnection. 1583d0407baSopenharmony_ci * @setup: Used for interface-specific control requests. 1593d0407baSopenharmony_ci * @req_match: Tests if a given class request can be handled by this function. 1603d0407baSopenharmony_ci * @suspend: Notifies functions when the host stops sending USB traffic. 1613d0407baSopenharmony_ci * @resume: Notifies functions when the host restarts USB traffic. 1623d0407baSopenharmony_ci * @get_status: Returns function status as a reply to 1633d0407baSopenharmony_ci * GetStatus() request when the recipient is Interface. 1643d0407baSopenharmony_ci * @func_suspend: callback to be called when 1653d0407baSopenharmony_ci * SetFeature(FUNCTION_SUSPEND) is reseived 1663d0407baSopenharmony_ci * 1673d0407baSopenharmony_ci * A single USB function uses one or more interfaces, and should in most 1683d0407baSopenharmony_ci * cases support operation at both full and high speeds. Each function is 1693d0407baSopenharmony_ci * associated by @usb_add_function() with a one configuration; that function 1703d0407baSopenharmony_ci * causes @bind() to be called so resources can be allocated as part of 1713d0407baSopenharmony_ci * setting up a gadget driver. Those resources include endpoints, which 1723d0407baSopenharmony_ci * should be allocated using @usb_ep_autoconfig(). 1733d0407baSopenharmony_ci * 1743d0407baSopenharmony_ci * To support dual speed operation, a function driver provides descriptors 1753d0407baSopenharmony_ci * for both high and full speed operation. Except in rare cases that don't 1763d0407baSopenharmony_ci * involve bulk endpoints, each speed needs different endpoint descriptors. 1773d0407baSopenharmony_ci * 1783d0407baSopenharmony_ci * Function drivers choose their own strategies for managing instance data. 1793d0407baSopenharmony_ci * The simplest strategy just declares it "static', which means the function 1803d0407baSopenharmony_ci * can only be activated once. If the function needs to be exposed in more 1813d0407baSopenharmony_ci * than one configuration at a given speed, it needs to support multiple 1823d0407baSopenharmony_ci * usb_function structures (one for each configuration). 1833d0407baSopenharmony_ci * 1843d0407baSopenharmony_ci * A more complex strategy might encapsulate a @usb_function structure inside 1853d0407baSopenharmony_ci * a driver-specific instance structure to allows multiple activations. An 1863d0407baSopenharmony_ci * example of multiple activations might be a CDC ACM function that supports 1873d0407baSopenharmony_ci * two or more distinct instances within the same configuration, providing 1883d0407baSopenharmony_ci * several independent logical data links to a USB host. 1893d0407baSopenharmony_ci */ 1903d0407baSopenharmony_ci 1913d0407baSopenharmony_cistruct usb_function { 1923d0407baSopenharmony_ci const char *name; 1933d0407baSopenharmony_ci struct usb_gadget_strings **strings; 1943d0407baSopenharmony_ci struct usb_descriptor_header **fs_descriptors; 1953d0407baSopenharmony_ci struct usb_descriptor_header **hs_descriptors; 1963d0407baSopenharmony_ci struct usb_descriptor_header **ss_descriptors; 1973d0407baSopenharmony_ci struct usb_descriptor_header **ssp_descriptors; 1983d0407baSopenharmony_ci 1993d0407baSopenharmony_ci struct usb_configuration *config; 2003d0407baSopenharmony_ci 2013d0407baSopenharmony_ci struct usb_os_desc_table *os_desc_table; 2023d0407baSopenharmony_ci unsigned os_desc_n; 2033d0407baSopenharmony_ci 2043d0407baSopenharmony_ci /* REVISIT: bind() functions can be marked __init, which 2053d0407baSopenharmony_ci * makes trouble for section mismatch analysis. See if 2063d0407baSopenharmony_ci * we can't restructure things to avoid mismatching. 2073d0407baSopenharmony_ci * Related: unbind() may kfree() but bind() won't... 2083d0407baSopenharmony_ci */ 2093d0407baSopenharmony_ci 2103d0407baSopenharmony_ci /* configuration management: bind/unbind */ 2113d0407baSopenharmony_ci int (*bind)(struct usb_configuration *, struct usb_function *); 2123d0407baSopenharmony_ci void (*unbind)(struct usb_configuration *, struct usb_function *); 2133d0407baSopenharmony_ci void (*free_func)(struct usb_function *f); 2143d0407baSopenharmony_ci struct module *mod; 2153d0407baSopenharmony_ci 2163d0407baSopenharmony_ci /* runtime state management */ 2173d0407baSopenharmony_ci int (*set_alt)(struct usb_function *, unsigned interface, unsigned alt); 2183d0407baSopenharmony_ci int (*get_alt)(struct usb_function *, unsigned interface); 2193d0407baSopenharmony_ci void (*disable)(struct usb_function *); 2203d0407baSopenharmony_ci int (*setup)(struct usb_function *, const struct usb_ctrlrequest *); 2213d0407baSopenharmony_ci bool (*req_match)(struct usb_function *, const struct usb_ctrlrequest *, bool config0); 2223d0407baSopenharmony_ci void (*suspend)(struct usb_function *); 2233d0407baSopenharmony_ci void (*resume)(struct usb_function *); 2243d0407baSopenharmony_ci 2253d0407baSopenharmony_ci /* USB 3.0 additions */ 2263d0407baSopenharmony_ci int (*get_status)(struct usb_function *); 2273d0407baSopenharmony_ci int (*func_suspend)(struct usb_function *, u8 suspend_opt); 2283d0407baSopenharmony_ci /* private: */ 2293d0407baSopenharmony_ci /* internals */ 2303d0407baSopenharmony_ci struct list_head list; 2313d0407baSopenharmony_ci DECLARE_BITMAP(endpoints, 32); 2323d0407baSopenharmony_ci const struct usb_function_instance *fi; 2333d0407baSopenharmony_ci 2343d0407baSopenharmony_ci unsigned int bind_deactivated : 1; 2353d0407baSopenharmony_ci}; 2363d0407baSopenharmony_ci 2373d0407baSopenharmony_ciint usb_add_function(struct usb_configuration *, struct usb_function *); 2383d0407baSopenharmony_ci 2393d0407baSopenharmony_ciint usb_function_deactivate(struct usb_function *); 2403d0407baSopenharmony_ciint usb_function_activate(struct usb_function *); 2413d0407baSopenharmony_ci 2423d0407baSopenharmony_ciint usb_interface_id(struct usb_configuration *, struct usb_function *); 2433d0407baSopenharmony_ci 2443d0407baSopenharmony_ciint config_ep_by_speed_and_alt(struct usb_gadget *g, struct usb_function *f, struct usb_ep *_ep, u8 alt); 2453d0407baSopenharmony_ci 2463d0407baSopenharmony_ciint config_ep_by_speed(struct usb_gadget *g, struct usb_function *f, struct usb_ep *_ep); 2473d0407baSopenharmony_ci 2483d0407baSopenharmony_ci#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */ 2493d0407baSopenharmony_ci 2503d0407baSopenharmony_ci/** 2513d0407baSopenharmony_ci * struct usb_configuration - represents one gadget configuration 2523d0407baSopenharmony_ci * @label: For diagnostics, describes the configuration. 2533d0407baSopenharmony_ci * @strings: Tables of strings, keyed by identifiers assigned during @bind() 2543d0407baSopenharmony_ci * and by language IDs provided in control requests. 2553d0407baSopenharmony_ci * @descriptors: Table of descriptors preceding all function descriptors. 2563d0407baSopenharmony_ci * Examples include OTG and vendor-specific descriptors. 2573d0407baSopenharmony_ci * @unbind: Reverses @bind; called as a side effect of unregistering the 2583d0407baSopenharmony_ci * driver which added this configuration. 2593d0407baSopenharmony_ci * @setup: Used to delegate control requests that aren't handled by standard 2603d0407baSopenharmony_ci * device infrastructure or directed at a specific interface. 2613d0407baSopenharmony_ci * @bConfigurationValue: Copied into configuration descriptor. 2623d0407baSopenharmony_ci * @iConfiguration: Copied into configuration descriptor. 2633d0407baSopenharmony_ci * @bmAttributes: Copied into configuration descriptor. 2643d0407baSopenharmony_ci * @MaxPower: Power consumtion in mA. Used to compute bMaxPower in the 2653d0407baSopenharmony_ci * configuration descriptor after considering the bus speed. 2663d0407baSopenharmony_ci * @cdev: assigned by @usb_add_config() before calling @bind(); this is 2673d0407baSopenharmony_ci * the device associated with this configuration. 2683d0407baSopenharmony_ci * 2693d0407baSopenharmony_ci * Configurations are building blocks for gadget drivers structured around 2703d0407baSopenharmony_ci * function drivers. Simple USB gadgets require only one function and one 2713d0407baSopenharmony_ci * configuration, and handle dual-speed hardware by always providing the same 2723d0407baSopenharmony_ci * functionality. Slightly more complex gadgets may have more than one 2733d0407baSopenharmony_ci * single-function configuration at a given speed; or have configurations 2743d0407baSopenharmony_ci * that only work at one speed. 2753d0407baSopenharmony_ci * 2763d0407baSopenharmony_ci * Composite devices are, by definition, ones with configurations which 2773d0407baSopenharmony_ci * include more than one function. 2783d0407baSopenharmony_ci * 2793d0407baSopenharmony_ci * The lifecycle of a usb_configuration includes allocation, initialization 2803d0407baSopenharmony_ci * of the fields described above, and calling @usb_add_config() to set up 2813d0407baSopenharmony_ci * internal data and bind it to a specific device. The configuration's 2823d0407baSopenharmony_ci * @bind() method is then used to initialize all the functions and then 2833d0407baSopenharmony_ci * call @usb_add_function() for them. 2843d0407baSopenharmony_ci * 2853d0407baSopenharmony_ci * Those functions would normally be independent of each other, but that's 2863d0407baSopenharmony_ci * not mandatory. CDC WMC devices are an example where functions often 2873d0407baSopenharmony_ci * depend on other functions, with some functions subsidiary to others. 2883d0407baSopenharmony_ci * Such interdependency may be managed in any way, so long as all of the 2893d0407baSopenharmony_ci * descriptors complete by the time the composite driver returns from 2903d0407baSopenharmony_ci * its bind() routine. 2913d0407baSopenharmony_ci */ 2923d0407baSopenharmony_cistruct usb_configuration { 2933d0407baSopenharmony_ci const char *label; 2943d0407baSopenharmony_ci struct usb_gadget_strings **strings; 2953d0407baSopenharmony_ci const struct usb_descriptor_header **descriptors; 2963d0407baSopenharmony_ci 2973d0407baSopenharmony_ci /* REVISIT: bind() functions can be marked __init, which 2983d0407baSopenharmony_ci * makes trouble for section mismatch analysis. See if 2993d0407baSopenharmony_ci * we can't restructure things to avoid mismatching... 3003d0407baSopenharmony_ci */ 3013d0407baSopenharmony_ci 3023d0407baSopenharmony_ci /* configuration management: unbind/setup */ 3033d0407baSopenharmony_ci void (*unbind)(struct usb_configuration *); 3043d0407baSopenharmony_ci int (*setup)(struct usb_configuration *, const struct usb_ctrlrequest *); 3053d0407baSopenharmony_ci 3063d0407baSopenharmony_ci /* fields in the config descriptor */ 3073d0407baSopenharmony_ci u8 bConfigurationValue; 3083d0407baSopenharmony_ci u8 iConfiguration; 3093d0407baSopenharmony_ci u8 bmAttributes; 3103d0407baSopenharmony_ci u16 MaxPower; 3113d0407baSopenharmony_ci 3123d0407baSopenharmony_ci struct usb_composite_dev *cdev; 3133d0407baSopenharmony_ci 3143d0407baSopenharmony_ci /* private: */ 3153d0407baSopenharmony_ci /* internals */ 3163d0407baSopenharmony_ci struct list_head list; 3173d0407baSopenharmony_ci struct list_head functions; 3183d0407baSopenharmony_ci u8 next_interface_id; 3193d0407baSopenharmony_ci unsigned superspeed : 1; 3203d0407baSopenharmony_ci unsigned highspeed : 1; 3213d0407baSopenharmony_ci unsigned fullspeed : 1; 3223d0407baSopenharmony_ci unsigned superspeed_plus : 1; 3233d0407baSopenharmony_ci struct usb_function *interface[MAX_CONFIG_INTERFACES]; 3243d0407baSopenharmony_ci}; 3253d0407baSopenharmony_ci 3263d0407baSopenharmony_ciint usb_add_config(struct usb_composite_dev *, struct usb_configuration *, int (*)(struct usb_configuration *)); 3273d0407baSopenharmony_ci 3283d0407baSopenharmony_civoid usb_remove_config(struct usb_composite_dev *, struct usb_configuration *); 3293d0407baSopenharmony_ci 3303d0407baSopenharmony_ci/* predefined index for usb_composite_driver */ 3313d0407baSopenharmony_cienum { 3323d0407baSopenharmony_ci USB_GADGET_MANUFACTURER_IDX = 0, 3333d0407baSopenharmony_ci USB_GADGET_PRODUCT_IDX, 3343d0407baSopenharmony_ci USB_GADGET_SERIAL_IDX, 3353d0407baSopenharmony_ci USB_GADGET_FIRST_AVAIL_IDX, 3363d0407baSopenharmony_ci}; 3373d0407baSopenharmony_ci 3383d0407baSopenharmony_ci/** 3393d0407baSopenharmony_ci * struct usb_composite_driver - groups configurations into a gadget 3403d0407baSopenharmony_ci * @name: For diagnostics, identifies the driver. 3413d0407baSopenharmony_ci * @dev: Template descriptor for the device, including default device 3423d0407baSopenharmony_ci * identifiers. 3433d0407baSopenharmony_ci * @strings: tables of strings, keyed by identifiers assigned during @bind 3443d0407baSopenharmony_ci * and language IDs provided in control requests. Note: The first entries 3453d0407baSopenharmony_ci * are predefined. The first entry that may be used is 3463d0407baSopenharmony_ci * USB_GADGET_FIRST_AVAIL_IDX 3473d0407baSopenharmony_ci * @max_speed: Highest speed the driver supports. 3483d0407baSopenharmony_ci * @needs_serial: set to 1 if the gadget needs userspace to provide 3493d0407baSopenharmony_ci * a serial number. If one is not provided, warning will be printed. 3503d0407baSopenharmony_ci * @bind: (REQUIRED) Used to allocate resources that are shared across the 3513d0407baSopenharmony_ci * whole device, such as string IDs, and add its configurations using 3523d0407baSopenharmony_ci * @usb_add_config(). This may fail by returning a negative errno 3533d0407baSopenharmony_ci * value; it should return zero on successful initialization. 3543d0407baSopenharmony_ci * @unbind: Reverses @bind; called as a side effect of unregistering 3553d0407baSopenharmony_ci * this driver. 3563d0407baSopenharmony_ci * @disconnect: optional driver disconnect method 3573d0407baSopenharmony_ci * @suspend: Notifies when the host stops sending USB traffic, 3583d0407baSopenharmony_ci * after function notifications 3593d0407baSopenharmony_ci * @resume: Notifies configuration when the host restarts USB traffic, 3603d0407baSopenharmony_ci * before function notifications 3613d0407baSopenharmony_ci * @gadget_driver: Gadget driver controlling this driver 3623d0407baSopenharmony_ci * 3633d0407baSopenharmony_ci * Devices default to reporting self powered operation. Devices which rely 3643d0407baSopenharmony_ci * on bus powered operation should report this in their @bind method. 3653d0407baSopenharmony_ci * 3663d0407baSopenharmony_ci * Before returning from @bind, various fields in the template descriptor 3673d0407baSopenharmony_ci * may be overridden. These include the idVendor/idProduct/bcdDevice values 3683d0407baSopenharmony_ci * normally to bind the appropriate host side driver, and the three strings 3693d0407baSopenharmony_ci * (iManufacturer, iProduct, iSerialNumber) normally used to provide user 3703d0407baSopenharmony_ci * meaningful device identifiers. (The strings will not be defined unless 3713d0407baSopenharmony_ci * they are defined in @dev and @strings.) The correct ep0 maxpacket size 3723d0407baSopenharmony_ci * is also reported, as defined by the underlying controller driver. 3733d0407baSopenharmony_ci */ 3743d0407baSopenharmony_cistruct usb_composite_driver { 3753d0407baSopenharmony_ci const char *name; 3763d0407baSopenharmony_ci const struct usb_device_descriptor *dev; 3773d0407baSopenharmony_ci struct usb_gadget_strings **strings; 3783d0407baSopenharmony_ci enum usb_device_speed max_speed; 3793d0407baSopenharmony_ci unsigned needs_serial : 1; 3803d0407baSopenharmony_ci 3813d0407baSopenharmony_ci int (*bind)(struct usb_composite_dev *cdev); 3823d0407baSopenharmony_ci int (*unbind)(struct usb_composite_dev *); 3833d0407baSopenharmony_ci 3843d0407baSopenharmony_ci void (*disconnect)(struct usb_composite_dev *); 3853d0407baSopenharmony_ci 3863d0407baSopenharmony_ci /* global suspend hooks */ 3873d0407baSopenharmony_ci void (*suspend)(struct usb_composite_dev *); 3883d0407baSopenharmony_ci void (*resume)(struct usb_composite_dev *); 3893d0407baSopenharmony_ci struct usb_gadget_driver gadget_driver; 3903d0407baSopenharmony_ci}; 3913d0407baSopenharmony_ci 3923d0407baSopenharmony_ciextern int usb_composite_probe(struct usb_composite_driver *driver); 3933d0407baSopenharmony_ciextern void usb_composite_unregister(struct usb_composite_driver *driver); 3943d0407baSopenharmony_ci 3953d0407baSopenharmony_ci/** 3963d0407baSopenharmony_ci * module_usb_composite_driver() - Helper macro for registering a USB gadget 3973d0407baSopenharmony_ci * composite driver 3983d0407baSopenharmony_ci * @__usb_composite_driver: usb_composite_driver struct 3993d0407baSopenharmony_ci * 4003d0407baSopenharmony_ci * Helper macro for USB gadget composite drivers which do not do anything 4013d0407baSopenharmony_ci * special in module init/exit. This eliminates a lot of boilerplate. Each 4023d0407baSopenharmony_ci * module may only use this macro once, and calling it replaces module_init() 4033d0407baSopenharmony_ci * and module_exit() 4043d0407baSopenharmony_ci */ 4053d0407baSopenharmony_ci#define module_usb_composite_driver(__usb_composite_driver) \ 4063d0407baSopenharmony_ci module_driver(__usb_composite_driver, usb_composite_probe, usb_composite_unregister) 4073d0407baSopenharmony_ci 4083d0407baSopenharmony_ciextern void usb_composite_setup_continue(struct usb_composite_dev *cdev); 4093d0407baSopenharmony_ciextern int composite_dev_prepare(struct usb_composite_driver *composite, struct usb_composite_dev *cdev); 4103d0407baSopenharmony_ciextern int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, struct usb_ep *ep0); 4113d0407baSopenharmony_civoid composite_dev_cleanup(struct usb_composite_dev *cdev); 4123d0407baSopenharmony_ci 4133d0407baSopenharmony_cistatic inline struct usb_composite_driver *to_cdriver(struct usb_gadget_driver *gdrv) 4143d0407baSopenharmony_ci{ 4153d0407baSopenharmony_ci return container_of(gdrv, struct usb_composite_driver, gadget_driver); 4163d0407baSopenharmony_ci} 4173d0407baSopenharmony_ci 4183d0407baSopenharmony_ci#define OS_STRING_QW_SIGN_LEN 14 4193d0407baSopenharmony_ci#define OS_STRING_IDX 0xEE 4203d0407baSopenharmony_ci 4213d0407baSopenharmony_ci/** 4223d0407baSopenharmony_ci * struct usb_composite_dev - represents one composite usb gadget 4233d0407baSopenharmony_ci * @gadget: read-only, abstracts the gadget's usb peripheral controller 4243d0407baSopenharmony_ci * @req: used for control responses; buffer is pre-allocated 4253d0407baSopenharmony_ci * @os_desc_req: used for OS descriptors responses; buffer is pre-allocated 4263d0407baSopenharmony_ci * @config: the currently active configuration 4273d0407baSopenharmony_ci * @qw_sign: qwSignature part of the OS string 4283d0407baSopenharmony_ci * @b_vendor_code: bMS_VendorCode part of the OS string 4293d0407baSopenharmony_ci * @use_os_string: false by default, interested gadgets set it 4303d0407baSopenharmony_ci * @os_desc_config: the configuration to be used with OS descriptors 4313d0407baSopenharmony_ci * @setup_pending: true when setup request is queued but not completed 4323d0407baSopenharmony_ci * @os_desc_pending: true when os_desc request is queued but not completed 4333d0407baSopenharmony_ci * 4343d0407baSopenharmony_ci * One of these devices is allocated and initialized before the 4353d0407baSopenharmony_ci * associated device driver's bind() is called. 4363d0407baSopenharmony_ci * 4373d0407baSopenharmony_ci * OPEN ISSUE: it appears that some WUSB devices will need to be 4383d0407baSopenharmony_ci * built by combining a normal (wired) gadget with a wireless one. 4393d0407baSopenharmony_ci * This revision of the gadget framework should probably try to make 4403d0407baSopenharmony_ci * sure doing that won't hurt too much. 4413d0407baSopenharmony_ci * 4423d0407baSopenharmony_ci * One notion for how to handle Wireless USB devices involves: 4433d0407baSopenharmony_ci * 4443d0407baSopenharmony_ci * (a) a second gadget here, discovery mechanism, but likely 4453d0407baSopenharmony_ci * needing separate "register/unregister WUSB gadget" calls; 4463d0407baSopenharmony_ci * (b) updates to usb_gadget to include flags "is it wireless", 4473d0407baSopenharmony_ci * "is it wired", plus (presumably in a wrapper structure) 4483d0407baSopenharmony_ci * bandgroup and PHY info; 4493d0407baSopenharmony_ci * (c) presumably a wireless_ep wrapping a usb_ep, and reporting 4503d0407baSopenharmony_ci * wireless-specific parameters like maxburst and maxsequence; 4513d0407baSopenharmony_ci * (d) configurations that are specific to wireless links; 4523d0407baSopenharmony_ci * (e) function drivers that understand wireless configs and will 4533d0407baSopenharmony_ci * support wireless for (additional) function instances; 4543d0407baSopenharmony_ci * (f) a function to support association setup (like CBAF), not 4553d0407baSopenharmony_ci * necessarily requiring a wireless adapter; 4563d0407baSopenharmony_ci * (g) composite device setup that can create one or more wireless 4573d0407baSopenharmony_ci * configs, including appropriate association setup support; 4583d0407baSopenharmony_ci * (h) more. 4593d0407baSopenharmony_ci */ 4603d0407baSopenharmony_cistruct usb_composite_dev { 4613d0407baSopenharmony_ci struct usb_gadget *gadget; 4623d0407baSopenharmony_ci struct usb_request *req; 4633d0407baSopenharmony_ci struct usb_request *os_desc_req; 4643d0407baSopenharmony_ci 4653d0407baSopenharmony_ci struct usb_configuration *config; 4663d0407baSopenharmony_ci 4673d0407baSopenharmony_ci /* OS String is a custom (yet popular) extension to the USB standard. */ 4683d0407baSopenharmony_ci u8 qw_sign[OS_STRING_QW_SIGN_LEN]; 4693d0407baSopenharmony_ci u8 b_vendor_code; 4703d0407baSopenharmony_ci struct usb_configuration *os_desc_config; 4713d0407baSopenharmony_ci unsigned int use_os_string : 1; 4723d0407baSopenharmony_ci 4733d0407baSopenharmony_ci /* private: */ 4743d0407baSopenharmony_ci /* internals */ 4753d0407baSopenharmony_ci unsigned int suspended : 1; 4763d0407baSopenharmony_ci struct usb_device_descriptor desc; 4773d0407baSopenharmony_ci struct list_head configs; 4783d0407baSopenharmony_ci struct list_head gstrings; 4793d0407baSopenharmony_ci struct usb_composite_driver *driver; 4803d0407baSopenharmony_ci u8 next_string_id; 4813d0407baSopenharmony_ci char *def_manufacturer; 4823d0407baSopenharmony_ci 4833d0407baSopenharmony_ci /* the gadget driver won't enable the data pullup 4843d0407baSopenharmony_ci * while the deactivation count is nonzero. 4853d0407baSopenharmony_ci */ 4863d0407baSopenharmony_ci unsigned deactivations; 4873d0407baSopenharmony_ci 4883d0407baSopenharmony_ci /* the composite driver won't complete the control transfer's 4893d0407baSopenharmony_ci * data/status stages till delayed_status is zero. 4903d0407baSopenharmony_ci */ 4913d0407baSopenharmony_ci int delayed_status; 4923d0407baSopenharmony_ci 4933d0407baSopenharmony_ci /* protects deactivations and delayed_status counts */ 4943d0407baSopenharmony_ci spinlock_t lock; 4953d0407baSopenharmony_ci 4963d0407baSopenharmony_ci /* public: */ 4973d0407baSopenharmony_ci unsigned int setup_pending : 1; 4983d0407baSopenharmony_ci unsigned int os_desc_pending : 1; 4993d0407baSopenharmony_ci}; 5003d0407baSopenharmony_ci 5013d0407baSopenharmony_ciextern int usb_string_id(struct usb_composite_dev *c); 5023d0407baSopenharmony_ciextern int usb_string_ids_tab(struct usb_composite_dev *c, struct usb_string *str); 5033d0407baSopenharmony_ciextern struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev, struct usb_gadget_strings **sp, 5043d0407baSopenharmony_ci unsigned n_strings); 5053d0407baSopenharmony_ci 5063d0407baSopenharmony_ciextern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n); 5073d0407baSopenharmony_ci 5083d0407baSopenharmony_ciextern void composite_disconnect(struct usb_gadget *gadget); 5093d0407baSopenharmony_ciextern void composite_reset(struct usb_gadget *gadget); 5103d0407baSopenharmony_ci 5113d0407baSopenharmony_ciextern int composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl); 5123d0407baSopenharmony_ciextern void composite_suspend(struct usb_gadget *gadget); 5133d0407baSopenharmony_ciextern void composite_resume(struct usb_gadget *gadget); 5143d0407baSopenharmony_ci 5153d0407baSopenharmony_ci/* 5163d0407baSopenharmony_ci * Some systems will need runtime overrides for the product identifiers 5173d0407baSopenharmony_ci * published in the device descriptor, either numbers or strings or both. 5183d0407baSopenharmony_ci * String parameters are in UTF-8 (superset of ASCII's 7 bit characters). 5193d0407baSopenharmony_ci */ 5203d0407baSopenharmony_cistruct usb_composite_overwrite { 5213d0407baSopenharmony_ci u16 idVendor; 5223d0407baSopenharmony_ci u16 idProduct; 5233d0407baSopenharmony_ci u16 bcdDevice; 5243d0407baSopenharmony_ci char *serial_number; 5253d0407baSopenharmony_ci char *manufacturer; 5263d0407baSopenharmony_ci char *product; 5273d0407baSopenharmony_ci}; 5283d0407baSopenharmony_ci#define USB_GADGET_COMPOSITE_OPTIONS() \ 5293d0407baSopenharmony_ci static struct usb_composite_overwrite coverwrite; \ 5303d0407baSopenharmony_ci \ 5313d0407baSopenharmony_ci module_param_named(idVendor, coverwrite.idVendor, ushort, S_IRUGO); \ 5323d0407baSopenharmony_ci MODULE_PARM_DESC(idVendor, "USB Vendor ID"); \ 5333d0407baSopenharmony_ci \ 5343d0407baSopenharmony_ci module_param_named(idProduct, coverwrite.idProduct, ushort, S_IRUGO); \ 5353d0407baSopenharmony_ci MODULE_PARM_DESC(idProduct, "USB Product ID"); \ 5363d0407baSopenharmony_ci \ 5373d0407baSopenharmony_ci module_param_named(bcdDevice, coverwrite.bcdDevice, ushort, S_IRUGO); \ 5383d0407baSopenharmony_ci MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); \ 5393d0407baSopenharmony_ci \ 5403d0407baSopenharmony_ci module_param_named(iSerialNumber, coverwrite.serial_number, charp, S_IRUGO); \ 5413d0407baSopenharmony_ci MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); \ 5423d0407baSopenharmony_ci \ 5433d0407baSopenharmony_ci module_param_named(iManufacturer, coverwrite.manufacturer, charp, S_IRUGO); \ 5443d0407baSopenharmony_ci MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); \ 5453d0407baSopenharmony_ci \ 5463d0407baSopenharmony_ci module_param_named(iProduct, coverwrite.product, charp, S_IRUGO); \ 5473d0407baSopenharmony_ci MODULE_PARM_DESC(iProduct, "USB Product string") 5483d0407baSopenharmony_ci 5493d0407baSopenharmony_civoid usb_composite_overwrite_options(struct usb_composite_dev *cdev, struct usb_composite_overwrite *covr); 5503d0407baSopenharmony_ci 5513d0407baSopenharmony_cistatic inline u16 get_default_bcdDevice(void) 5523d0407baSopenharmony_ci{ 5533d0407baSopenharmony_ci u16 bcdDevice; 5543d0407baSopenharmony_ci 5553d0407baSopenharmony_ci bcdDevice = bin2bcd(((LINUX_VERSION_CODE >> 0x10) & 0xff)) << 0x8; 5563d0407baSopenharmony_ci bcdDevice |= bin2bcd(((LINUX_VERSION_CODE >> 0x8) & 0xff)); 5573d0407baSopenharmony_ci return bcdDevice; 5583d0407baSopenharmony_ci} 5593d0407baSopenharmony_ci 5603d0407baSopenharmony_cistruct usb_function_driver { 5613d0407baSopenharmony_ci const char *name; 5623d0407baSopenharmony_ci struct module *mod; 5633d0407baSopenharmony_ci struct list_head list; 5643d0407baSopenharmony_ci struct usb_function_instance *(*alloc_inst)(void); 5653d0407baSopenharmony_ci struct usb_function *(*alloc_func)(struct usb_function_instance *inst); 5663d0407baSopenharmony_ci}; 5673d0407baSopenharmony_ci 5683d0407baSopenharmony_cistruct usb_function_instance { 5693d0407baSopenharmony_ci struct config_group group; 5703d0407baSopenharmony_ci struct list_head cfs_list; 5713d0407baSopenharmony_ci struct usb_function_driver *fd; 5723d0407baSopenharmony_ci struct usb_function *f; 5733d0407baSopenharmony_ci int (*set_inst_name)(struct usb_function_instance *inst, const char *name); 5743d0407baSopenharmony_ci void (*free_func_inst)(struct usb_function_instance *inst); 5753d0407baSopenharmony_ci}; 5763d0407baSopenharmony_ci 5773d0407baSopenharmony_civoid usb_function_unregister(struct usb_function_driver *f); 5783d0407baSopenharmony_ciint usb_function_register(struct usb_function_driver *newf); 5793d0407baSopenharmony_civoid usb_put_function_instance(struct usb_function_instance *fi); 5803d0407baSopenharmony_civoid usb_put_function(struct usb_function *f); 5813d0407baSopenharmony_cistruct usb_function_instance *usb_get_function_instance(const char *name); 5823d0407baSopenharmony_cistruct usb_function *usb_get_function(struct usb_function_instance *fi); 5833d0407baSopenharmony_ci 5843d0407baSopenharmony_cistruct usb_configuration *usb_get_config(struct usb_composite_dev *cdev, int val); 5853d0407baSopenharmony_ciint usb_add_config_only(struct usb_composite_dev *cdev, struct usb_configuration *config); 5863d0407baSopenharmony_civoid usb_remove_function(struct usb_configuration *c, struct usb_function *f); 5873d0407baSopenharmony_ci 5883d0407baSopenharmony_ci#define DECLARE_USB_FUNCTION(_name, _inst_alloc, _func_alloc) \ 5893d0407baSopenharmony_ci static struct usb_function_driver _name##usb_func = { \ 5903d0407baSopenharmony_ci .name = __stringify(_name), \ 5913d0407baSopenharmony_ci .mod = THIS_MODULE, \ 5923d0407baSopenharmony_ci .alloc_inst = (_inst_alloc), \ 5933d0407baSopenharmony_ci .alloc_func = (_func_alloc), \ 5943d0407baSopenharmony_ci }; \ 5953d0407baSopenharmony_ci MODULE_ALIAS("usbfunc:"__stringify(_name)); 5963d0407baSopenharmony_ci 5973d0407baSopenharmony_ci#define DECLARE_USB_FUNCTION_INIT(_name, _inst_alloc, _func_alloc) \ 5983d0407baSopenharmony_ci DECLARE_USB_FUNCTION(_name, _inst_alloc, _func_alloc) \ 5993d0407baSopenharmony_ci static int __init _name##mod_init(void) \ 6003d0407baSopenharmony_ci { \ 6013d0407baSopenharmony_ci return usb_function_register(&_name##usb_func); \ 6023d0407baSopenharmony_ci } \ 6033d0407baSopenharmony_ci static void __exit _name##mod_exit(void) \ 6043d0407baSopenharmony_ci { \ 6053d0407baSopenharmony_ci usb_function_unregister(&_name##usb_func); \ 6063d0407baSopenharmony_ci } \ 6073d0407baSopenharmony_ci module_init(_name##mod_init); \ 6083d0407baSopenharmony_ci module_exit(_name##mod_exit) 6093d0407baSopenharmony_ci 6103d0407baSopenharmony_ci/* messaging utils */ 6113d0407baSopenharmony_ci#define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev, fmt, ##args) 6123d0407baSopenharmony_ci#define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev, fmt, ##args) 6133d0407baSopenharmony_ci#define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev, fmt, ##args) 6143d0407baSopenharmony_ci#define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev, fmt, ##args) 6153d0407baSopenharmony_ci#define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev, fmt, ##args) 6163d0407baSopenharmony_ci 6173d0407baSopenharmony_ci#endif /* __LINUX_USB_COMPOSITE_H */ 618