183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */ 2012771d8Swdenk /* 3012771d8Swdenk * (C) Copyright 2001 4012771d8Swdenk * Denis Peter, MPL AG Switzerland 5012771d8Swdenk * 6de31213fSSimon Glass * Adapted for U-Boot driver model 7de31213fSSimon Glass * (C) Copyright 2015 Google, Inc 8012771d8Swdenk * Note: Part of this code has been derived from linux 9012771d8Swdenk * 10012771d8Swdenk */ 11012771d8Swdenk #ifndef _USB_H_ 12012771d8Swdenk #define _USB_H_ 13012771d8Swdenk 14de31213fSSimon Glass #include <fdtdec.h> 15012771d8Swdenk #include <usb_defs.h> 16c60795f4SIlya Yanok #include <linux/usb/ch9.h> 17a8c2ebcfSMasahiro Yamada #include <asm/cache.h> 18a8c2ebcfSMasahiro Yamada #include <part.h> 19012771d8Swdenk 2071c5de4fSTom Rini /* 2171c5de4fSTom Rini * The EHCI spec says that we must align to at least 32 bytes. However, 2271c5de4fSTom Rini * some platforms require larger alignment. 2371c5de4fSTom Rini */ 2471c5de4fSTom Rini #if ARCH_DMA_MINALIGN > 32 2571c5de4fSTom Rini #define USB_DMA_MINALIGN ARCH_DMA_MINALIGN 2671c5de4fSTom Rini #else 2771c5de4fSTom Rini #define USB_DMA_MINALIGN 32 2871c5de4fSTom Rini #endif 2971c5de4fSTom Rini 30012771d8Swdenk /* Everything is aribtrary */ 31012771d8Swdenk #define USB_ALTSETTINGALLOC 4 32012771d8Swdenk #define USB_MAXALTSETTING 128 /* Hard limit */ 33012771d8Swdenk 34012771d8Swdenk #define USB_MAX_DEVICE 32 35012771d8Swdenk #define USB_MAXCONFIG 8 36012771d8Swdenk #define USB_MAXINTERFACES 8 37012771d8Swdenk #define USB_MAXENDPOINTS 16 38012771d8Swdenk #define USB_MAXCHILDREN 8 /* This is arbitrary */ 39012771d8Swdenk #define USB_MAX_HUB 16 40012771d8Swdenk 41012771d8Swdenk #define USB_CNTL_TIMEOUT 100 /* 100ms timeout */ 42012771d8Swdenk 4396820a35SSimon Glass /* 4496820a35SSimon Glass * This is the timeout to allow for submitting an urb in ms. We allow more 4596820a35SSimon Glass * time for a BULK device to react - some are slow. 4696820a35SSimon Glass */ 4780b350a7SJason Cooper #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000) 4896820a35SSimon Glass 49012771d8Swdenk /* device request (setup) */ 50012771d8Swdenk struct devrequest { 51b12242acSSergey Temerkhanov __u8 requesttype; 52b12242acSSergey Temerkhanov __u8 request; 53b12242acSSergey Temerkhanov __le16 value; 54b12242acSSergey Temerkhanov __le16 index; 55b12242acSSergey Temerkhanov __le16 length; 56012771d8Swdenk } __attribute__ ((packed)); 57012771d8Swdenk 588f8bd565STom Rix /* Interface */ 598f8bd565STom Rix struct usb_interface { 608f8bd565STom Rix struct usb_interface_descriptor desc; 61012771d8Swdenk 62b12242acSSergey Temerkhanov __u8 no_of_ep; 63b12242acSSergey Temerkhanov __u8 num_altsetting; 64b12242acSSergey Temerkhanov __u8 act_altsetting; 65de39f8c1SMichael Trimarchi 66012771d8Swdenk struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS]; 676497c667SVivek Gautam /* 686497c667SVivek Gautam * Super Speed Device will have Super Speed Endpoint 696497c667SVivek Gautam * Companion Descriptor (section 9.6.7 of usb 3.0 spec) 706497c667SVivek Gautam * Revision 1.0 June 6th 2011 716497c667SVivek Gautam */ 726497c667SVivek Gautam struct usb_ss_ep_comp_descriptor ss_ep_comp_desc[USB_MAXENDPOINTS]; 73012771d8Swdenk } __attribute__ ((packed)); 74012771d8Swdenk 758f8bd565STom Rix /* Configuration information.. */ 768f8bd565STom Rix struct usb_config { 77c60795f4SIlya Yanok struct usb_config_descriptor desc; 78012771d8Swdenk 79b12242acSSergey Temerkhanov __u8 no_of_if; /* number of interfaces */ 808f8bd565STom Rix struct usb_interface if_desc[USB_MAXINTERFACES]; 81012771d8Swdenk } __attribute__ ((packed)); 82012771d8Swdenk 8348867208SRemy Bohmer enum { 8448867208SRemy Bohmer /* Maximum packet size; encoded as 0,1,2,3 = 8,16,32,64 */ 8548867208SRemy Bohmer PACKET_SIZE_8 = 0, 8648867208SRemy Bohmer PACKET_SIZE_16 = 1, 8748867208SRemy Bohmer PACKET_SIZE_32 = 2, 8848867208SRemy Bohmer PACKET_SIZE_64 = 3, 8948867208SRemy Bohmer }; 90012771d8Swdenk 91de31213fSSimon Glass /** 92de31213fSSimon Glass * struct usb_device - information about a USB device 93de31213fSSimon Glass * 94de31213fSSimon Glass * With driver model both UCLASS_USB (the USB controllers) and UCLASS_USB_HUB 95de31213fSSimon Glass * (the hubs) have this as parent data. Hubs are children of controllers or 96de31213fSSimon Glass * other hubs and there is always a single root hub for each controller. 97de31213fSSimon Glass * Therefore struct usb_device can always be accessed with 98bcbe3d15SSimon Glass * dev_get_parent_priv(dev), where dev is a USB device. 99de31213fSSimon Glass * 100de31213fSSimon Glass * Pointers exist for obtaining both the device (could be any uclass) and 101de31213fSSimon Glass * controller (UCLASS_USB) from this structure. The controller does not have 102de31213fSSimon Glass * a struct usb_device since it is not a device. 103de31213fSSimon Glass */ 104012771d8Swdenk struct usb_device { 105012771d8Swdenk int devnum; /* Device number on USB bus */ 1063e126484SMichael Trimarchi int speed; /* full/low/high */ 107012771d8Swdenk char mf[32]; /* manufacturer */ 108012771d8Swdenk char prod[32]; /* product */ 109012771d8Swdenk char serial[32]; /* serial number */ 110012771d8Swdenk 11148867208SRemy Bohmer /* Maximum packet size; one of: PACKET_SIZE_* */ 11248867208SRemy Bohmer int maxpacketsize; 11348867208SRemy Bohmer /* one bit for each endpoint ([0] = IN, [1] = OUT) */ 11448867208SRemy Bohmer unsigned int toggle[2]; 115de39f8c1SMichael Trimarchi /* endpoint halts; one bit per endpoint # & direction; 116de39f8c1SMichael Trimarchi * [0] = IN, [1] = OUT 117de39f8c1SMichael Trimarchi */ 11848867208SRemy Bohmer unsigned int halted[2]; 119012771d8Swdenk int epmaxpacketin[16]; /* INput endpoint specific maximums */ 120012771d8Swdenk int epmaxpacketout[16]; /* OUTput endpoint specific maximums */ 121012771d8Swdenk 122012771d8Swdenk int configno; /* selected config number */ 123f5766139SPuneet Saxena /* Device Descriptor */ 124f5766139SPuneet Saxena struct usb_device_descriptor descriptor 125f5766139SPuneet Saxena __attribute__((aligned(ARCH_DMA_MINALIGN))); 1268f8bd565STom Rix struct usb_config config; /* config descriptor */ 127012771d8Swdenk 128012771d8Swdenk int have_langid; /* whether string_langid is valid yet */ 129012771d8Swdenk int string_langid; /* language ID for strings */ 130012771d8Swdenk int (*irq_handle)(struct usb_device *dev); 131012771d8Swdenk unsigned long irq_status; 132a6f70a3dSVagrant Cascadian int irq_act_len; /* transferred bytes */ 133012771d8Swdenk void *privptr; 134012771d8Swdenk /* 135012771d8Swdenk * Child devices - if this is a hub device 136012771d8Swdenk * Each instance needs its own set of data structures. 137012771d8Swdenk */ 138012771d8Swdenk unsigned long status; 139904f2a83SHans de Goede unsigned long int_pending; /* 1 bit per ep, used by int_queue */ 140a6f70a3dSVagrant Cascadian int act_len; /* transferred bytes */ 141012771d8Swdenk int maxchild; /* Number of ports if hub */ 142de31213fSSimon Glass int portnr; /* Port number, 1=first */ 143*fd09c205SSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB) 144de31213fSSimon Glass /* parent hub, or NULL if this is the root hub */ 145012771d8Swdenk struct usb_device *parent; 146012771d8Swdenk struct usb_device *children[USB_MAXCHILDREN]; 147c7e3b2b5SLucas Stach void *controller; /* hardware controller private data */ 148de31213fSSimon Glass #endif 1495853e133SVivek Gautam /* slot_id - for xHCI enabled devices */ 1505853e133SVivek Gautam unsigned int slot_id; 151*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB) 152de31213fSSimon Glass struct udevice *dev; /* Pointer to associated device */ 153de31213fSSimon Glass struct udevice *controller_dev; /* Pointer to associated controller */ 154de31213fSSimon Glass #endif 155012771d8Swdenk }; 156012771d8Swdenk 1578460b89aSHans de Goede struct int_queue; 1588460b89aSHans de Goede 159bba67914STroy Kisky /* 160bba67914STroy Kisky * You can initialize platform's USB host or device 161bba67914STroy Kisky * ports by passing this enum as an argument to 162bba67914STroy Kisky * board_usb_init(). 163bba67914STroy Kisky */ 164bba67914STroy Kisky enum usb_init_type { 165bba67914STroy Kisky USB_INIT_HOST, 166bba67914STroy Kisky USB_INIT_DEVICE 167bba67914STroy Kisky }; 168bba67914STroy Kisky 169012771d8Swdenk /********************************************************************** 170012771d8Swdenk * this is how the lowlevel part communicate with the outer world 171012771d8Swdenk */ 172012771d8Swdenk 17306d513ecSTroy Kisky int usb_lowlevel_init(int index, enum usb_init_type init, void **controller); 174c7e3b2b5SLucas Stach int usb_lowlevel_stop(int index); 175de31213fSSimon Glass 176*fd09c205SSven Schwermer #if defined(CONFIG_USB_MUSB_HOST) || CONFIG_IS_ENABLED(DM_USB) 1778802f563SHans de Goede int usb_reset_root_port(struct usb_device *dev); 17890cdc103SHans de Goede #else 1798802f563SHans de Goede #define usb_reset_root_port(dev) 18090cdc103SHans de Goede #endif 181c7e3b2b5SLucas Stach 182de39f8c1SMichael Trimarchi int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, 183de39f8c1SMichael Trimarchi void *buffer, int transfer_len); 184012771d8Swdenk int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, 185012771d8Swdenk int transfer_len, struct devrequest *setup); 186012771d8Swdenk int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer, 187012771d8Swdenk int transfer_len, int interval); 188012771d8Swdenk 1898850c5d5STom Rini #if defined CONFIG_USB_EHCI_HCD || defined CONFIG_USB_MUSB_HOST \ 190*fd09c205SSven Schwermer || CONFIG_IS_ENABLED(DM_USB) 1918460b89aSHans de Goede struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe, 1928bb6c1d1SHans de Goede int queuesize, int elementsize, void *buffer, int interval); 1938460b89aSHans de Goede int destroy_int_queue(struct usb_device *dev, struct int_queue *queue); 1948460b89aSHans de Goede void *poll_int_queue(struct usb_device *dev, struct int_queue *queue); 1958460b89aSHans de Goede #endif 1968460b89aSHans de Goede 197012771d8Swdenk /* Defines */ 198012771d8Swdenk #define USB_UHCI_VEND_ID 0x8086 199012771d8Swdenk #define USB_UHCI_DEV_ID 0x7112 200012771d8Swdenk 201e5f24753SLukasz Dalek /* 202e5f24753SLukasz Dalek * PXA25x can only act as USB device. There are drivers 203e5f24753SLukasz Dalek * which works with USB CDC gadgets implementations. 204e5f24753SLukasz Dalek * Some of them have common routines which can be used 205e5f24753SLukasz Dalek * in boards init functions e.g. udc_disconnect() used for 206e5f24753SLukasz Dalek * forced device disconnection from host. 207e5f24753SLukasz Dalek */ 208e5f24753SLukasz Dalek extern void udc_disconnect(void); 209e5f24753SLukasz Dalek 21016297cfbSMateusz Zalega /* 21116297cfbSMateusz Zalega * board-specific hardware initialization, called by 21216297cfbSMateusz Zalega * usb drivers and u-boot commands 21316297cfbSMateusz Zalega * 21416297cfbSMateusz Zalega * @param index USB controller number 21516297cfbSMateusz Zalega * @param init initializes controller as USB host or device 21616297cfbSMateusz Zalega */ 217bba67914STroy Kisky int board_usb_init(int index, enum usb_init_type init); 21816297cfbSMateusz Zalega 21916297cfbSMateusz Zalega /* 22016297cfbSMateusz Zalega * can be used to clean up after failed USB initialization attempt 22116297cfbSMateusz Zalega * vide: board_usb_init() 22216297cfbSMateusz Zalega * 22316297cfbSMateusz Zalega * @param index USB controller number for selective cleanup 224bba67914STroy Kisky * @param init usb_init_type passed to board_usb_init() 22516297cfbSMateusz Zalega */ 226bba67914STroy Kisky int board_usb_cleanup(int index, enum usb_init_type init); 22716297cfbSMateusz Zalega 228012771d8Swdenk #ifdef CONFIG_USB_STORAGE 229012771d8Swdenk 23070caa971SSimon Glass #define USB_MAX_STOR_DEV 7 231012771d8Swdenk int usb_stor_scan(int mode); 232e813eae3SAnatolij Gustschin int usb_stor_info(void); 233012771d8Swdenk 234012771d8Swdenk #endif 235012771d8Swdenk 23689d48367SSimon Glass #ifdef CONFIG_USB_HOST_ETHER 23789d48367SSimon Glass 23889d48367SSimon Glass #define USB_MAX_ETH_DEV 5 23989d48367SSimon Glass int usb_host_eth_scan(int mode); 24089d48367SSimon Glass 24189d48367SSimon Glass #endif 24289d48367SSimon Glass 243012771d8Swdenk #ifdef CONFIG_USB_KEYBOARD 244012771d8Swdenk 245012771d8Swdenk int drv_usb_kbd_init(void); 2468a8a2257SHans de Goede int usb_kbd_deregister(int force); 247012771d8Swdenk 248012771d8Swdenk #endif 249012771d8Swdenk /* routines */ 250012771d8Swdenk int usb_init(void); /* initialize the USB Controller */ 251012771d8Swdenk int usb_stop(void); /* stop the USB Controller */ 25208f3bb0bSVincent Palatin int usb_detect_change(void); /* detect if a USB device has been (un)plugged */ 253012771d8Swdenk 254012771d8Swdenk 255012771d8Swdenk int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol); 256de39f8c1SMichael Trimarchi int usb_set_idle(struct usb_device *dev, int ifnum, int duration, 257de39f8c1SMichael Trimarchi int report_id); 258012771d8Swdenk int usb_control_msg(struct usb_device *dev, unsigned int pipe, 259012771d8Swdenk unsigned char request, unsigned char requesttype, 260012771d8Swdenk unsigned short value, unsigned short index, 261012771d8Swdenk void *data, unsigned short size, int timeout); 262012771d8Swdenk int usb_bulk_msg(struct usb_device *dev, unsigned int pipe, 263012771d8Swdenk void *data, int len, int *actual_length, int timeout); 264012771d8Swdenk int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe, 265012771d8Swdenk void *buffer, int transfer_len, int interval); 26689d48367SSimon Glass int usb_disable_asynch(int disable); 267012771d8Swdenk int usb_maxpacket(struct usb_device *dev, unsigned long pipe); 268c75f57fbSStefan Brüns int usb_get_configuration_no(struct usb_device *dev, int cfgno, 269c75f57fbSStefan Brüns unsigned char *buffer, int length); 270c75f57fbSStefan Brüns int usb_get_configuration_len(struct usb_device *dev, int cfgno); 271de39f8c1SMichael Trimarchi int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type, 272de39f8c1SMichael Trimarchi unsigned char id, void *buf, int size); 273012771d8Swdenk int usb_get_class_descriptor(struct usb_device *dev, int ifnum, 274de39f8c1SMichael Trimarchi unsigned char type, unsigned char id, void *buf, 275de39f8c1SMichael Trimarchi int size); 276012771d8Swdenk int usb_clear_halt(struct usb_device *dev, int pipe); 277012771d8Swdenk int usb_string(struct usb_device *dev, int index, char *buf, size_t size); 278012771d8Swdenk int usb_set_interface(struct usb_device *dev, int interface, int alternate); 27908f3bb0bSVincent Palatin int usb_get_port_status(struct usb_device *dev, int port, void *data); 280012771d8Swdenk 281012771d8Swdenk /* big endian -> little endian conversion */ 282149dded2Swdenk /* some CPUs are already little endian e.g. the ARM920T */ 283ae3b770eSMarkus Klotzbuecher #define __swap_16(x) \ 2843f85ce27Swdenk ({ unsigned short x_ = (unsigned short)x; \ 2853f85ce27Swdenk (unsigned short)( \ 2863f85ce27Swdenk ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \ 2873f85ce27Swdenk }) 288ae3b770eSMarkus Klotzbuecher #define __swap_32(x) \ 2893f85ce27Swdenk ({ unsigned long x_ = (unsigned long)x; \ 2903f85ce27Swdenk (unsigned long)( \ 2913f85ce27Swdenk ((x_ & 0x000000FFUL) << 24) | \ 2923f85ce27Swdenk ((x_ & 0x0000FF00UL) << 8) | \ 2933f85ce27Swdenk ((x_ & 0x00FF0000UL) >> 8) | \ 2943f85ce27Swdenk ((x_ & 0xFF000000UL) >> 24)); \ 2953f85ce27Swdenk }) 296ae3b770eSMarkus Klotzbuecher 297c7d703f3SMike Frysinger #ifdef __LITTLE_ENDIAN 298ae3b770eSMarkus Klotzbuecher # define swap_16(x) (x) 299ae3b770eSMarkus Klotzbuecher # define swap_32(x) (x) 300ae3b770eSMarkus Klotzbuecher #else 301ae3b770eSMarkus Klotzbuecher # define swap_16(x) __swap_16(x) 302ae3b770eSMarkus Klotzbuecher # define swap_32(x) __swap_32(x) 303c7d703f3SMike Frysinger #endif 304012771d8Swdenk 305012771d8Swdenk /* 306012771d8Swdenk * Calling this entity a "pipe" is glorifying it. A USB pipe 307012771d8Swdenk * is something embarrassingly simple: it basically consists 308012771d8Swdenk * of the following information: 309012771d8Swdenk * - device number (7 bits) 310012771d8Swdenk * - endpoint number (4 bits) 311012771d8Swdenk * - current Data0/1 state (1 bit) 312012771d8Swdenk * - direction (1 bit) 3133e126484SMichael Trimarchi * - speed (2 bits) 314012771d8Swdenk * - max packet size (2 bits: 8, 16, 32 or 64) 315012771d8Swdenk * - pipe type (2 bits: control, interrupt, bulk, isochronous) 316012771d8Swdenk * 317012771d8Swdenk * That's 18 bits. Really. Nothing more. And the USB people have 318012771d8Swdenk * documented these eighteen bits as some kind of glorious 319012771d8Swdenk * virtual data structure. 320012771d8Swdenk * 321012771d8Swdenk * Let's not fall in that trap. We'll just encode it as a simple 322012771d8Swdenk * unsigned int. The encoding is: 323012771d8Swdenk * 324012771d8Swdenk * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64) 325de39f8c1SMichael Trimarchi * - direction: bit 7 (0 = Host-to-Device [Out], 326de39f8c1SMichael Trimarchi * (1 = Device-to-Host [In]) 327012771d8Swdenk * - device: bits 8-14 328012771d8Swdenk * - endpoint: bits 15-18 329012771d8Swdenk * - Data0/1: bit 19 330de39f8c1SMichael Trimarchi * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 331de39f8c1SMichael Trimarchi * 10 = control, 11 = bulk) 332012771d8Swdenk * 333012771d8Swdenk * Why? Because it's arbitrary, and whatever encoding we select is really 334012771d8Swdenk * up to us. This one happens to share a lot of bit positions with the UHCI 335012771d8Swdenk * specification, so that much of the uhci driver can just mask the bits 336012771d8Swdenk * appropriately. 337012771d8Swdenk */ 338012771d8Swdenk /* Create various pipes... */ 339012771d8Swdenk #define create_pipe(dev,endpoint) \ 340d0fe1128SSergei Shtylyov (((dev)->devnum << 8) | ((endpoint) << 15) | \ 341c60795f4SIlya Yanok (dev)->maxpacketsize) 3423e126484SMichael Trimarchi #define default_pipe(dev) ((dev)->speed << 26) 343012771d8Swdenk 344de39f8c1SMichael Trimarchi #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \ 345de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 346de39f8c1SMichael Trimarchi #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \ 347de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 348de39f8c1SMichael Trimarchi USB_DIR_IN) 349de39f8c1SMichael Trimarchi #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \ 350de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 351de39f8c1SMichael Trimarchi #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \ 352de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 353de39f8c1SMichael Trimarchi USB_DIR_IN) 354de39f8c1SMichael Trimarchi #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \ 355de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 356de39f8c1SMichael Trimarchi #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \ 357de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 358de39f8c1SMichael Trimarchi USB_DIR_IN) 359de39f8c1SMichael Trimarchi #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \ 360de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 361de39f8c1SMichael Trimarchi #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \ 362de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 363de39f8c1SMichael Trimarchi USB_DIR_IN) 364de39f8c1SMichael Trimarchi #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \ 365de39f8c1SMichael Trimarchi default_pipe(dev)) 366de39f8c1SMichael Trimarchi #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \ 367de39f8c1SMichael Trimarchi default_pipe(dev) | \ 368de39f8c1SMichael Trimarchi USB_DIR_IN) 369012771d8Swdenk 370012771d8Swdenk /* The D0/D1 toggle bits */ 371012771d8Swdenk #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1) 372012771d8Swdenk #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep)) 373de39f8c1SMichael Trimarchi #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \ 374de39f8c1SMichael Trimarchi ((dev)->toggle[out] & \ 375de39f8c1SMichael Trimarchi ~(1 << ep)) | ((bit) << ep)) 376012771d8Swdenk 377012771d8Swdenk /* Endpoint halt control/status */ 378012771d8Swdenk #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1) 379012771d8Swdenk #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep))) 380012771d8Swdenk #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep))) 381012771d8Swdenk #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep))) 382012771d8Swdenk 383de39f8c1SMichael Trimarchi #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \ 384de39f8c1SMichael Trimarchi USB_PID_OUT) 385012771d8Swdenk 386012771d8Swdenk #define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1) 387012771d8Swdenk #define usb_pipein(pipe) (((pipe) >> 7) & 1) 388012771d8Swdenk #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f) 389012771d8Swdenk #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff) 390012771d8Swdenk #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf) 391012771d8Swdenk #define usb_pipedata(pipe) (((pipe) >> 19) & 1) 392012771d8Swdenk #define usb_pipetype(pipe) (((pipe) >> 30) & 3) 393012771d8Swdenk #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS) 394012771d8Swdenk #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT) 395012771d8Swdenk #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL) 396012771d8Swdenk #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK) 397012771d8Swdenk 3985853e133SVivek Gautam #define usb_pipe_ep_index(pipe) \ 3995853e133SVivek Gautam usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \ 4005853e133SVivek Gautam ((usb_pipeendpoint(pipe) * 2) - \ 4015853e133SVivek Gautam (usb_pipein(pipe) ? 0 : 1)) 402012771d8Swdenk 4030566e240SSimon Glass /** 4040566e240SSimon Glass * struct usb_device_id - identifies USB devices for probing and hotplugging 4050566e240SSimon Glass * @match_flags: Bit mask controlling which of the other fields are used to 4060566e240SSimon Glass * match against new devices. Any field except for driver_info may be 4070566e240SSimon Glass * used, although some only make sense in conjunction with other fields. 4080566e240SSimon Glass * This is usually set by a USB_DEVICE_*() macro, which sets all 4090566e240SSimon Glass * other fields in this structure except for driver_info. 4100566e240SSimon Glass * @idVendor: USB vendor ID for a device; numbers are assigned 4110566e240SSimon Glass * by the USB forum to its members. 4120566e240SSimon Glass * @idProduct: Vendor-assigned product ID. 4130566e240SSimon Glass * @bcdDevice_lo: Low end of range of vendor-assigned product version numbers. 4140566e240SSimon Glass * This is also used to identify individual product versions, for 4150566e240SSimon Glass * a range consisting of a single device. 4160566e240SSimon Glass * @bcdDevice_hi: High end of version number range. The range of product 4170566e240SSimon Glass * versions is inclusive. 4180566e240SSimon Glass * @bDeviceClass: Class of device; numbers are assigned 4190566e240SSimon Glass * by the USB forum. Products may choose to implement classes, 4200566e240SSimon Glass * or be vendor-specific. Device classes specify behavior of all 4210566e240SSimon Glass * the interfaces on a device. 4220566e240SSimon Glass * @bDeviceSubClass: Subclass of device; associated with bDeviceClass. 4230566e240SSimon Glass * @bDeviceProtocol: Protocol of device; associated with bDeviceClass. 4240566e240SSimon Glass * @bInterfaceClass: Class of interface; numbers are assigned 4250566e240SSimon Glass * by the USB forum. Products may choose to implement classes, 4260566e240SSimon Glass * or be vendor-specific. Interface classes specify behavior only 4270566e240SSimon Glass * of a given interface; other interfaces may support other classes. 4280566e240SSimon Glass * @bInterfaceSubClass: Subclass of interface; associated with bInterfaceClass. 4290566e240SSimon Glass * @bInterfaceProtocol: Protocol of interface; associated with bInterfaceClass. 4300566e240SSimon Glass * @bInterfaceNumber: Number of interface; composite devices may use 4310566e240SSimon Glass * fixed interface numbers to differentiate between vendor-specific 4320566e240SSimon Glass * interfaces. 4330566e240SSimon Glass * @driver_info: Holds information used by the driver. Usually it holds 4340566e240SSimon Glass * a pointer to a descriptor understood by the driver, or perhaps 4350566e240SSimon Glass * device flags. 4360566e240SSimon Glass * 4370566e240SSimon Glass * In most cases, drivers will create a table of device IDs by using 4380566e240SSimon Glass * USB_DEVICE(), or similar macros designed for that purpose. 4390566e240SSimon Glass * They will then export it to userspace using MODULE_DEVICE_TABLE(), 4400566e240SSimon Glass * and provide it to the USB core through their usb_driver structure. 4410566e240SSimon Glass * 4420566e240SSimon Glass * See the usb_match_id() function for information about how matches are 4430566e240SSimon Glass * performed. Briefly, you will normally use one of several macros to help 4440566e240SSimon Glass * construct these entries. Each entry you provide will either identify 4450566e240SSimon Glass * one or more specific products, or will identify a class of products 4460566e240SSimon Glass * which have agreed to behave the same. You should put the more specific 4470566e240SSimon Glass * matches towards the beginning of your table, so that driver_info can 4480566e240SSimon Glass * record quirks of specific products. 4490566e240SSimon Glass */ 4500566e240SSimon Glass struct usb_device_id { 4510566e240SSimon Glass /* which fields to match against? */ 4520566e240SSimon Glass u16 match_flags; 4530566e240SSimon Glass 4540566e240SSimon Glass /* Used for product specific matches; range is inclusive */ 4550566e240SSimon Glass u16 idVendor; 4560566e240SSimon Glass u16 idProduct; 4570566e240SSimon Glass u16 bcdDevice_lo; 4580566e240SSimon Glass u16 bcdDevice_hi; 4590566e240SSimon Glass 4600566e240SSimon Glass /* Used for device class matches */ 4610566e240SSimon Glass u8 bDeviceClass; 4620566e240SSimon Glass u8 bDeviceSubClass; 4630566e240SSimon Glass u8 bDeviceProtocol; 4640566e240SSimon Glass 4650566e240SSimon Glass /* Used for interface class matches */ 4660566e240SSimon Glass u8 bInterfaceClass; 4670566e240SSimon Glass u8 bInterfaceSubClass; 4680566e240SSimon Glass u8 bInterfaceProtocol; 4690566e240SSimon Glass 4700566e240SSimon Glass /* Used for vendor-specific interface matches */ 4710566e240SSimon Glass u8 bInterfaceNumber; 4720566e240SSimon Glass 4730566e240SSimon Glass /* not matched against */ 4740566e240SSimon Glass ulong driver_info; 4750566e240SSimon Glass }; 4760566e240SSimon Glass 4770566e240SSimon Glass /* Some useful macros to use to create struct usb_device_id */ 4780566e240SSimon Glass #define USB_DEVICE_ID_MATCH_VENDOR 0x0001 4790566e240SSimon Glass #define USB_DEVICE_ID_MATCH_PRODUCT 0x0002 4800566e240SSimon Glass #define USB_DEVICE_ID_MATCH_DEV_LO 0x0004 4810566e240SSimon Glass #define USB_DEVICE_ID_MATCH_DEV_HI 0x0008 4820566e240SSimon Glass #define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010 4830566e240SSimon Glass #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020 4840566e240SSimon Glass #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040 4850566e240SSimon Glass #define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080 4860566e240SSimon Glass #define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100 4870566e240SSimon Glass #define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200 4880566e240SSimon Glass #define USB_DEVICE_ID_MATCH_INT_NUMBER 0x0400 4890566e240SSimon Glass 4900566e240SSimon Glass /* Match anything, indicates this is a valid entry even if everything is 0 */ 4910566e240SSimon Glass #define USB_DEVICE_ID_MATCH_NONE 0x0800 4920566e240SSimon Glass #define USB_DEVICE_ID_MATCH_ALL 0x07ff 4930566e240SSimon Glass 4940566e240SSimon Glass /** 4950566e240SSimon Glass * struct usb_driver_entry - Matches a driver to its usb_device_ids 496b483915fSSimon Glass * @driver: Driver to use 497b483915fSSimon Glass * @match: List of match records for this driver, terminated by {} 4980566e240SSimon Glass */ 4990566e240SSimon Glass struct usb_driver_entry { 5000566e240SSimon Glass struct driver *driver; 5010566e240SSimon Glass const struct usb_device_id *match; 5020566e240SSimon Glass }; 5030566e240SSimon Glass 504abb59cffSSimon Glass #define USB_DEVICE_ID_MATCH_DEVICE \ 505abb59cffSSimon Glass (USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT) 506abb59cffSSimon Glass 507abb59cffSSimon Glass /** 508abb59cffSSimon Glass * USB_DEVICE - macro used to describe a specific usb device 509abb59cffSSimon Glass * @vend: the 16 bit USB Vendor ID 510abb59cffSSimon Glass * @prod: the 16 bit USB Product ID 511abb59cffSSimon Glass * 512abb59cffSSimon Glass * This macro is used to create a struct usb_device_id that matches a 513abb59cffSSimon Glass * specific device. 514abb59cffSSimon Glass */ 515abb59cffSSimon Glass #define USB_DEVICE(vend, prod) \ 516abb59cffSSimon Glass .match_flags = USB_DEVICE_ID_MATCH_DEVICE, \ 517abb59cffSSimon Glass .idVendor = (vend), \ 518abb59cffSSimon Glass .idProduct = (prod) 519abb59cffSSimon Glass 520abb59cffSSimon Glass #define U_BOOT_USB_DEVICE(__name, __match) \ 5210566e240SSimon Glass ll_entry_declare(struct usb_driver_entry, __name, usb_driver_entry) = {\ 5220566e240SSimon Glass .driver = llsym(struct driver, __name, driver), \ 5230566e240SSimon Glass .match = __match, \ 5240566e240SSimon Glass } 5250566e240SSimon Glass 526012771d8Swdenk /************************************************************************* 527012771d8Swdenk * Hub Stuff 528012771d8Swdenk */ 529012771d8Swdenk struct usb_port_status { 530012771d8Swdenk unsigned short wPortStatus; 531012771d8Swdenk unsigned short wPortChange; 532012771d8Swdenk } __attribute__ ((packed)); 533012771d8Swdenk 534012771d8Swdenk struct usb_hub_status { 535012771d8Swdenk unsigned short wHubStatus; 536012771d8Swdenk unsigned short wHubChange; 537012771d8Swdenk } __attribute__ ((packed)); 538012771d8Swdenk 5395624dfd5SBin Meng /* 5405624dfd5SBin Meng * Hub Device descriptor 5415624dfd5SBin Meng * USB Hub class device protocols 5425624dfd5SBin Meng */ 5435624dfd5SBin Meng #define USB_HUB_PR_FS 0 /* Full speed hub */ 5445624dfd5SBin Meng #define USB_HUB_PR_HS_NO_TT 0 /* Hi-speed hub without TT */ 5455624dfd5SBin Meng #define USB_HUB_PR_HS_SINGLE_TT 1 /* Hi-speed hub with single TT */ 5465624dfd5SBin Meng #define USB_HUB_PR_HS_MULTI_TT 2 /* Hi-speed hub with multiple TT */ 5475624dfd5SBin Meng #define USB_HUB_PR_SS 3 /* Super speed hub */ 5485624dfd5SBin Meng 5495624dfd5SBin Meng /* Transaction Translator Think Times, in bits */ 5505624dfd5SBin Meng #define HUB_TTTT_8_BITS 0x00 5515624dfd5SBin Meng #define HUB_TTTT_16_BITS 0x20 5525624dfd5SBin Meng #define HUB_TTTT_24_BITS 0x40 5535624dfd5SBin Meng #define HUB_TTTT_32_BITS 0x60 554012771d8Swdenk 555012771d8Swdenk /* Hub descriptor */ 556012771d8Swdenk struct usb_hub_descriptor { 557012771d8Swdenk unsigned char bLength; 558012771d8Swdenk unsigned char bDescriptorType; 559012771d8Swdenk unsigned char bNbrPorts; 560012771d8Swdenk unsigned short wHubCharacteristics; 561012771d8Swdenk unsigned char bPwrOn2PwrGood; 562012771d8Swdenk unsigned char bHubContrCurrent; 563337fc7e6SBin Meng /* 2.0 and 3.0 hubs differ here */ 564337fc7e6SBin Meng union { 565337fc7e6SBin Meng struct { 566337fc7e6SBin Meng /* add 1 bit for hub status change; round to bytes */ 567337fc7e6SBin Meng __u8 DeviceRemovable[(USB_MAXCHILDREN + 1 + 7) / 8]; 568337fc7e6SBin Meng __u8 PortPowerCtrlMask[(USB_MAXCHILDREN + 1 + 7) / 8]; 569337fc7e6SBin Meng } __attribute__ ((packed)) hs; 570337fc7e6SBin Meng 571337fc7e6SBin Meng struct { 572337fc7e6SBin Meng __u8 bHubHdrDecLat; 573337fc7e6SBin Meng __le16 wHubDelay; 574337fc7e6SBin Meng __le16 DeviceRemovable; 575337fc7e6SBin Meng } __attribute__ ((packed)) ss; 576337fc7e6SBin Meng } u; 577012771d8Swdenk } __attribute__ ((packed)); 578012771d8Swdenk 579012771d8Swdenk 580012771d8Swdenk struct usb_hub_device { 581012771d8Swdenk struct usb_device *pusb_dev; 582012771d8Swdenk struct usb_hub_descriptor desc; 583c998da0dSStefan Roese 584c998da0dSStefan Roese ulong connect_timeout; /* Device connection timeout in ms */ 585c998da0dSStefan Roese ulong query_delay; /* Device query delay in ms */ 586c998da0dSStefan Roese int overcurrent_count[USB_MAXCHILDREN]; /* Over-current counter */ 587bbc6f06cSBin Meng int hub_depth; /* USB 3.0 hub depth */ 5885624dfd5SBin Meng struct usb_tt tt; /* Transaction Translator */ 589012771d8Swdenk }; 590012771d8Swdenk 591*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB) 592de31213fSSimon Glass /** 593de31213fSSimon Glass * struct usb_platdata - Platform data about a USB controller 594de31213fSSimon Glass * 595de31213fSSimon Glass * Given a USB controller (UCLASS_USB) dev this is dev_get_platdata(dev) 596de31213fSSimon Glass */ 597de31213fSSimon Glass struct usb_platdata { 598de31213fSSimon Glass enum usb_init_type init_type; 599de31213fSSimon Glass }; 600de31213fSSimon Glass 601de31213fSSimon Glass /** 602de31213fSSimon Glass * struct usb_dev_platdata - Platform data about a USB device 603de31213fSSimon Glass * 604de31213fSSimon Glass * Given a USB device dev this structure is dev_get_parent_platdata(dev). 605de31213fSSimon Glass * This is used by sandbox to provide emulation data also. 606de31213fSSimon Glass * 607de31213fSSimon Glass * @id: ID used to match this device 608de31213fSSimon Glass * @devnum: Device address on the USB bus 6097f1a0753SHans de Goede * @udev: usb-uclass internal use only do NOT use 610de31213fSSimon Glass * @strings: List of descriptor strings (for sandbox emulation purposes) 611de31213fSSimon Glass * @desc_list: List of descriptors (for sandbox emulation purposes) 612de31213fSSimon Glass */ 613de31213fSSimon Glass struct usb_dev_platdata { 614de31213fSSimon Glass struct usb_device_id id; 615de31213fSSimon Glass int devnum; 6167f1a0753SHans de Goede /* 6177f1a0753SHans de Goede * This pointer is used to pass the usb_device used in usb_scan_device, 6187f1a0753SHans de Goede * to get the usb descriptors before the driver is known, to the 6197f1a0753SHans de Goede * actual udevice once the driver is known and the udevice is created. 6207f1a0753SHans de Goede * This will be NULL except during probe, do NOT use. 6217f1a0753SHans de Goede * 6227f1a0753SHans de Goede * This should eventually go away. 6237f1a0753SHans de Goede */ 6247f1a0753SHans de Goede struct usb_device *udev; 625de31213fSSimon Glass #ifdef CONFIG_SANDBOX 626de31213fSSimon Glass struct usb_string *strings; 627de31213fSSimon Glass /* NULL-terminated list of descriptor pointers */ 628de31213fSSimon Glass struct usb_generic_descriptor **desc_list; 629de31213fSSimon Glass #endif 630de31213fSSimon Glass int configno; 631de31213fSSimon Glass }; 632de31213fSSimon Glass 633de31213fSSimon Glass /** 634de31213fSSimon Glass * struct usb_bus_priv - information about the USB controller 635de31213fSSimon Glass * 636de31213fSSimon Glass * Given a USB controller (UCLASS_USB) 'dev', this is 637de31213fSSimon Glass * dev_get_uclass_priv(dev). 638de31213fSSimon Glass * 639de31213fSSimon Glass * @next_addr: Next device address to allocate minus 1. Incremented by 1 640de31213fSSimon Glass * each time a new device address is set, so this holds the 641de31213fSSimon Glass * number of devices on the bus 642de31213fSSimon Glass * @desc_before_addr: true if we can read a device descriptor before it 643de31213fSSimon Glass * has been assigned an address. For XHCI this is not possible 644de31213fSSimon Glass * so this will be false. 645b6de4d10SHans de Goede * @companion: True if this is a companion controller to another USB 646b6de4d10SHans de Goede * controller 647de31213fSSimon Glass */ 648de31213fSSimon Glass struct usb_bus_priv { 649de31213fSSimon Glass int next_addr; 650de31213fSSimon Glass bool desc_before_addr; 651b6de4d10SHans de Goede bool companion; 652de31213fSSimon Glass }; 653de31213fSSimon Glass 654de31213fSSimon Glass /** 65584aa8536SBin Meng * struct usb_emul_platdata - platform data about the USB emulator 65684aa8536SBin Meng * 65784aa8536SBin Meng * Given a USB emulator (UCLASS_USB_EMUL) 'dev', this is 65884aa8536SBin Meng * dev_get_uclass_platdata(dev). 65984aa8536SBin Meng * 66084aa8536SBin Meng * @port1: USB emulator device port number on the parent hub 66184aa8536SBin Meng */ 66284aa8536SBin Meng struct usb_emul_platdata { 66384aa8536SBin Meng int port1; /* Port number (numbered from 1) */ 66484aa8536SBin Meng }; 66584aa8536SBin Meng 66684aa8536SBin Meng /** 667de31213fSSimon Glass * struct dm_usb_ops - USB controller operations 668de31213fSSimon Glass * 669de31213fSSimon Glass * This defines the operations supoorted on a USB controller. Common 670de31213fSSimon Glass * arguments are: 671de31213fSSimon Glass * 672de31213fSSimon Glass * @bus: USB bus (i.e. controller), which is in UCLASS_USB. 673de31213fSSimon Glass * @udev: USB device parent data. Controllers are not expected to need 674de31213fSSimon Glass * this, since the device address on the bus is encoded in @pipe. 675de31213fSSimon Glass * It is used for sandbox, and can be handy for debugging and 676de31213fSSimon Glass * logging. 677de31213fSSimon Glass * @pipe: An assortment of bitfields which provide address and packet 678de31213fSSimon Glass * type information. See create_pipe() above for encoding 679de31213fSSimon Glass * details 680de31213fSSimon Glass * @buffer: A buffer to use for sending/receiving. This should be 681de31213fSSimon Glass * DMA-aligned. 682de31213fSSimon Glass * @length: Buffer length in bytes 683de31213fSSimon Glass */ 684de31213fSSimon Glass struct dm_usb_ops { 685de31213fSSimon Glass /** 686de31213fSSimon Glass * control() - Send a control message 687de31213fSSimon Glass * 688de31213fSSimon Glass * Most parameters are as above. 689de31213fSSimon Glass * 690de31213fSSimon Glass * @setup: Additional setup information required by the message 691de31213fSSimon Glass */ 692de31213fSSimon Glass int (*control)(struct udevice *bus, struct usb_device *udev, 693de31213fSSimon Glass unsigned long pipe, void *buffer, int length, 694de31213fSSimon Glass struct devrequest *setup); 695de31213fSSimon Glass /** 696de31213fSSimon Glass * bulk() - Send a bulk message 697de31213fSSimon Glass * 698de31213fSSimon Glass * Parameters are as above. 699de31213fSSimon Glass */ 700de31213fSSimon Glass int (*bulk)(struct udevice *bus, struct usb_device *udev, 701de31213fSSimon Glass unsigned long pipe, void *buffer, int length); 702de31213fSSimon Glass /** 703de31213fSSimon Glass * interrupt() - Send an interrupt message 704de31213fSSimon Glass * 705de31213fSSimon Glass * Most parameters are as above. 706de31213fSSimon Glass * 707de31213fSSimon Glass * @interval: Interrupt interval 708de31213fSSimon Glass */ 709de31213fSSimon Glass int (*interrupt)(struct udevice *bus, struct usb_device *udev, 710de31213fSSimon Glass unsigned long pipe, void *buffer, int length, 711de31213fSSimon Glass int interval); 7128a5f0665SHans de Goede 7138a5f0665SHans de Goede /** 7148a5f0665SHans de Goede * create_int_queue() - Create and queue interrupt packets 7158a5f0665SHans de Goede * 7168a5f0665SHans de Goede * Create and queue @queuesize number of interrupt usb packets of 7178a5f0665SHans de Goede * @elementsize bytes each. @buffer must be atleast @queuesize * 7188a5f0665SHans de Goede * @elementsize bytes. 7198a5f0665SHans de Goede * 7208a5f0665SHans de Goede * Note some controllers only support a queuesize of 1. 7218a5f0665SHans de Goede * 7228a5f0665SHans de Goede * @interval: Interrupt interval 7238a5f0665SHans de Goede * 7248a5f0665SHans de Goede * @return A pointer to the created interrupt queue or NULL on error 7258a5f0665SHans de Goede */ 7268a5f0665SHans de Goede struct int_queue * (*create_int_queue)(struct udevice *bus, 7278a5f0665SHans de Goede struct usb_device *udev, unsigned long pipe, 7288a5f0665SHans de Goede int queuesize, int elementsize, void *buffer, 7298a5f0665SHans de Goede int interval); 7308a5f0665SHans de Goede 7318a5f0665SHans de Goede /** 7328a5f0665SHans de Goede * poll_int_queue() - Poll an interrupt queue for completed packets 7338a5f0665SHans de Goede * 7348a5f0665SHans de Goede * Poll an interrupt queue for completed packets. The return value 7358a5f0665SHans de Goede * points to the part of the buffer passed to create_int_queue() 7368a5f0665SHans de Goede * corresponding to the completed packet. 7378a5f0665SHans de Goede * 7388a5f0665SHans de Goede * @queue: queue to poll 7398a5f0665SHans de Goede * 7408a5f0665SHans de Goede * @return Pointer to the data of the first completed packet, or 7418a5f0665SHans de Goede * NULL if no packets are ready 7428a5f0665SHans de Goede */ 7438a5f0665SHans de Goede void * (*poll_int_queue)(struct udevice *bus, struct usb_device *udev, 7448a5f0665SHans de Goede struct int_queue *queue); 7458a5f0665SHans de Goede 7468a5f0665SHans de Goede /** 7478a5f0665SHans de Goede * destroy_int_queue() - Destroy an interrupt queue 7488a5f0665SHans de Goede * 7498a5f0665SHans de Goede * Destroy an interrupt queue created by create_int_queue(). 7508a5f0665SHans de Goede * 7518a5f0665SHans de Goede * @queue: queue to poll 7528a5f0665SHans de Goede * 7538a5f0665SHans de Goede * @return 0 if OK, -ve on error 7548a5f0665SHans de Goede */ 7558a5f0665SHans de Goede int (*destroy_int_queue)(struct udevice *bus, struct usb_device *udev, 7568a5f0665SHans de Goede struct int_queue *queue); 7578a5f0665SHans de Goede 758de31213fSSimon Glass /** 759de31213fSSimon Glass * alloc_device() - Allocate a new device context (XHCI) 760de31213fSSimon Glass * 761de31213fSSimon Glass * Before sending packets to a new device on an XHCI bus, a device 762de31213fSSimon Glass * context must be created. If this method is not NULL it will be 763de31213fSSimon Glass * called before the device is enumerated (even before its descriptor 764de31213fSSimon Glass * is read). This should be NULL for EHCI, which does not need this. 765de31213fSSimon Glass */ 766de31213fSSimon Glass int (*alloc_device)(struct udevice *bus, struct usb_device *udev); 767b2f219b0SHans de Goede 768b2f219b0SHans de Goede /** 769b2f219b0SHans de Goede * reset_root_port() - Reset usb root port 770b2f219b0SHans de Goede */ 771b2f219b0SHans de Goede int (*reset_root_port)(struct udevice *bus, struct usb_device *udev); 7729ca1b4baSBin Meng 7739ca1b4baSBin Meng /** 7749ca1b4baSBin Meng * update_hub_device() - Update HCD's internal representation of hub 7759ca1b4baSBin Meng * 7769ca1b4baSBin Meng * After a hub descriptor is fetched, notify HCD so that its internal 7779ca1b4baSBin Meng * representation of this hub can be updated (xHCI) 7789ca1b4baSBin Meng */ 7799ca1b4baSBin Meng int (*update_hub_device)(struct udevice *bus, struct usb_device *udev); 7803e59f590SBin Meng 7813e59f590SBin Meng /** 7823e59f590SBin Meng * get_max_xfer_size() - Get HCD's maximum transfer bytes 7833e59f590SBin Meng * 7843e59f590SBin Meng * The HCD may have limitation on the maximum bytes to be transferred 7853e59f590SBin Meng * in a USB transfer. USB class driver needs to be aware of this. 7863e59f590SBin Meng */ 7873e59f590SBin Meng int (*get_max_xfer_size)(struct udevice *bus, size_t *size); 788de31213fSSimon Glass }; 789de31213fSSimon Glass 790de31213fSSimon Glass #define usb_get_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops) 791de31213fSSimon Glass #define usb_get_emul_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops) 792de31213fSSimon Glass 793de31213fSSimon Glass /** 794de31213fSSimon Glass * usb_get_dev_index() - look up a device index number 795de31213fSSimon Glass * 796de31213fSSimon Glass * Look up devices using their index number (starting at 0). This works since 797de31213fSSimon Glass * in U-Boot device addresses are allocated starting at 1 with no gaps. 798de31213fSSimon Glass * 799de31213fSSimon Glass * TODO(sjg@chromium.org): Remove this function when usb_ether.c is modified 800de31213fSSimon Glass * to work better with driver model. 801de31213fSSimon Glass * 802de31213fSSimon Glass * @bus: USB bus to check 803de31213fSSimon Glass * @index: Index number of device to find (0=first). This is just the 804de31213fSSimon Glass * device address less 1. 805de31213fSSimon Glass */ 806de31213fSSimon Glass struct usb_device *usb_get_dev_index(struct udevice *bus, int index); 807de31213fSSimon Glass 808de31213fSSimon Glass /** 809de31213fSSimon Glass * usb_setup_device() - set up a device ready for use 810de31213fSSimon Glass * 811de31213fSSimon Glass * @dev: USB device pointer. This need not be a real device - it is 812de31213fSSimon Glass * common for it to just be a local variable with its ->dev 8139eb72dd1SHans de Goede * member (i.e. @dev->dev) set to the parent device and 8149eb72dd1SHans de Goede * dev->portnr set to the port number on the hub (1=first) 815de31213fSSimon Glass * @do_read: true to read the device descriptor before an address is set 816de31213fSSimon Glass * (should be false for XHCI buses, true otherwise) 817de31213fSSimon Glass * @parent: Parent device (either UCLASS_USB or UCLASS_USB_HUB) 818de31213fSSimon Glass * @return 0 if OK, -ve on error */ 819de31213fSSimon Glass int usb_setup_device(struct usb_device *dev, bool do_read, 8209eb72dd1SHans de Goede struct usb_device *parent); 821de31213fSSimon Glass 822de31213fSSimon Glass /** 82346c1d493SBin Meng * usb_hub_is_root_hub() - Test whether a hub device is root hub or not 82446c1d493SBin Meng * 82546c1d493SBin Meng * @hub: USB hub device to test 82646c1d493SBin Meng * @return: true if the hub device is root hub, false otherwise. 82746c1d493SBin Meng */ 82846c1d493SBin Meng bool usb_hub_is_root_hub(struct udevice *hub); 82946c1d493SBin Meng 83046c1d493SBin Meng /** 831de31213fSSimon Glass * usb_hub_scan() - Scan a hub and find its devices 832de31213fSSimon Glass * 833de31213fSSimon Glass * @hub: Hub device to scan 834de31213fSSimon Glass */ 835de31213fSSimon Glass int usb_hub_scan(struct udevice *hub); 836de31213fSSimon Glass 837de31213fSSimon Glass /** 838de31213fSSimon Glass * usb_scan_device() - Scan a device on a bus 839de31213fSSimon Glass * 840de31213fSSimon Glass * Scan a device on a bus. It has already been detected and is ready to 841de31213fSSimon Glass * be enumerated. This may be either the root hub (@parent is a bus) or a 842de31213fSSimon Glass * normal device (@parent is a hub) 843de31213fSSimon Glass * 844de31213fSSimon Glass * @parent: Parent device 845de31213fSSimon Glass * @port: Hub port number (numbered from 1) 846de31213fSSimon Glass * @speed: USB speed to use for this device 847de31213fSSimon Glass * @devp: Returns pointer to device if all is well 848de31213fSSimon Glass * @return 0 if OK, -ve on error 849de31213fSSimon Glass */ 850de31213fSSimon Glass int usb_scan_device(struct udevice *parent, int port, 851de31213fSSimon Glass enum usb_device_speed speed, struct udevice **devp); 852de31213fSSimon Glass 853de31213fSSimon Glass /** 854de31213fSSimon Glass * usb_get_bus() - Find the bus for a device 855de31213fSSimon Glass * 856de31213fSSimon Glass * Search up through parents to find the bus this device is connected to. This 857de31213fSSimon Glass * will be a device with uclass UCLASS_USB. 858de31213fSSimon Glass * 859de31213fSSimon Glass * @dev: Device to check 860f78a5c07SHans de Goede * @return The bus, or NULL if not found (this indicates a critical error in 861f78a5c07SHans de Goede * the USB stack 862de31213fSSimon Glass */ 863f78a5c07SHans de Goede struct udevice *usb_get_bus(struct udevice *dev); 864de31213fSSimon Glass 865de31213fSSimon Glass /** 866de31213fSSimon Glass * usb_select_config() - Set up a device ready for use 867de31213fSSimon Glass * 868de31213fSSimon Glass * This function assumes that the device already has an address and a driver 869de31213fSSimon Glass * bound, and is ready to be set up. 870de31213fSSimon Glass * 871de31213fSSimon Glass * This re-reads the device and configuration descriptors and sets the 872de31213fSSimon Glass * configuration 873de31213fSSimon Glass * 874de31213fSSimon Glass * @dev: Device to set up 875de31213fSSimon Glass */ 876de31213fSSimon Glass int usb_select_config(struct usb_device *dev); 877de31213fSSimon Glass 878de31213fSSimon Glass /** 879de31213fSSimon Glass * usb_child_pre_probe() - Pre-probe function for USB devices 880de31213fSSimon Glass * 881de31213fSSimon Glass * This is called on all children of hubs and USB controllers (i.e. UCLASS_USB 882de31213fSSimon Glass * and UCLASS_USB_HUB) when a new device is about to be probed. It sets up the 883de31213fSSimon Glass * device from the saved platform data and calls usb_select_config() to 884de31213fSSimon Glass * finish set up. 885de31213fSSimon Glass * 886de31213fSSimon Glass * Once this is done, the device's normal driver can take over, knowing the 887de31213fSSimon Glass * device is accessible on the USB bus. 888de31213fSSimon Glass * 889de31213fSSimon Glass * This function is for use only by the internal USB stack. 890de31213fSSimon Glass * 891de31213fSSimon Glass * @dev: Device to set up 892de31213fSSimon Glass */ 893de31213fSSimon Glass int usb_child_pre_probe(struct udevice *dev); 894de31213fSSimon Glass 895de31213fSSimon Glass struct ehci_ctrl; 896de31213fSSimon Glass 897de31213fSSimon Glass /** 898de31213fSSimon Glass * usb_setup_ehci_gadget() - Set up a USB device as a gadget 899de31213fSSimon Glass * 900de31213fSSimon Glass * TODO(sjg@chromium.org): Tidy this up when USB gadgets can use driver model 901de31213fSSimon Glass * 902de31213fSSimon Glass * This provides a way to tell a controller to start up as a USB device 903de31213fSSimon Glass * instead of as a host. It is untested. 904de31213fSSimon Glass */ 905de31213fSSimon Glass int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp); 906de31213fSSimon Glass 907de31213fSSimon Glass /** 908de31213fSSimon Glass * usb_stor_reset() - Prepare to scan USB storage devices 909de31213fSSimon Glass * 910de31213fSSimon Glass * Empty the list of USB storage devices in preparation for scanning them. 911de31213fSSimon Glass * This must be called before a USB scan. 912de31213fSSimon Glass */ 913de31213fSSimon Glass void usb_stor_reset(void); 914de31213fSSimon Glass 915*fd09c205SSven Schwermer #else /* !CONFIG_IS_ENABLED(DM_USB) */ 916de31213fSSimon Glass 917de31213fSSimon Glass struct usb_device *usb_get_dev_index(int index); 918de31213fSSimon Glass 919de31213fSSimon Glass #endif 920de31213fSSimon Glass 921de31213fSSimon Glass bool usb_device_has_child_on_port(struct usb_device *parent, int port); 922de31213fSSimon Glass 92323faf2bcSMarek Vasut int usb_hub_probe(struct usb_device *dev, int ifnum); 92423faf2bcSMarek Vasut void usb_hub_reset(void); 925862e75c0SSimon Glass 926faa7db24SStefan Brüns /* 927faa7db24SStefan Brüns * usb_find_usb2_hub_address_port() - Get hub address and port for TT setting 928faa7db24SStefan Brüns * 929faa7db24SStefan Brüns * Searches for the first HS hub above the given device. If a 930faa7db24SStefan Brüns * HS hub is found, the hub address and the port the device is 931faa7db24SStefan Brüns * connected to is return, as required for SPLIT transactions 932faa7db24SStefan Brüns * 933faa7db24SStefan Brüns * @param: udev full speed or low speed device 934faa7db24SStefan Brüns */ 935faa7db24SStefan Brüns void usb_find_usb2_hub_address_port(struct usb_device *udev, 936faa7db24SStefan Brüns uint8_t *hub_address, uint8_t *hub_port); 937faa7db24SStefan Brüns 93879b58887SSimon Glass /** 93979b58887SSimon Glass * usb_alloc_new_device() - Allocate a new device 94079b58887SSimon Glass * 94179b58887SSimon Glass * @devp: returns a pointer of a new device structure. With driver model this 94279b58887SSimon Glass * is a device pointer, but with legacy USB this pointer is 94379b58887SSimon Glass * driver-specific. 94479b58887SSimon Glass * @return 0 if OK, -ENOSPC if we have found out of room for new devices 94579b58887SSimon Glass */ 94679b58887SSimon Glass int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp); 94779b58887SSimon Glass 94879b58887SSimon Glass /** 94979b58887SSimon Glass * usb_free_device() - Free a partially-inited device 95079b58887SSimon Glass * 95179b58887SSimon Glass * This is an internal function. It is used to reverse the action of 95279b58887SSimon Glass * usb_alloc_new_device() when we hit a problem during init. 95379b58887SSimon Glass */ 95479b58887SSimon Glass void usb_free_device(struct udevice *controller); 955c7e3b2b5SLucas Stach 95623faf2bcSMarek Vasut int usb_new_device(struct usb_device *dev); 95779b58887SSimon Glass 9585853e133SVivek Gautam int usb_alloc_device(struct usb_device *dev); 95923faf2bcSMarek Vasut 960019808f9SSimon Glass /** 9613e59f590SBin Meng * usb_update_hub_device() - Update HCD's internal representation of hub 9629ca1b4baSBin Meng * 9639ca1b4baSBin Meng * After a hub descriptor is fetched, notify HCD so that its internal 9649ca1b4baSBin Meng * representation of this hub can be updated. 9659ca1b4baSBin Meng * 9669ca1b4baSBin Meng * @dev: Hub device 9679ca1b4baSBin Meng * @return 0 if OK, -ve on error 9689ca1b4baSBin Meng */ 9699ca1b4baSBin Meng int usb_update_hub_device(struct usb_device *dev); 9709ca1b4baSBin Meng 9719ca1b4baSBin Meng /** 9723e59f590SBin Meng * usb_get_max_xfer_size() - Get HCD's maximum transfer bytes 9733e59f590SBin Meng * 9743e59f590SBin Meng * The HCD may have limitation on the maximum bytes to be transferred 9753e59f590SBin Meng * in a USB transfer. USB class driver needs to be aware of this. 9763e59f590SBin Meng * 9773e59f590SBin Meng * @dev: USB device 9783e59f590SBin Meng * @size: maximum transfer bytes 9793e59f590SBin Meng * @return 0 if OK, -ve on error 9803e59f590SBin Meng */ 9813e59f590SBin Meng int usb_get_max_xfer_size(struct usb_device *dev, size_t *size); 9823e59f590SBin Meng 9833e59f590SBin Meng /** 984019808f9SSimon Glass * usb_emul_setup_device() - Set up a new USB device emulation 985019808f9SSimon Glass * 986019808f9SSimon Glass * This is normally called when a new emulation device is bound. It tells 987019808f9SSimon Glass * the USB emulation uclass about the features of the emulator. 988019808f9SSimon Glass * 989019808f9SSimon Glass * @dev: Emulation device 990019808f9SSimon Glass * @strings: List of USB string descriptors, terminated by a NULL 991019808f9SSimon Glass * entry 992019808f9SSimon Glass * @desc_list: List of points or USB descriptors, terminated by NULL. 993019808f9SSimon Glass * The first entry must be struct usb_device_descriptor, 994019808f9SSimon Glass * and others follow on after that. 9959ca1b4baSBin Meng * @return 0 if OK, -ENOSYS if not implemented, other -ve on error 996019808f9SSimon Glass */ 99798b639fcSBin Meng int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings, 99898b639fcSBin Meng void **desc_list); 999019808f9SSimon Glass 1000019808f9SSimon Glass /** 1001019808f9SSimon Glass * usb_emul_control() - Send a control packet to an emulator 1002019808f9SSimon Glass * 1003019808f9SSimon Glass * @emul: Emulator device 1004019808f9SSimon Glass * @udev: USB device (which the emulator is causing to appear) 1005019808f9SSimon Glass * See struct dm_usb_ops for details on other parameters 1006019808f9SSimon Glass * @return 0 if OK, -ve on error 1007019808f9SSimon Glass */ 1008019808f9SSimon Glass int usb_emul_control(struct udevice *emul, struct usb_device *udev, 1009019808f9SSimon Glass unsigned long pipe, void *buffer, int length, 1010019808f9SSimon Glass struct devrequest *setup); 1011019808f9SSimon Glass 1012019808f9SSimon Glass /** 1013019808f9SSimon Glass * usb_emul_bulk() - Send a bulk packet to an emulator 1014019808f9SSimon Glass * 1015019808f9SSimon Glass * @emul: Emulator device 1016019808f9SSimon Glass * @udev: USB device (which the emulator is causing to appear) 1017019808f9SSimon Glass * See struct dm_usb_ops for details on other parameters 1018019808f9SSimon Glass * @return 0 if OK, -ve on error 1019019808f9SSimon Glass */ 1020019808f9SSimon Glass int usb_emul_bulk(struct udevice *emul, struct usb_device *udev, 1021019808f9SSimon Glass unsigned long pipe, void *buffer, int length); 1022019808f9SSimon Glass 1023019808f9SSimon Glass /** 1024b70a3feaSSimon Glass * usb_emul_int() - Send an interrupt packet to an emulator 1025b70a3feaSSimon Glass * 1026b70a3feaSSimon Glass * @emul: Emulator device 1027b70a3feaSSimon Glass * @udev: USB device (which the emulator is causing to appear) 1028b70a3feaSSimon Glass * See struct dm_usb_ops for details on other parameters 1029b70a3feaSSimon Glass * @return 0 if OK, -ve on error 1030b70a3feaSSimon Glass */ 1031b70a3feaSSimon Glass int usb_emul_int(struct udevice *emul, struct usb_device *udev, 1032b70a3feaSSimon Glass unsigned long pipe, void *buffer, int length, int interval); 1033b70a3feaSSimon Glass 1034b70a3feaSSimon Glass /** 1035019808f9SSimon Glass * usb_emul_find() - Find an emulator for a particular device 1036019808f9SSimon Glass * 103784aa8536SBin Meng * Check @pipe and @port1 to find a device number on bus @bus and return it. 1038019808f9SSimon Glass * 1039019808f9SSimon Glass * @bus: USB bus (controller) 1040019808f9SSimon Glass * @pipe: Describes pipe being used, and includes the device number 104184aa8536SBin Meng * @port1: Describes port number on the parent hub 1042019808f9SSimon Glass * @emulp: Returns pointer to emulator, or NULL if not found 1043019808f9SSimon Glass * @return 0 if found, -ve on error 1044019808f9SSimon Glass */ 104584aa8536SBin Meng int usb_emul_find(struct udevice *bus, ulong pipe, int port1, 104684aa8536SBin Meng struct udevice **emulp); 1047019808f9SSimon Glass 1048019808f9SSimon Glass /** 1049af9c7c11SSimon Glass * usb_emul_find_for_dev() - Find an emulator for a particular device 1050af9c7c11SSimon Glass * 1051af9c7c11SSimon Glass * @dev: USB device to check 1052af9c7c11SSimon Glass * @emulp: Returns pointer to emulator, or NULL if not found 1053af9c7c11SSimon Glass * @return 0 if found, -ve on error 1054af9c7c11SSimon Glass */ 1055af9c7c11SSimon Glass int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp); 1056af9c7c11SSimon Glass 1057af9c7c11SSimon Glass /** 1058848436a4SBin Meng * usb_emul_find_descriptor() - Find a USB descriptor of a particular device 1059848436a4SBin Meng * 1060848436a4SBin Meng * @ptr: a pointer to a list of USB descriptor pointers 1061848436a4SBin Meng * @type: type of USB descriptor to find 1062848436a4SBin Meng * @index: if @type is USB_DT_CONFIG, this is the configuration value 1063848436a4SBin Meng * @return a pointer to the USB descriptor found, NULL if not found 1064848436a4SBin Meng */ 1065848436a4SBin Meng struct usb_generic_descriptor **usb_emul_find_descriptor( 1066848436a4SBin Meng struct usb_generic_descriptor **ptr, int type, int index); 1067848436a4SBin Meng 1068848436a4SBin Meng /** 106945bfa47eSSimon Glass * usb_show_tree() - show the USB device tree 107045bfa47eSSimon Glass * 107145bfa47eSSimon Glass * This shows a list of active USB devices along with basic information about 107245bfa47eSSimon Glass * each. 107345bfa47eSSimon Glass */ 107445bfa47eSSimon Glass void usb_show_tree(void); 107545bfa47eSSimon Glass 1076012771d8Swdenk #endif /*_USB_H_ */ 1077