13d0407baSopenharmony_ci// SPDX-License-Identifier: GPL-2.0 23d0407baSopenharmony_ci/* 33d0407baSopenharmony_ci * <linux/usb/gadget.h> 43d0407baSopenharmony_ci * 53d0407baSopenharmony_ci * We call the USB code inside a Linux-based peripheral device a "gadget" 63d0407baSopenharmony_ci * driver, except for the hardware-specific bus glue. One USB host can 73d0407baSopenharmony_ci * talk to many USB gadgets, but the gadgets are only able to communicate 83d0407baSopenharmony_ci * to one host. 93d0407baSopenharmony_ci * 103d0407baSopenharmony_ci * 113d0407baSopenharmony_ci * (C) Copyright 2002-2004 by David Brownell 123d0407baSopenharmony_ci * All Rights Reserved. 133d0407baSopenharmony_ci * 143d0407baSopenharmony_ci * This software is licensed under the GNU GPL version 2. 153d0407baSopenharmony_ci */ 163d0407baSopenharmony_ci 173d0407baSopenharmony_ci#ifndef __LINUX_USB_GADGET_H 183d0407baSopenharmony_ci#define __LINUX_USB_GADGET_H 193d0407baSopenharmony_ci 203d0407baSopenharmony_ci#include <linux/device.h> 213d0407baSopenharmony_ci#include <linux/errno.h> 223d0407baSopenharmony_ci#include <linux/init.h> 233d0407baSopenharmony_ci#include <linux/list.h> 243d0407baSopenharmony_ci#include <linux/slab.h> 253d0407baSopenharmony_ci#include <linux/scatterlist.h> 263d0407baSopenharmony_ci#include <linux/types.h> 273d0407baSopenharmony_ci#include <linux/workqueue.h> 283d0407baSopenharmony_ci#include <linux/usb/ch9.h> 293d0407baSopenharmony_ci#include <linux/android_kabi.h> 303d0407baSopenharmony_ci 313d0407baSopenharmony_ci#define UDC_TRACE_STR_MAX 512 323d0407baSopenharmony_ci 333d0407baSopenharmony_cistruct usb_ep; 343d0407baSopenharmony_ci 353d0407baSopenharmony_ci/** 363d0407baSopenharmony_ci * struct usb_request - describes one i/o request 373d0407baSopenharmony_ci * @buf: Buffer used for data. Always provide this; some controllers 383d0407baSopenharmony_ci * only use PIO, or don't use DMA for some endpoints. 393d0407baSopenharmony_ci * @dma: DMA address corresponding to 'buf'. If you don't set this 403d0407baSopenharmony_ci * field, and the usb controller needs one, it is responsible 413d0407baSopenharmony_ci * for mapping and unmapping the buffer. 423d0407baSopenharmony_ci * @sg: a scatterlist for SG-capable controllers. 433d0407baSopenharmony_ci * @num_sgs: number of SG entries 443d0407baSopenharmony_ci * @num_mapped_sgs: number of SG entries mapped to DMA (internal) 453d0407baSopenharmony_ci * @length: Length of that data 463d0407baSopenharmony_ci * @stream_id: The stream id, when USB3.0 bulk streams are being used 473d0407baSopenharmony_ci * @is_last: Indicates if this is the last request of a stream_id before 483d0407baSopenharmony_ci * switching to a different stream (required for DWC3 controllers). 493d0407baSopenharmony_ci * @no_interrupt: If true, hints that no completion irq is needed. 503d0407baSopenharmony_ci * Helpful sometimes with deep request queues that are handled 513d0407baSopenharmony_ci * directly by DMA controllers. 523d0407baSopenharmony_ci * @zero: If true, when writing data, makes the last packet be "short" 533d0407baSopenharmony_ci * by adding a zero length packet as needed; 543d0407baSopenharmony_ci * @short_not_ok: When reading data, makes short packets be 553d0407baSopenharmony_ci * treated as errors (queue stops advancing till cleanup). 563d0407baSopenharmony_ci * @dma_mapped: Indicates if request has been mapped to DMA (internal) 573d0407baSopenharmony_ci * @complete: Function called when request completes, so this request and 583d0407baSopenharmony_ci * its buffer may be re-used. The function will always be called with 593d0407baSopenharmony_ci * interrupts disabled, and it must not sleep. 603d0407baSopenharmony_ci * Reads terminate with a short packet, or when the buffer fills, 613d0407baSopenharmony_ci * whichever comes first. When writes terminate, some data bytes 623d0407baSopenharmony_ci * will usually still be in flight (often in a hardware fifo). 633d0407baSopenharmony_ci * Errors (for reads or writes) stop the queue from advancing 643d0407baSopenharmony_ci * until the completion function returns, so that any transfers 653d0407baSopenharmony_ci * invalidated by the error may first be dequeued. 663d0407baSopenharmony_ci * @context: For use by the completion callback 673d0407baSopenharmony_ci * @list: For use by the gadget driver. 683d0407baSopenharmony_ci * @frame_number: Reports the interval number in (micro)frame in which the 693d0407baSopenharmony_ci * isochronous transfer was transmitted or received. 703d0407baSopenharmony_ci * @status: Reports completion code, zero or a negative errno. 713d0407baSopenharmony_ci * Normally, faults block the transfer queue from advancing until 723d0407baSopenharmony_ci * the completion callback returns. 733d0407baSopenharmony_ci * Code "-ESHUTDOWN" indicates completion caused by device disconnect, 743d0407baSopenharmony_ci * or when the driver disabled the endpoint. 753d0407baSopenharmony_ci * @actual: Reports bytes transferred to/from the buffer. For reads (OUT 763d0407baSopenharmony_ci * transfers) this may be less than the requested length. If the 773d0407baSopenharmony_ci * short_not_ok flag is set, short reads are treated as errors 783d0407baSopenharmony_ci * even when status otherwise indicates successful completion. 793d0407baSopenharmony_ci * Note that for writes (IN transfers) some data bytes may still 803d0407baSopenharmony_ci * reside in a device-side FIFO when the request is reported as 813d0407baSopenharmony_ci * complete. 823d0407baSopenharmony_ci * 833d0407baSopenharmony_ci * These are allocated/freed through the endpoint they're used with. The 843d0407baSopenharmony_ci * hardware's driver can add extra per-request data to the memory it returns, 853d0407baSopenharmony_ci * which often avoids separate memory allocations (potential failures), 863d0407baSopenharmony_ci * later when the request is queued. 873d0407baSopenharmony_ci * 883d0407baSopenharmony_ci * Request flags affect request handling, such as whether a zero length 893d0407baSopenharmony_ci * packet is written (the "zero" flag), whether a short read should be 903d0407baSopenharmony_ci * treated as an error (blocking request queue advance, the "short_not_ok" 913d0407baSopenharmony_ci * flag), or hinting that an interrupt is not required (the "no_interrupt" 923d0407baSopenharmony_ci * flag, for use with deep request queues). 933d0407baSopenharmony_ci * 943d0407baSopenharmony_ci * Bulk endpoints can use any size buffers, and can also be used for interrupt 953d0407baSopenharmony_ci * transfers. interrupt-only endpoints can be much less functional. 963d0407baSopenharmony_ci * 973d0407baSopenharmony_ci * NOTE: this is analogous to 'struct urb' on the host side, except that 983d0407baSopenharmony_ci * it's thinner and promotes more pre-allocation. 993d0407baSopenharmony_ci */ 1003d0407baSopenharmony_ci 1013d0407baSopenharmony_cistruct usb_request { 1023d0407baSopenharmony_ci void *buf; 1033d0407baSopenharmony_ci unsigned length; 1043d0407baSopenharmony_ci dma_addr_t dma; 1053d0407baSopenharmony_ci 1063d0407baSopenharmony_ci struct scatterlist *sg; 1073d0407baSopenharmony_ci unsigned num_sgs; 1083d0407baSopenharmony_ci unsigned num_mapped_sgs; 1093d0407baSopenharmony_ci 1103d0407baSopenharmony_ci unsigned stream_id:16; 1113d0407baSopenharmony_ci unsigned is_last:1; 1123d0407baSopenharmony_ci unsigned no_interrupt:1; 1133d0407baSopenharmony_ci unsigned zero:1; 1143d0407baSopenharmony_ci unsigned short_not_ok:1; 1153d0407baSopenharmony_ci unsigned dma_mapped:1; 1163d0407baSopenharmony_ci 1173d0407baSopenharmony_ci void (*complete)(struct usb_ep *ep, 1183d0407baSopenharmony_ci struct usb_request *req); 1193d0407baSopenharmony_ci void *context; 1203d0407baSopenharmony_ci struct list_head list; 1213d0407baSopenharmony_ci 1223d0407baSopenharmony_ci unsigned frame_number; /* ISO ONLY */ 1233d0407baSopenharmony_ci 1243d0407baSopenharmony_ci int status; 1253d0407baSopenharmony_ci unsigned actual; 1263d0407baSopenharmony_ci 1273d0407baSopenharmony_ci ANDROID_KABI_RESERVE(1); 1283d0407baSopenharmony_ci}; 1293d0407baSopenharmony_ci 1303d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 1313d0407baSopenharmony_ci 1323d0407baSopenharmony_ci/* endpoint-specific parts of the api to the usb controller hardware. 1333d0407baSopenharmony_ci * unlike the urb model, (de)multiplexing layers are not required. 1343d0407baSopenharmony_ci * (so this api could slash overhead if used on the host side...) 1353d0407baSopenharmony_ci * 1363d0407baSopenharmony_ci * note that device side usb controllers commonly differ in how many 1373d0407baSopenharmony_ci * endpoints they support, as well as their capabilities. 1383d0407baSopenharmony_ci */ 1393d0407baSopenharmony_cistruct usb_ep_ops { 1403d0407baSopenharmony_ci int (*enable) (struct usb_ep *ep, 1413d0407baSopenharmony_ci const struct usb_endpoint_descriptor *desc); 1423d0407baSopenharmony_ci int (*disable) (struct usb_ep *ep); 1433d0407baSopenharmony_ci void (*dispose) (struct usb_ep *ep); 1443d0407baSopenharmony_ci 1453d0407baSopenharmony_ci struct usb_request *(*alloc_request) (struct usb_ep *ep, 1463d0407baSopenharmony_ci gfp_t gfp_flags); 1473d0407baSopenharmony_ci void (*free_request) (struct usb_ep *ep, struct usb_request *req); 1483d0407baSopenharmony_ci 1493d0407baSopenharmony_ci int (*queue) (struct usb_ep *ep, struct usb_request *req, 1503d0407baSopenharmony_ci gfp_t gfp_flags); 1513d0407baSopenharmony_ci int (*dequeue) (struct usb_ep *ep, struct usb_request *req); 1523d0407baSopenharmony_ci 1533d0407baSopenharmony_ci int (*set_halt) (struct usb_ep *ep, int value); 1543d0407baSopenharmony_ci int (*set_wedge) (struct usb_ep *ep); 1553d0407baSopenharmony_ci 1563d0407baSopenharmony_ci int (*fifo_status) (struct usb_ep *ep); 1573d0407baSopenharmony_ci void (*fifo_flush) (struct usb_ep *ep); 1583d0407baSopenharmony_ci 1593d0407baSopenharmony_ci ANDROID_KABI_RESERVE(1); 1603d0407baSopenharmony_ci}; 1613d0407baSopenharmony_ci 1623d0407baSopenharmony_ci/** 1633d0407baSopenharmony_ci * struct usb_ep_caps - endpoint capabilities description 1643d0407baSopenharmony_ci * @type_control:Endpoint supports control type (reserved for ep0). 1653d0407baSopenharmony_ci * @type_iso:Endpoint supports isochronous transfers. 1663d0407baSopenharmony_ci * @type_bulk:Endpoint supports bulk transfers. 1673d0407baSopenharmony_ci * @type_int:Endpoint supports interrupt transfers. 1683d0407baSopenharmony_ci * @dir_in:Endpoint supports IN direction. 1693d0407baSopenharmony_ci * @dir_out:Endpoint supports OUT direction. 1703d0407baSopenharmony_ci */ 1713d0407baSopenharmony_cistruct usb_ep_caps { 1723d0407baSopenharmony_ci unsigned type_control:1; 1733d0407baSopenharmony_ci unsigned type_iso:1; 1743d0407baSopenharmony_ci unsigned type_bulk:1; 1753d0407baSopenharmony_ci unsigned type_int:1; 1763d0407baSopenharmony_ci unsigned dir_in:1; 1773d0407baSopenharmony_ci unsigned dir_out:1; 1783d0407baSopenharmony_ci}; 1793d0407baSopenharmony_ci 1803d0407baSopenharmony_ci#define USB_EP_CAPS_TYPE_CONTROL 0x01 1813d0407baSopenharmony_ci#define USB_EP_CAPS_TYPE_ISO 0x02 1823d0407baSopenharmony_ci#define USB_EP_CAPS_TYPE_BULK 0x04 1833d0407baSopenharmony_ci#define USB_EP_CAPS_TYPE_INT 0x08 1843d0407baSopenharmony_ci#define USB_EP_CAPS_TYPE_ALL \ 1853d0407baSopenharmony_ci (USB_EP_CAPS_TYPE_ISO | USB_EP_CAPS_TYPE_BULK | USB_EP_CAPS_TYPE_INT) 1863d0407baSopenharmony_ci#define USB_EP_CAPS_DIR_IN 0x01 1873d0407baSopenharmony_ci#define USB_EP_CAPS_DIR_OUT 0x02 1883d0407baSopenharmony_ci#define USB_EP_CAPS_DIR_ALL (USB_EP_CAPS_DIR_IN | USB_EP_CAPS_DIR_OUT) 1893d0407baSopenharmony_ci 1903d0407baSopenharmony_ci#define USB_EP_CAPS(_type, _dir) \ 1913d0407baSopenharmony_ci { \ 1923d0407baSopenharmony_ci .type_control = !!(_type & USB_EP_CAPS_TYPE_CONTROL), \ 1933d0407baSopenharmony_ci .type_iso = !!(_type & USB_EP_CAPS_TYPE_ISO), \ 1943d0407baSopenharmony_ci .type_bulk = !!(_type & USB_EP_CAPS_TYPE_BULK), \ 1953d0407baSopenharmony_ci .type_int = !!(_type & USB_EP_CAPS_TYPE_INT), \ 1963d0407baSopenharmony_ci .dir_in = !!(_dir & USB_EP_CAPS_DIR_IN), \ 1973d0407baSopenharmony_ci .dir_out = !!(_dir & USB_EP_CAPS_DIR_OUT), \ 1983d0407baSopenharmony_ci } 1993d0407baSopenharmony_ci 2003d0407baSopenharmony_ci/** 2013d0407baSopenharmony_ci * struct usb_ep - device side representation of USB endpoint 2023d0407baSopenharmony_ci * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk" 2033d0407baSopenharmony_ci * @ops: Function pointers used to access hardware-specific operations. 2043d0407baSopenharmony_ci * @ep_list:the gadget's ep_list holds all of its endpoints 2053d0407baSopenharmony_ci * @caps:The structure describing types and directions supported by endoint. 2063d0407baSopenharmony_ci * @enabled: The current endpoint enabled/disabled state. 2073d0407baSopenharmony_ci * @claimed: True if this endpoint is claimed by a function. 2083d0407baSopenharmony_ci * @maxpacket:The maximum packet size used on this endpoint. The initial 2093d0407baSopenharmony_ci * value can sometimes be reduced (hardware allowing), according to 2103d0407baSopenharmony_ci * the endpoint descriptor used to configure the endpoint. 2113d0407baSopenharmony_ci * @maxpacket_limit:The maximum packet size value which can be handled by this 2123d0407baSopenharmony_ci * endpoint. It's set once by UDC driver when endpoint is initialized, and 2133d0407baSopenharmony_ci * should not be changed. Should not be confused with maxpacket. 2143d0407baSopenharmony_ci * @max_streams: The maximum number of streams supported 2153d0407baSopenharmony_ci * by this EP (0 - 16, actual number is 2^n) 2163d0407baSopenharmony_ci * @mult: multiplier, 'mult' value for SS Isoc EPs 2173d0407baSopenharmony_ci * @maxburst: the maximum number of bursts supported by this EP (for usb3) 2183d0407baSopenharmony_ci * @driver_data:for use by the gadget driver. 2193d0407baSopenharmony_ci * @address: used to identify the endpoint when finding descriptor that 2203d0407baSopenharmony_ci * matches connection speed 2213d0407baSopenharmony_ci * @desc: endpoint descriptor. This pointer is set before the endpoint is 2223d0407baSopenharmony_ci * enabled and remains valid until the endpoint is disabled. 2233d0407baSopenharmony_ci * @comp_desc: In case of SuperSpeed support, this is the endpoint companion 2243d0407baSopenharmony_ci * descriptor that is used to configure the endpoint 2253d0407baSopenharmony_ci * @transfer_type: Used to specify transfer type of EP. 2263d0407baSopenharmony_ci * 2273d0407baSopenharmony_ci * the bus controller driver lists all the general purpose endpoints in 2283d0407baSopenharmony_ci * gadget->ep_list. the control endpoint (gadget->ep0) is not in that list, 2293d0407baSopenharmony_ci * and is accessed only in response to a driver setup() callback. 2303d0407baSopenharmony_ci */ 2313d0407baSopenharmony_ci 2323d0407baSopenharmony_cistruct usb_ep { 2333d0407baSopenharmony_ci void *driver_data; 2343d0407baSopenharmony_ci 2353d0407baSopenharmony_ci const char *name; 2363d0407baSopenharmony_ci const struct usb_ep_ops *ops; 2373d0407baSopenharmony_ci struct list_head ep_list; 2383d0407baSopenharmony_ci struct usb_ep_caps caps; 2393d0407baSopenharmony_ci bool claimed; 2403d0407baSopenharmony_ci bool enabled; 2413d0407baSopenharmony_ci unsigned maxpacket:16; 2423d0407baSopenharmony_ci unsigned maxpacket_limit:16; 2433d0407baSopenharmony_ci unsigned max_streams:16; 2443d0407baSopenharmony_ci unsigned mult:2; 2453d0407baSopenharmony_ci unsigned maxburst:5; 2463d0407baSopenharmony_ci u8 address; 2473d0407baSopenharmony_ci const struct usb_endpoint_descriptor *desc; 2483d0407baSopenharmony_ci const struct usb_ss_ep_comp_descriptor *comp_desc; 2493d0407baSopenharmony_ci#if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI) 2503d0407baSopenharmony_ci u8 transfer_type; 2513d0407baSopenharmony_ci#endif 2523d0407baSopenharmony_ci 2533d0407baSopenharmony_ci ANDROID_KABI_RESERVE(1); 2543d0407baSopenharmony_ci}; 2553d0407baSopenharmony_ci 2563d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 2573d0407baSopenharmony_ci 2583d0407baSopenharmony_ci#if IS_ENABLED(CONFIG_USB_GADGET) 2593d0407baSopenharmony_civoid usb_ep_set_maxpacket_limit(struct usb_ep *ep, unsigned maxpacket_limit); 2603d0407baSopenharmony_ciint usb_ep_enable(struct usb_ep *ep); 2613d0407baSopenharmony_ciint usb_ep_disable(struct usb_ep *ep); 2623d0407baSopenharmony_cistruct usb_request *usb_ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags); 2633d0407baSopenharmony_civoid usb_ep_free_request(struct usb_ep *ep, struct usb_request *req); 2643d0407baSopenharmony_ciint usb_ep_queue(struct usb_ep *ep, struct usb_request *req, gfp_t gfp_flags); 2653d0407baSopenharmony_ciint usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req); 2663d0407baSopenharmony_ciint usb_ep_set_halt(struct usb_ep *ep); 2673d0407baSopenharmony_ciint usb_ep_clear_halt(struct usb_ep *ep); 2683d0407baSopenharmony_ciint usb_ep_set_wedge(struct usb_ep *ep); 2693d0407baSopenharmony_ciint usb_ep_fifo_status(struct usb_ep *ep); 2703d0407baSopenharmony_civoid usb_ep_fifo_flush(struct usb_ep *ep); 2713d0407baSopenharmony_ci#else 2723d0407baSopenharmony_cistatic inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep, 2733d0407baSopenharmony_ci unsigned maxpacket_limit) 2743d0407baSopenharmony_ci{ } 2753d0407baSopenharmony_cistatic inline int usb_ep_enable(struct usb_ep *ep) 2763d0407baSopenharmony_ci{ return 0; } 2773d0407baSopenharmony_cistatic inline int usb_ep_disable(struct usb_ep *ep) 2783d0407baSopenharmony_ci{ return 0; } 2793d0407baSopenharmony_cistatic inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, 2803d0407baSopenharmony_ci gfp_t gfp_flags) 2813d0407baSopenharmony_ci{ return NULL; } 2823d0407baSopenharmony_cistatic inline void usb_ep_free_request(struct usb_ep *ep, 2833d0407baSopenharmony_ci struct usb_request *req) 2843d0407baSopenharmony_ci{ } 2853d0407baSopenharmony_cistatic inline int usb_ep_queue(struct usb_ep *ep, struct usb_request *req, 2863d0407baSopenharmony_ci gfp_t gfp_flags) 2873d0407baSopenharmony_ci{ return 0; } 2883d0407baSopenharmony_cistatic inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req) 2893d0407baSopenharmony_ci{ return 0; } 2903d0407baSopenharmony_cistatic inline int usb_ep_set_halt(struct usb_ep *ep) 2913d0407baSopenharmony_ci{ return 0; } 2923d0407baSopenharmony_cistatic inline int usb_ep_clear_halt(struct usb_ep *ep) 2933d0407baSopenharmony_ci{ return 0; } 2943d0407baSopenharmony_cistatic inline int usb_ep_set_wedge(struct usb_ep *ep) 2953d0407baSopenharmony_ci{ return 0; } 2963d0407baSopenharmony_cistatic inline int usb_ep_fifo_status(struct usb_ep *ep) 2973d0407baSopenharmony_ci{ return 0; } 2983d0407baSopenharmony_cistatic inline void usb_ep_fifo_flush(struct usb_ep *ep) 2993d0407baSopenharmony_ci{ } 3003d0407baSopenharmony_ci#endif /* USB_GADGET */ 3013d0407baSopenharmony_ci 3023d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 3033d0407baSopenharmony_ci 3043d0407baSopenharmony_cistruct usb_dcd_config_params { 3053d0407baSopenharmony_ci __u8 bU1devExitLat; /* U1 Device exit Latency */ 3063d0407baSopenharmony_ci#define USB_DEFAULT_U1_DEV_EXIT_LAT 0x01 /* Less then 1 microsec */ 3073d0407baSopenharmony_ci __le16 bU2DevExitLat; /* U2 Device exit Latency */ 3083d0407baSopenharmony_ci#define USB_DEFAULT_U2_DEV_EXIT_LAT 0x1F4 /* Less then 500 microsec */ 3093d0407baSopenharmony_ci __u8 besl_baseline; /* Recommended baseline BESL (0-15) */ 3103d0407baSopenharmony_ci __u8 besl_deep; /* Recommended deep BESL (0-15) */ 3113d0407baSopenharmony_ci#define USB_DEFAULT_BESL_UNSPECIFIED 0xFF /* No recommended value */ 3123d0407baSopenharmony_ci}; 3133d0407baSopenharmony_ci 3143d0407baSopenharmony_ci 3153d0407baSopenharmony_cistruct usb_gadget; 3163d0407baSopenharmony_cistruct usb_gadget_driver; 3173d0407baSopenharmony_cistruct usb_udc; 3183d0407baSopenharmony_ci 3193d0407baSopenharmony_ci/* the rest of the api to the controller hardware: device operations, 3203d0407baSopenharmony_ci * which don't involve endpoints (or i/o). 3213d0407baSopenharmony_ci */ 3223d0407baSopenharmony_cistruct usb_gadget_ops { 3233d0407baSopenharmony_ci int (*get_frame)(struct usb_gadget *); 3243d0407baSopenharmony_ci int (*wakeup)(struct usb_gadget *); 3253d0407baSopenharmony_ci int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered); 3263d0407baSopenharmony_ci int (*vbus_session) (struct usb_gadget *, int is_active); 3273d0407baSopenharmony_ci int (*vbus_draw) (struct usb_gadget *, unsigned mA); 3283d0407baSopenharmony_ci int (*pullup) (struct usb_gadget *, int is_on); 3293d0407baSopenharmony_ci int (*ioctl)(struct usb_gadget *, 3303d0407baSopenharmony_ci unsigned code, unsigned long param); 3313d0407baSopenharmony_ci void (*get_config_params)(struct usb_gadget *, 3323d0407baSopenharmony_ci struct usb_dcd_config_params *); 3333d0407baSopenharmony_ci int (*udc_start)(struct usb_gadget *, 3343d0407baSopenharmony_ci struct usb_gadget_driver *); 3353d0407baSopenharmony_ci int (*udc_stop)(struct usb_gadget *); 3363d0407baSopenharmony_ci void (*udc_set_speed)(struct usb_gadget *, enum usb_device_speed); 3373d0407baSopenharmony_ci void (*udc_set_ssp_rate)(struct usb_gadget *gadget, 3383d0407baSopenharmony_ci enum usb_ssp_rate rate); 3393d0407baSopenharmony_ci void (*udc_async_callbacks)(struct usb_gadget *gadget, bool enable); 3403d0407baSopenharmony_ci struct usb_ep *(*match_ep)(struct usb_gadget *, 3413d0407baSopenharmony_ci struct usb_endpoint_descriptor *, 3423d0407baSopenharmony_ci struct usb_ss_ep_comp_descriptor *); 3433d0407baSopenharmony_ci int (*check_config)(struct usb_gadget *gadget); 3443d0407baSopenharmony_ci 3453d0407baSopenharmony_ci ANDROID_KABI_RESERVE(1); 3463d0407baSopenharmony_ci ANDROID_KABI_RESERVE(2); 3473d0407baSopenharmony_ci ANDROID_KABI_RESERVE(3); 3483d0407baSopenharmony_ci ANDROID_KABI_RESERVE(4); 3493d0407baSopenharmony_ci}; 3503d0407baSopenharmony_ci 3513d0407baSopenharmony_ci/** 3523d0407baSopenharmony_ci * struct usb_gadget - represents a usb device 3533d0407baSopenharmony_ci * @work: (internal use) Workqueue to be used for sysfs_notify() 3543d0407baSopenharmony_ci * @udc: struct usb_udc pointer for this gadget 3553d0407baSopenharmony_ci * @ops: Function pointers used to access hardware-specific operations. 3563d0407baSopenharmony_ci * @ep0: Endpoint zero, used when reading or writing responses to 3573d0407baSopenharmony_ci * driver setup() requests 3583d0407baSopenharmony_ci * @ep_list: List of other endpoints supported by the device. 3593d0407baSopenharmony_ci * @speed: Speed of current connection to USB host. 3603d0407baSopenharmony_ci * @max_speed: Maximal speed the UDC can handle. UDC must support this 3613d0407baSopenharmony_ci * and all slower speeds. 3623d0407baSopenharmony_ci * @ssp_rate: Current connected SuperSpeed Plus signaling rate and lane count. 3633d0407baSopenharmony_ci * @max_ssp_rate: Maximum SuperSpeed Plus signaling rate and lane count the UDC 3643d0407baSopenharmony_ci * can handle. The UDC must support this and all slower speeds and lower 3653d0407baSopenharmony_ci * number of lanes. 3663d0407baSopenharmony_ci * @state: the state we are now (attached, suspended, configured, etc) 3673d0407baSopenharmony_ci * @name: Identifies the controller hardware type. Used in diagnostics 3683d0407baSopenharmony_ci * and sometimes configuration. 3693d0407baSopenharmony_ci * @dev: Driver model state for this abstract device. 3703d0407baSopenharmony_ci * @isoch_delay: value from Set Isoch Delay request. Only valid on SS/SSP 3713d0407baSopenharmony_ci * @out_epnum: last used out ep number 3723d0407baSopenharmony_ci * @in_epnum: last used in ep number 3733d0407baSopenharmony_ci * @mA: last set mA value 3743d0407baSopenharmony_ci * @otg_caps: OTG capabilities of this gadget. 3753d0407baSopenharmony_ci * @sg_supported: true if we can handle scatter-gather 3763d0407baSopenharmony_ci * @is_otg: True if the USB device port uses a Mini-AB jack, so that the 3773d0407baSopenharmony_ci * gadget driver must provide a USB OTG descriptor. 3783d0407baSopenharmony_ci * @is_a_peripheral: False unless is_otg, the "A" end of a USB cable 3793d0407baSopenharmony_ci * is in the Mini-AB jack, and HNP has been used to switch roles 3803d0407baSopenharmony_ci * so that the "A" device currently acts as A-Peripheral, not A-Host. 3813d0407baSopenharmony_ci * @a_hnp_support: OTG device feature flag, indicating that the A-Host 3823d0407baSopenharmony_ci * supports HNP at this port. 3833d0407baSopenharmony_ci * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host 3843d0407baSopenharmony_ci * only supports HNP on a different root port. 3853d0407baSopenharmony_ci * @b_hnp_enable: OTG device feature flag, indicating that the A-Host 3863d0407baSopenharmony_ci * enabled HNP support. 3873d0407baSopenharmony_ci * @hnp_polling_support: OTG device feature flag, indicating if the OTG device 3883d0407baSopenharmony_ci * in peripheral mode can support HNP polling. 3893d0407baSopenharmony_ci * @host_request_flag: OTG device feature flag, indicating if A-Peripheral 3903d0407baSopenharmony_ci * or B-Peripheral wants to take host role. 3913d0407baSopenharmony_ci * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to 3923d0407baSopenharmony_ci * MaxPacketSize. 3933d0407baSopenharmony_ci * @quirk_altset_not_supp: UDC controller doesn't support alt settings. 3943d0407baSopenharmony_ci * @quirk_stall_not_supp: UDC controller doesn't support stalling. 3953d0407baSopenharmony_ci * @quirk_zlp_not_supp: UDC controller doesn't support ZLP. 3963d0407baSopenharmony_ci * @quirk_avoids_skb_reserve: udc/platform wants to avoid skb_reserve() in 3973d0407baSopenharmony_ci * u_ether.c to improve performance. 3983d0407baSopenharmony_ci * @is_selfpowered: if the gadget is self-powered. 3993d0407baSopenharmony_ci * @deactivated: True if gadget is deactivated - in deactivated state it cannot 4003d0407baSopenharmony_ci * be connected. 4013d0407baSopenharmony_ci * @connected: True if gadget is connected. 4023d0407baSopenharmony_ci * @lpm_capable: If the gadget max_speed is FULL or HIGH, this flag 4033d0407baSopenharmony_ci * indicates that it supports LPM as per the LPM ECN & errata. 4043d0407baSopenharmony_ci * @irq: the interrupt number for device controller. 4053d0407baSopenharmony_ci * 4063d0407baSopenharmony_ci * Gadgets have a mostly-portable "gadget driver" implementing device 4073d0407baSopenharmony_ci * functions, handling all usb configurations and interfaces. Gadget 4083d0407baSopenharmony_ci * drivers talk to hardware-specific code indirectly, through ops vectors. 4093d0407baSopenharmony_ci * That insulates the gadget driver from hardware details, and packages 4103d0407baSopenharmony_ci * the hardware endpoints through generic i/o queues. The "usb_gadget" 4113d0407baSopenharmony_ci * and "usb_ep" interfaces provide that insulation from the hardware. 4123d0407baSopenharmony_ci * 4133d0407baSopenharmony_ci * Except for the driver data, all fields in this structure are 4143d0407baSopenharmony_ci * read-only to the gadget driver. That driver data is part of the 4153d0407baSopenharmony_ci * "driver model" infrastructure in 2.6 (and later) kernels, and for 4163d0407baSopenharmony_ci * earlier systems is grouped in a similar structure that's not known 4173d0407baSopenharmony_ci * to the rest of the kernel. 4183d0407baSopenharmony_ci * 4193d0407baSopenharmony_ci * Values of the three OTG device feature flags are updated before the 4203d0407baSopenharmony_ci * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before 4213d0407baSopenharmony_ci * driver suspend() calls. They are valid only when is_otg, and when the 4223d0407baSopenharmony_ci * device is acting as a B-Peripheral (so is_a_peripheral is false). 4233d0407baSopenharmony_ci */ 4243d0407baSopenharmony_cistruct usb_gadget { 4253d0407baSopenharmony_ci struct work_struct work; 4263d0407baSopenharmony_ci struct usb_udc *udc; 4273d0407baSopenharmony_ci /* readonly to gadget driver */ 4283d0407baSopenharmony_ci const struct usb_gadget_ops *ops; 4293d0407baSopenharmony_ci struct usb_ep *ep0; 4303d0407baSopenharmony_ci struct list_head ep_list; /* of usb_ep */ 4313d0407baSopenharmony_ci enum usb_device_speed speed; 4323d0407baSopenharmony_ci enum usb_device_speed max_speed; 4333d0407baSopenharmony_ci 4343d0407baSopenharmony_ci /* USB SuperSpeed Plus only */ 4353d0407baSopenharmony_ci enum usb_ssp_rate ssp_rate; 4363d0407baSopenharmony_ci enum usb_ssp_rate max_ssp_rate; 4373d0407baSopenharmony_ci 4383d0407baSopenharmony_ci enum usb_device_state state; 4393d0407baSopenharmony_ci const char *name; 4403d0407baSopenharmony_ci struct device dev; 4413d0407baSopenharmony_ci unsigned isoch_delay; 4423d0407baSopenharmony_ci unsigned out_epnum; 4433d0407baSopenharmony_ci unsigned in_epnum; 4443d0407baSopenharmony_ci unsigned mA; 4453d0407baSopenharmony_ci struct usb_otg_caps *otg_caps; 4463d0407baSopenharmony_ci 4473d0407baSopenharmony_ci unsigned sg_supported:1; 4483d0407baSopenharmony_ci unsigned is_otg:1; 4493d0407baSopenharmony_ci unsigned is_a_peripheral:1; 4503d0407baSopenharmony_ci unsigned b_hnp_enable:1; 4513d0407baSopenharmony_ci unsigned a_hnp_support:1; 4523d0407baSopenharmony_ci unsigned a_alt_hnp_support:1; 4533d0407baSopenharmony_ci unsigned hnp_polling_support:1; 4543d0407baSopenharmony_ci unsigned host_request_flag:1; 4553d0407baSopenharmony_ci unsigned quirk_ep_out_aligned_size:1; 4563d0407baSopenharmony_ci unsigned quirk_altset_not_supp:1; 4573d0407baSopenharmony_ci unsigned quirk_stall_not_supp:1; 4583d0407baSopenharmony_ci unsigned quirk_zlp_not_supp:1; 4593d0407baSopenharmony_ci unsigned quirk_avoids_skb_reserve:1; 4603d0407baSopenharmony_ci unsigned is_selfpowered:1; 4613d0407baSopenharmony_ci unsigned deactivated:1; 4623d0407baSopenharmony_ci unsigned connected:1; 4633d0407baSopenharmony_ci unsigned lpm_capable:1; 4643d0407baSopenharmony_ci int irq; 4653d0407baSopenharmony_ci 4663d0407baSopenharmony_ci ANDROID_KABI_RESERVE(1); 4673d0407baSopenharmony_ci ANDROID_KABI_RESERVE(2); 4683d0407baSopenharmony_ci ANDROID_KABI_RESERVE(3); 4693d0407baSopenharmony_ci ANDROID_KABI_RESERVE(4); 4703d0407baSopenharmony_ci}; 4713d0407baSopenharmony_ci#define work_to_gadget(w) (container_of((w), struct usb_gadget, work)) 4723d0407baSopenharmony_ci 4733d0407baSopenharmony_ci/* Interface to the device model */ 4743d0407baSopenharmony_cistatic inline void set_gadget_data(struct usb_gadget *gadget, void *data) 4753d0407baSopenharmony_ci { dev_set_drvdata(&gadget->dev, data); } 4763d0407baSopenharmony_cistatic inline void *get_gadget_data(struct usb_gadget *gadget) 4773d0407baSopenharmony_ci { return dev_get_drvdata(&gadget->dev); } 4783d0407baSopenharmony_cistatic inline struct usb_gadget *dev_to_usb_gadget(struct device *dev) 4793d0407baSopenharmony_ci{ 4803d0407baSopenharmony_ci return container_of(dev, struct usb_gadget, dev); 4813d0407baSopenharmony_ci} 4823d0407baSopenharmony_cistatic inline struct usb_gadget *usb_get_gadget(struct usb_gadget *gadget) 4833d0407baSopenharmony_ci{ 4843d0407baSopenharmony_ci get_device(&gadget->dev); 4853d0407baSopenharmony_ci return gadget; 4863d0407baSopenharmony_ci} 4873d0407baSopenharmony_cistatic inline void usb_put_gadget(struct usb_gadget *gadget) 4883d0407baSopenharmony_ci{ 4893d0407baSopenharmony_ci put_device(&gadget->dev); 4903d0407baSopenharmony_ci} 4913d0407baSopenharmony_ciextern void usb_initialize_gadget(struct device *parent, 4923d0407baSopenharmony_ci struct usb_gadget *gadget, void (*release)(struct device *dev)); 4933d0407baSopenharmony_ciextern int usb_add_gadget(struct usb_gadget *gadget); 4943d0407baSopenharmony_ciextern void usb_del_gadget(struct usb_gadget *gadget); 4953d0407baSopenharmony_ci 4963d0407baSopenharmony_ci/* Legacy device-model interface */ 4973d0407baSopenharmony_ciextern int usb_add_gadget_udc_release(struct device *parent, 4983d0407baSopenharmony_ci struct usb_gadget *gadget, void (*release)(struct device *dev)); 4993d0407baSopenharmony_ciextern int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget); 5003d0407baSopenharmony_ciextern void usb_del_gadget_udc(struct usb_gadget *gadget); 5013d0407baSopenharmony_ciextern char *usb_get_gadget_udc_name(void); 5023d0407baSopenharmony_ci 5033d0407baSopenharmony_ci/* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */ 5043d0407baSopenharmony_ci#define gadget_for_each_ep(tmp, gadget) \ 5053d0407baSopenharmony_ci list_for_each_entry(tmp, &(gadget)->ep_list, ep_list) 5063d0407baSopenharmony_ci 5073d0407baSopenharmony_ci/** 5083d0407baSopenharmony_ci * usb_ep_align - returns @len aligned to ep's maxpacketsize. 5093d0407baSopenharmony_ci * @ep: the endpoint whose maxpacketsize is used to align @len 5103d0407baSopenharmony_ci * @len: buffer size's length to align to @ep's maxpacketsize 5113d0407baSopenharmony_ci * 5123d0407baSopenharmony_ci * This helper is used to align buffer's size to an ep's maxpacketsize. 5133d0407baSopenharmony_ci */ 5143d0407baSopenharmony_cistatic inline size_t usb_ep_align(struct usb_ep *ep, size_t len) 5153d0407baSopenharmony_ci{ 5163d0407baSopenharmony_ci int max_packet_size = (size_t)usb_endpoint_maxp(ep->desc) & 0x7ff; 5173d0407baSopenharmony_ci 5183d0407baSopenharmony_ci return round_up(len, max_packet_size); 5193d0407baSopenharmony_ci} 5203d0407baSopenharmony_ci 5213d0407baSopenharmony_ci/** 5223d0407baSopenharmony_ci * usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget 5233d0407baSopenharmony_ci * requires quirk_ep_out_aligned_size, otherwise returns len. 5243d0407baSopenharmony_ci * @g: controller to check for quirk 5253d0407baSopenharmony_ci * @ep: the endpoint whose maxpacketsize is used to align @len 5263d0407baSopenharmony_ci * @len: buffer size's length to align to @ep's maxpacketsize 5273d0407baSopenharmony_ci * 5283d0407baSopenharmony_ci * This helper is used in case it's required for any reason to check and maybe 5293d0407baSopenharmony_ci * align buffer's size to an ep's maxpacketsize. 5303d0407baSopenharmony_ci */ 5313d0407baSopenharmony_cistatic inline size_t 5323d0407baSopenharmony_ciusb_ep_align_maybe(struct usb_gadget *g, struct usb_ep *ep, size_t len) 5333d0407baSopenharmony_ci{ 5343d0407baSopenharmony_ci return g->quirk_ep_out_aligned_size ? usb_ep_align(ep, len) : len; 5353d0407baSopenharmony_ci} 5363d0407baSopenharmony_ci 5373d0407baSopenharmony_ci/** 5383d0407baSopenharmony_ci * gadget_is_altset_supported - return true iff the hardware supports 5393d0407baSopenharmony_ci * altsettings 5403d0407baSopenharmony_ci * @g: controller to check for quirk 5413d0407baSopenharmony_ci */ 5423d0407baSopenharmony_cistatic inline int gadget_is_altset_supported(struct usb_gadget *g) 5433d0407baSopenharmony_ci{ 5443d0407baSopenharmony_ci return !g->quirk_altset_not_supp; 5453d0407baSopenharmony_ci} 5463d0407baSopenharmony_ci 5473d0407baSopenharmony_ci/** 5483d0407baSopenharmony_ci * gadget_is_stall_supported - return true iff the hardware supports stalling 5493d0407baSopenharmony_ci * @g: controller to check for quirk 5503d0407baSopenharmony_ci */ 5513d0407baSopenharmony_cistatic inline int gadget_is_stall_supported(struct usb_gadget *g) 5523d0407baSopenharmony_ci{ 5533d0407baSopenharmony_ci return !g->quirk_stall_not_supp; 5543d0407baSopenharmony_ci} 5553d0407baSopenharmony_ci 5563d0407baSopenharmony_ci/** 5573d0407baSopenharmony_ci * gadget_is_zlp_supported - return true iff the hardware supports zlp 5583d0407baSopenharmony_ci * @g: controller to check for quirk 5593d0407baSopenharmony_ci */ 5603d0407baSopenharmony_cistatic inline int gadget_is_zlp_supported(struct usb_gadget *g) 5613d0407baSopenharmony_ci{ 5623d0407baSopenharmony_ci return !g->quirk_zlp_not_supp; 5633d0407baSopenharmony_ci} 5643d0407baSopenharmony_ci 5653d0407baSopenharmony_ci/** 5663d0407baSopenharmony_ci * gadget_avoids_skb_reserve - return true iff the hardware would like to avoid 5673d0407baSopenharmony_ci * skb_reserve to improve performance. 5683d0407baSopenharmony_ci * @g: controller to check for quirk 5693d0407baSopenharmony_ci */ 5703d0407baSopenharmony_cistatic inline int gadget_avoids_skb_reserve(struct usb_gadget *g) 5713d0407baSopenharmony_ci{ 5723d0407baSopenharmony_ci return g->quirk_avoids_skb_reserve; 5733d0407baSopenharmony_ci} 5743d0407baSopenharmony_ci 5753d0407baSopenharmony_ci/** 5763d0407baSopenharmony_ci * gadget_is_dualspeed - return true iff the hardware handles high speed 5773d0407baSopenharmony_ci * @g: controller that might support both high and full speeds 5783d0407baSopenharmony_ci */ 5793d0407baSopenharmony_cistatic inline int gadget_is_dualspeed(struct usb_gadget *g) 5803d0407baSopenharmony_ci{ 5813d0407baSopenharmony_ci return g->max_speed >= USB_SPEED_HIGH; 5823d0407baSopenharmony_ci} 5833d0407baSopenharmony_ci 5843d0407baSopenharmony_ci/** 5853d0407baSopenharmony_ci * gadget_is_superspeed() - return true if the hardware handles superspeed 5863d0407baSopenharmony_ci * @g: controller that might support superspeed 5873d0407baSopenharmony_ci */ 5883d0407baSopenharmony_cistatic inline int gadget_is_superspeed(struct usb_gadget *g) 5893d0407baSopenharmony_ci{ 5903d0407baSopenharmony_ci return g->max_speed >= USB_SPEED_SUPER; 5913d0407baSopenharmony_ci} 5923d0407baSopenharmony_ci 5933d0407baSopenharmony_ci/** 5943d0407baSopenharmony_ci * gadget_is_superspeed_plus() - return true if the hardware handles 5953d0407baSopenharmony_ci * superspeed plus 5963d0407baSopenharmony_ci * @g: controller that might support superspeed plus 5973d0407baSopenharmony_ci */ 5983d0407baSopenharmony_cistatic inline int gadget_is_superspeed_plus(struct usb_gadget *g) 5993d0407baSopenharmony_ci{ 6003d0407baSopenharmony_ci return g->max_speed >= USB_SPEED_SUPER_PLUS; 6013d0407baSopenharmony_ci} 6023d0407baSopenharmony_ci 6033d0407baSopenharmony_ci/** 6043d0407baSopenharmony_ci * gadget_is_otg - return true iff the hardware is OTG-ready 6053d0407baSopenharmony_ci * @g: controller that might have a Mini-AB connector 6063d0407baSopenharmony_ci * 6073d0407baSopenharmony_ci * This is a runtime test, since kernels with a USB-OTG stack sometimes 6083d0407baSopenharmony_ci * run on boards which only have a Mini-B (or Mini-A) connector. 6093d0407baSopenharmony_ci */ 6103d0407baSopenharmony_cistatic inline int gadget_is_otg(struct usb_gadget *g) 6113d0407baSopenharmony_ci{ 6123d0407baSopenharmony_ci#ifdef CONFIG_USB_OTG 6133d0407baSopenharmony_ci return g->is_otg; 6143d0407baSopenharmony_ci#else 6153d0407baSopenharmony_ci return 0; 6163d0407baSopenharmony_ci#endif 6173d0407baSopenharmony_ci} 6183d0407baSopenharmony_ci 6193d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 6203d0407baSopenharmony_ci 6213d0407baSopenharmony_ci#if IS_ENABLED(CONFIG_USB_GADGET) 6223d0407baSopenharmony_ciint usb_gadget_frame_number(struct usb_gadget *gadget); 6233d0407baSopenharmony_ciint usb_gadget_wakeup(struct usb_gadget *gadget); 6243d0407baSopenharmony_ciint usb_gadget_set_selfpowered(struct usb_gadget *gadget); 6253d0407baSopenharmony_ciint usb_gadget_clear_selfpowered(struct usb_gadget *gadget); 6263d0407baSopenharmony_ciint usb_gadget_vbus_connect(struct usb_gadget *gadget); 6273d0407baSopenharmony_ciint usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA); 6283d0407baSopenharmony_ciint usb_gadget_vbus_disconnect(struct usb_gadget *gadget); 6293d0407baSopenharmony_ciint usb_gadget_connect(struct usb_gadget *gadget); 6303d0407baSopenharmony_ciint usb_gadget_disconnect(struct usb_gadget *gadget); 6313d0407baSopenharmony_ciint usb_gadget_deactivate(struct usb_gadget *gadget); 6323d0407baSopenharmony_ciint usb_gadget_activate(struct usb_gadget *gadget); 6333d0407baSopenharmony_ciint usb_gadget_check_config(struct usb_gadget *gadget); 6343d0407baSopenharmony_ci#else 6353d0407baSopenharmony_cistatic inline int usb_gadget_frame_number(struct usb_gadget *gadget) 6363d0407baSopenharmony_ci{ return 0; } 6373d0407baSopenharmony_cistatic inline int usb_gadget_wakeup(struct usb_gadget *gadget) 6383d0407baSopenharmony_ci{ return 0; } 6393d0407baSopenharmony_cistatic inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget) 6403d0407baSopenharmony_ci{ return 0; } 6413d0407baSopenharmony_cistatic inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget) 6423d0407baSopenharmony_ci{ return 0; } 6433d0407baSopenharmony_cistatic inline int usb_gadget_vbus_connect(struct usb_gadget *gadget) 6443d0407baSopenharmony_ci{ return 0; } 6453d0407baSopenharmony_cistatic inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 6463d0407baSopenharmony_ci{ return 0; } 6473d0407baSopenharmony_cistatic inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget) 6483d0407baSopenharmony_ci{ return 0; } 6493d0407baSopenharmony_cistatic inline int usb_gadget_connect(struct usb_gadget *gadget) 6503d0407baSopenharmony_ci{ return 0; } 6513d0407baSopenharmony_cistatic inline int usb_gadget_disconnect(struct usb_gadget *gadget) 6523d0407baSopenharmony_ci{ return 0; } 6533d0407baSopenharmony_cistatic inline int usb_gadget_deactivate(struct usb_gadget *gadget) 6543d0407baSopenharmony_ci{ return 0; } 6553d0407baSopenharmony_cistatic inline int usb_gadget_activate(struct usb_gadget *gadget) 6563d0407baSopenharmony_ci{ return 0; } 6573d0407baSopenharmony_cistatic inline int usb_gadget_check_config(struct usb_gadget *gadget) 6583d0407baSopenharmony_ci{ return 0; } 6593d0407baSopenharmony_ci#endif /* CONFIG_USB_GADGET */ 6603d0407baSopenharmony_ci 6613d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 6623d0407baSopenharmony_ci 6633d0407baSopenharmony_ci/** 6643d0407baSopenharmony_ci * struct usb_gadget_driver - driver for usb gadget devices 6653d0407baSopenharmony_ci * @function: String describing the gadget's function 6663d0407baSopenharmony_ci * @max_speed: Highest speed the driver handles. 6673d0407baSopenharmony_ci * @setup: Invoked for ep0 control requests that aren't handled by 6683d0407baSopenharmony_ci * the hardware level driver. Most calls must be handled by 6693d0407baSopenharmony_ci * the gadget driver, including descriptor and configuration 6703d0407baSopenharmony_ci * management. The 16 bit members of the setup data are in 6713d0407baSopenharmony_ci * USB byte order. Called in_interrupt; this may not sleep. Driver 6723d0407baSopenharmony_ci * queues a response to ep0, or returns negative to stall. 6733d0407baSopenharmony_ci * @disconnect: Invoked after all transfers have been stopped, 6743d0407baSopenharmony_ci * when the host is disconnected. May be called in_interrupt; this 6753d0407baSopenharmony_ci * may not sleep. Some devices can't detect disconnect, so this might 6763d0407baSopenharmony_ci * not be called except as part of controller shutdown. 6773d0407baSopenharmony_ci * @bind: the driver's bind callback 6783d0407baSopenharmony_ci * @unbind: Invoked when the driver is unbound from a gadget, 6793d0407baSopenharmony_ci * usually from rmmod (after a disconnect is reported). 6803d0407baSopenharmony_ci * Called in a context that permits sleeping. 6813d0407baSopenharmony_ci * @suspend: Invoked on USB suspend. May be called in_interrupt. 6823d0407baSopenharmony_ci * @resume: Invoked on USB resume. May be called in_interrupt. 6833d0407baSopenharmony_ci * @reset: Invoked on USB bus reset. It is mandatory for all gadget drivers 6843d0407baSopenharmony_ci * and should be called in_interrupt. 6853d0407baSopenharmony_ci * @driver: Driver model state for this driver. 6863d0407baSopenharmony_ci * @udc_name: A name of UDC this driver should be bound to. If udc_name is NULL, 6873d0407baSopenharmony_ci * this driver will be bound to any available UDC. 6883d0407baSopenharmony_ci * @pending: UDC core private data used for deferred probe of this driver. 6893d0407baSopenharmony_ci * @match_existing_only: If udc is not found, return an error and don't add this 6903d0407baSopenharmony_ci * gadget driver to list of pending driver 6913d0407baSopenharmony_ci * 6923d0407baSopenharmony_ci * Devices are disabled till a gadget driver successfully bind()s, which 6933d0407baSopenharmony_ci * means the driver will handle setup() requests needed to enumerate (and 6943d0407baSopenharmony_ci * meet "chapter 9" requirements) then do some useful work. 6953d0407baSopenharmony_ci * 6963d0407baSopenharmony_ci * If gadget->is_otg is true, the gadget driver must provide an OTG 6973d0407baSopenharmony_ci * descriptor during enumeration, or else fail the bind() call. In such 6983d0407baSopenharmony_ci * cases, no USB traffic may flow until both bind() returns without 6993d0407baSopenharmony_ci * having called usb_gadget_disconnect(), and the USB host stack has 7003d0407baSopenharmony_ci * initialized. 7013d0407baSopenharmony_ci * 7023d0407baSopenharmony_ci * Drivers use hardware-specific knowledge to configure the usb hardware. 7033d0407baSopenharmony_ci * endpoint addressing is only one of several hardware characteristics that 7043d0407baSopenharmony_ci * are in descriptors the ep0 implementation returns from setup() calls. 7053d0407baSopenharmony_ci * 7063d0407baSopenharmony_ci * Except for ep0 implementation, most driver code shouldn't need change to 7073d0407baSopenharmony_ci * run on top of different usb controllers. It'll use endpoints set up by 7083d0407baSopenharmony_ci * that ep0 implementation. 7093d0407baSopenharmony_ci * 7103d0407baSopenharmony_ci * The usb controller driver handles a few standard usb requests. Those 7113d0407baSopenharmony_ci * include set_address, and feature flags for devices, interfaces, and 7123d0407baSopenharmony_ci * endpoints (the get_status, set_feature, and clear_feature requests). 7133d0407baSopenharmony_ci * 7143d0407baSopenharmony_ci * Accordingly, the driver's setup() callback must always implement all 7153d0407baSopenharmony_ci * get_descriptor requests, returning at least a device descriptor and 7163d0407baSopenharmony_ci * a configuration descriptor. Drivers must make sure the endpoint 7173d0407baSopenharmony_ci * descriptors match any hardware constraints. Some hardware also constrains 7183d0407baSopenharmony_ci * other descriptors. (The pxa250 allows only configurations 1, 2, or 3). 7193d0407baSopenharmony_ci * 7203d0407baSopenharmony_ci * The driver's setup() callback must also implement set_configuration, 7213d0407baSopenharmony_ci * and should also implement set_interface, get_configuration, and 7223d0407baSopenharmony_ci * get_interface. Setting a configuration (or interface) is where 7233d0407baSopenharmony_ci * endpoints should be activated or (config 0) shut down. 7243d0407baSopenharmony_ci * 7253d0407baSopenharmony_ci * (Note that only the default control endpoint is supported. Neither 7263d0407baSopenharmony_ci * hosts nor devices generally support control traffic except to ep0.) 7273d0407baSopenharmony_ci * 7283d0407baSopenharmony_ci * Most devices will ignore USB suspend/resume operations, and so will 7293d0407baSopenharmony_ci * not provide those callbacks. However, some may need to change modes 7303d0407baSopenharmony_ci * when the host is not longer directing those activities. For example, 7313d0407baSopenharmony_ci * local controls (buttons, dials, etc) may need to be re-enabled since 7323d0407baSopenharmony_ci * the (remote) host can't do that any longer; or an error state might 7333d0407baSopenharmony_ci * be cleared, to make the device behave identically whether or not 7343d0407baSopenharmony_ci * power is maintained. 7353d0407baSopenharmony_ci */ 7363d0407baSopenharmony_cistruct usb_gadget_driver { 7373d0407baSopenharmony_ci char *function; 7383d0407baSopenharmony_ci enum usb_device_speed max_speed; 7393d0407baSopenharmony_ci int (*bind)(struct usb_gadget *gadget, 7403d0407baSopenharmony_ci struct usb_gadget_driver *driver); 7413d0407baSopenharmony_ci void (*unbind)(struct usb_gadget *); 7423d0407baSopenharmony_ci int (*setup)(struct usb_gadget *, 7433d0407baSopenharmony_ci const struct usb_ctrlrequest *); 7443d0407baSopenharmony_ci void (*disconnect)(struct usb_gadget *); 7453d0407baSopenharmony_ci void (*suspend)(struct usb_gadget *); 7463d0407baSopenharmony_ci void (*resume)(struct usb_gadget *); 7473d0407baSopenharmony_ci void (*reset)(struct usb_gadget *); 7483d0407baSopenharmony_ci 7493d0407baSopenharmony_ci /* FIXME support safe rmmod */ 7503d0407baSopenharmony_ci struct device_driver driver; 7513d0407baSopenharmony_ci 7523d0407baSopenharmony_ci char *udc_name; 7533d0407baSopenharmony_ci struct list_head pending; 7543d0407baSopenharmony_ci unsigned match_existing_only:1; 7553d0407baSopenharmony_ci}; 7563d0407baSopenharmony_ci 7573d0407baSopenharmony_ci 7583d0407baSopenharmony_ci 7593d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 7603d0407baSopenharmony_ci 7613d0407baSopenharmony_ci/* driver modules register and unregister, as usual. 7623d0407baSopenharmony_ci * these calls must be made in a context that can sleep. 7633d0407baSopenharmony_ci * 7643d0407baSopenharmony_ci * these will usually be implemented directly by the hardware-dependent 7653d0407baSopenharmony_ci * usb bus interface driver, which will only support a single driver. 7663d0407baSopenharmony_ci */ 7673d0407baSopenharmony_ci 7683d0407baSopenharmony_ci/** 7693d0407baSopenharmony_ci * usb_gadget_probe_driver - probe a gadget driver 7703d0407baSopenharmony_ci * @driver: the driver being registered 7713d0407baSopenharmony_ci * Context: can sleep 7723d0407baSopenharmony_ci * 7733d0407baSopenharmony_ci * Call this in your gadget driver's module initialization function, 7743d0407baSopenharmony_ci * to tell the underlying usb controller driver about your driver. 7753d0407baSopenharmony_ci * The @bind() function will be called to bind it to a gadget before this 7763d0407baSopenharmony_ci * registration call returns. It's expected that the @bind() function will 7773d0407baSopenharmony_ci * be in init sections. 7783d0407baSopenharmony_ci */ 7793d0407baSopenharmony_ciint usb_gadget_probe_driver(struct usb_gadget_driver *driver); 7803d0407baSopenharmony_ci 7813d0407baSopenharmony_ci/** 7823d0407baSopenharmony_ci * usb_gadget_unregister_driver - unregister a gadget driver 7833d0407baSopenharmony_ci * @driver:the driver being unregistered 7843d0407baSopenharmony_ci * Context: can sleep 7853d0407baSopenharmony_ci * 7863d0407baSopenharmony_ci * Call this in your gadget driver's module cleanup function, 7873d0407baSopenharmony_ci * to tell the underlying usb controller that your driver is 7883d0407baSopenharmony_ci * going away. If the controller is connected to a USB host, 7893d0407baSopenharmony_ci * it will first disconnect(). The driver is also requested 7903d0407baSopenharmony_ci * to unbind() and clean up any device state, before this procedure 7913d0407baSopenharmony_ci * finally returns. It's expected that the unbind() functions 7923d0407baSopenharmony_ci * will be in exit sections, so may not be linked in some kernels. 7933d0407baSopenharmony_ci */ 7943d0407baSopenharmony_ciint usb_gadget_unregister_driver(struct usb_gadget_driver *driver); 7953d0407baSopenharmony_ci 7963d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 7973d0407baSopenharmony_ci 7983d0407baSopenharmony_ci/* utility to simplify dealing with string descriptors */ 7993d0407baSopenharmony_ci 8003d0407baSopenharmony_ci/** 8013d0407baSopenharmony_ci * struct usb_string - wraps a C string and its USB id 8023d0407baSopenharmony_ci * @id:the (nonzero) ID for this string 8033d0407baSopenharmony_ci * @s:the string, in UTF-8 encoding 8043d0407baSopenharmony_ci * 8053d0407baSopenharmony_ci * If you're using usb_gadget_get_string(), use this to wrap a string 8063d0407baSopenharmony_ci * together with its ID. 8073d0407baSopenharmony_ci */ 8083d0407baSopenharmony_cistruct usb_string { 8093d0407baSopenharmony_ci u8 id; 8103d0407baSopenharmony_ci const char *s; 8113d0407baSopenharmony_ci}; 8123d0407baSopenharmony_ci 8133d0407baSopenharmony_ci/** 8143d0407baSopenharmony_ci * struct usb_gadget_strings - a set of USB strings in a given language 8153d0407baSopenharmony_ci * @language:identifies the strings' language (0x0409 for en-us) 8163d0407baSopenharmony_ci * @strings:array of strings with their ids 8173d0407baSopenharmony_ci * 8183d0407baSopenharmony_ci * If you're using usb_gadget_get_string(), use this to wrap all the 8193d0407baSopenharmony_ci * strings for a given language. 8203d0407baSopenharmony_ci */ 8213d0407baSopenharmony_cistruct usb_gadget_strings { 8223d0407baSopenharmony_ci u16 language; /* 0x0409 for en-us */ 8233d0407baSopenharmony_ci struct usb_string *strings; 8243d0407baSopenharmony_ci}; 8253d0407baSopenharmony_ci 8263d0407baSopenharmony_cistruct usb_gadget_string_container { 8273d0407baSopenharmony_ci struct list_head list; 8283d0407baSopenharmony_ci u8 *stash[]; 8293d0407baSopenharmony_ci}; 8303d0407baSopenharmony_ci 8313d0407baSopenharmony_ci/* put descriptor for string with that id into buf (buflen >= 256) */ 8323d0407baSopenharmony_ciint usb_gadget_get_string(const struct usb_gadget_strings *table, int id, u8 *buf); 8333d0407baSopenharmony_ci 8343d0407baSopenharmony_ci/* check if the given language identifier is valid */ 8353d0407baSopenharmony_cibool usb_validate_langid(u16 langid); 8363d0407baSopenharmony_ci 8373d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 8383d0407baSopenharmony_ci 8393d0407baSopenharmony_ci/* utility to simplify managing config descriptors */ 8403d0407baSopenharmony_ci 8413d0407baSopenharmony_ci/* write vector of descriptors into buffer */ 8423d0407baSopenharmony_ciint usb_descriptor_fillbuf(void *, unsigned, 8433d0407baSopenharmony_ci const struct usb_descriptor_header **); 8443d0407baSopenharmony_ci 8453d0407baSopenharmony_ci/* build config descriptor from single descriptor vector */ 8463d0407baSopenharmony_ciint usb_gadget_config_buf(const struct usb_config_descriptor *config, 8473d0407baSopenharmony_ci void *buf, unsigned buflen, const struct usb_descriptor_header **desc); 8483d0407baSopenharmony_ci 8493d0407baSopenharmony_ci/* copy a NULL-terminated vector of descriptors */ 8503d0407baSopenharmony_cistruct usb_descriptor_header **usb_copy_descriptors( 8513d0407baSopenharmony_ci struct usb_descriptor_header **); 8523d0407baSopenharmony_ci 8533d0407baSopenharmony_ci/** 8543d0407baSopenharmony_ci * usb_free_descriptors - free descriptors returned by usb_copy_descriptors() 8553d0407baSopenharmony_ci * @v: vector of descriptors 8563d0407baSopenharmony_ci */ 8573d0407baSopenharmony_cistatic inline void usb_free_descriptors(struct usb_descriptor_header **v) 8583d0407baSopenharmony_ci{ 8593d0407baSopenharmony_ci kfree(v); 8603d0407baSopenharmony_ci} 8613d0407baSopenharmony_ci 8623d0407baSopenharmony_cistruct usb_function; 8633d0407baSopenharmony_ciint usb_assign_descriptors(struct usb_function *f, 8643d0407baSopenharmony_ci struct usb_descriptor_header **fs, 8653d0407baSopenharmony_ci struct usb_descriptor_header **hs, 8663d0407baSopenharmony_ci struct usb_descriptor_header **ss, 8673d0407baSopenharmony_ci struct usb_descriptor_header **ssp); 8683d0407baSopenharmony_civoid usb_free_all_descriptors(struct usb_function *f); 8693d0407baSopenharmony_ci 8703d0407baSopenharmony_cistruct usb_descriptor_header *usb_otg_descriptor_alloc( 8713d0407baSopenharmony_ci struct usb_gadget *gadget); 8723d0407baSopenharmony_ciint usb_otg_descriptor_init(struct usb_gadget *gadget, 8733d0407baSopenharmony_ci struct usb_descriptor_header *otg_desc); 8743d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 8753d0407baSopenharmony_ci 8763d0407baSopenharmony_ci/* utility to simplify map/unmap of usb_requests to/from DMA */ 8773d0407baSopenharmony_ci 8783d0407baSopenharmony_ci#ifdef CONFIG_HAS_DMA 8793d0407baSopenharmony_ciextern int usb_gadget_map_request_by_dev(struct device *dev, 8803d0407baSopenharmony_ci struct usb_request *req, int is_in); 8813d0407baSopenharmony_ciextern int usb_gadget_map_request(struct usb_gadget *gadget, 8823d0407baSopenharmony_ci struct usb_request *req, int is_in); 8833d0407baSopenharmony_ci 8843d0407baSopenharmony_ciextern void usb_gadget_unmap_request_by_dev(struct device *dev, 8853d0407baSopenharmony_ci struct usb_request *req, int is_in); 8863d0407baSopenharmony_ciextern void usb_gadget_unmap_request(struct usb_gadget *gadget, 8873d0407baSopenharmony_ci struct usb_request *req, int is_in); 8883d0407baSopenharmony_ci#else /* !CONFIG_HAS_DMA */ 8893d0407baSopenharmony_cistatic inline int usb_gadget_map_request_by_dev(struct device *dev, 8903d0407baSopenharmony_ci struct usb_request *req, int is_in) { return -ENOSYS; } 8913d0407baSopenharmony_cistatic inline int usb_gadget_map_request(struct usb_gadget *gadget, 8923d0407baSopenharmony_ci struct usb_request *req, int is_in) { return -ENOSYS; } 8933d0407baSopenharmony_ci 8943d0407baSopenharmony_cistatic inline void usb_gadget_unmap_request_by_dev(struct device *dev, 8953d0407baSopenharmony_ci struct usb_request *req, int is_in) { } 8963d0407baSopenharmony_cistatic inline void usb_gadget_unmap_request(struct usb_gadget *gadget, 8973d0407baSopenharmony_ci struct usb_request *req, int is_in) { } 8983d0407baSopenharmony_ci#endif /* !CONFIG_HAS_DMA */ 8993d0407baSopenharmony_ci 9003d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 9013d0407baSopenharmony_ci 9023d0407baSopenharmony_ci/* utility to set gadget state properly */ 9033d0407baSopenharmony_ci 9043d0407baSopenharmony_ciextern void usb_gadget_set_state(struct usb_gadget *gadget, 9053d0407baSopenharmony_ci enum usb_device_state state); 9063d0407baSopenharmony_ci 9073d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 9083d0407baSopenharmony_ci 9093d0407baSopenharmony_ci/* utility to tell udc core that the bus reset occurs */ 9103d0407baSopenharmony_ciextern void usb_gadget_udc_reset(struct usb_gadget *gadget, 9113d0407baSopenharmony_ci struct usb_gadget_driver *driver); 9123d0407baSopenharmony_ci 9133d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 9143d0407baSopenharmony_ci 9153d0407baSopenharmony_ci/* utility to give requests back to the gadget layer */ 9163d0407baSopenharmony_ci 9173d0407baSopenharmony_ciextern void usb_gadget_giveback_request(struct usb_ep *ep, 9183d0407baSopenharmony_ci struct usb_request *req); 9193d0407baSopenharmony_ci 9203d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 9213d0407baSopenharmony_ci 9223d0407baSopenharmony_ci/* utility to find endpoint by name */ 9233d0407baSopenharmony_ci 9243d0407baSopenharmony_ciextern struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, 9253d0407baSopenharmony_ci const char *name); 9263d0407baSopenharmony_ci 9273d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 9283d0407baSopenharmony_ci 9293d0407baSopenharmony_ci/* utility to check if endpoint caps match descriptor needs */ 9303d0407baSopenharmony_ci 9313d0407baSopenharmony_ciextern int usb_gadget_ep_match_desc(struct usb_gadget *gadget, 9323d0407baSopenharmony_ci struct usb_ep *ep, struct usb_endpoint_descriptor *desc, 9333d0407baSopenharmony_ci struct usb_ss_ep_comp_descriptor *ep_comp); 9343d0407baSopenharmony_ci 9353d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 9363d0407baSopenharmony_ci 9373d0407baSopenharmony_ci/* utility to update vbus status for udc core, it may be scheduled */ 9383d0407baSopenharmony_ciextern void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status); 9393d0407baSopenharmony_ci 9403d0407baSopenharmony_ci/*-------------------------------------------------------------------------*/ 9413d0407baSopenharmony_ci 9423d0407baSopenharmony_ci/* utility wrapping a simple endpoint selection policy */ 9433d0407baSopenharmony_ci 9443d0407baSopenharmony_ciextern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *, 9453d0407baSopenharmony_ci struct usb_endpoint_descriptor *); 9463d0407baSopenharmony_ci 9473d0407baSopenharmony_ci 9483d0407baSopenharmony_ciextern struct usb_ep *usb_ep_autoconfig_ss(struct usb_gadget *, 9493d0407baSopenharmony_ci struct usb_endpoint_descriptor *, 9503d0407baSopenharmony_ci struct usb_ss_ep_comp_descriptor *); 9513d0407baSopenharmony_ci 9523d0407baSopenharmony_ciextern void usb_ep_autoconfig_release(struct usb_ep *); 9533d0407baSopenharmony_ci 9543d0407baSopenharmony_ciextern void usb_ep_autoconfig_reset(struct usb_gadget *); 9553d0407baSopenharmony_ci 9563d0407baSopenharmony_ci#endif /* __LINUX_USB_GADGET_H */ 957