1012771d8Swdenk /* 2012771d8Swdenk * (C) Copyright 2001 3012771d8Swdenk * Denis Peter, MPL AG Switzerland 4012771d8Swdenk * 5de31213fSSimon Glass * Adapted for U-Boot driver model 6de31213fSSimon Glass * (C) Copyright 2015 Google, Inc 7de31213fSSimon Glass * 81a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 9012771d8Swdenk * Note: Part of this code has been derived from linux 10012771d8Swdenk * 11012771d8Swdenk */ 12012771d8Swdenk #ifndef _USB_H_ 13012771d8Swdenk #define _USB_H_ 14012771d8Swdenk 15de31213fSSimon Glass #include <fdtdec.h> 16012771d8Swdenk #include <usb_defs.h> 17c60795f4SIlya Yanok #include <linux/usb/ch9.h> 18a8c2ebcfSMasahiro Yamada #include <asm/cache.h> 19a8c2ebcfSMasahiro Yamada #include <part.h> 20012771d8Swdenk 2171c5de4fSTom Rini /* 2271c5de4fSTom Rini * The EHCI spec says that we must align to at least 32 bytes. However, 2371c5de4fSTom Rini * some platforms require larger alignment. 2471c5de4fSTom Rini */ 2571c5de4fSTom Rini #if ARCH_DMA_MINALIGN > 32 2671c5de4fSTom Rini #define USB_DMA_MINALIGN ARCH_DMA_MINALIGN 2771c5de4fSTom Rini #else 2871c5de4fSTom Rini #define USB_DMA_MINALIGN 32 2971c5de4fSTom Rini #endif 3071c5de4fSTom Rini 31012771d8Swdenk /* Everything is aribtrary */ 32012771d8Swdenk #define USB_ALTSETTINGALLOC 4 33012771d8Swdenk #define USB_MAXALTSETTING 128 /* Hard limit */ 34012771d8Swdenk 35012771d8Swdenk #define USB_MAX_DEVICE 32 36012771d8Swdenk #define USB_MAXCONFIG 8 37012771d8Swdenk #define USB_MAXINTERFACES 8 38012771d8Swdenk #define USB_MAXENDPOINTS 16 39012771d8Swdenk #define USB_MAXCHILDREN 8 /* This is arbitrary */ 40012771d8Swdenk #define USB_MAX_HUB 16 41012771d8Swdenk 42012771d8Swdenk #define USB_CNTL_TIMEOUT 100 /* 100ms timeout */ 43012771d8Swdenk 4496820a35SSimon Glass /* 4596820a35SSimon Glass * This is the timeout to allow for submitting an urb in ms. We allow more 4696820a35SSimon Glass * time for a BULK device to react - some are slow. 4796820a35SSimon Glass */ 4880b350a7SJason Cooper #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000) 4996820a35SSimon Glass 50012771d8Swdenk /* device request (setup) */ 51012771d8Swdenk struct devrequest { 52b12242acSSergey Temerkhanov __u8 requesttype; 53b12242acSSergey Temerkhanov __u8 request; 54b12242acSSergey Temerkhanov __le16 value; 55b12242acSSergey Temerkhanov __le16 index; 56b12242acSSergey Temerkhanov __le16 length; 57012771d8Swdenk } __attribute__ ((packed)); 58012771d8Swdenk 598f8bd565STom Rix /* Interface */ 608f8bd565STom Rix struct usb_interface { 618f8bd565STom Rix struct usb_interface_descriptor desc; 62012771d8Swdenk 63b12242acSSergey Temerkhanov __u8 no_of_ep; 64b12242acSSergey Temerkhanov __u8 num_altsetting; 65b12242acSSergey Temerkhanov __u8 act_altsetting; 66de39f8c1SMichael Trimarchi 67012771d8Swdenk struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS]; 686497c667SVivek Gautam /* 696497c667SVivek Gautam * Super Speed Device will have Super Speed Endpoint 706497c667SVivek Gautam * Companion Descriptor (section 9.6.7 of usb 3.0 spec) 716497c667SVivek Gautam * Revision 1.0 June 6th 2011 726497c667SVivek Gautam */ 736497c667SVivek Gautam struct usb_ss_ep_comp_descriptor ss_ep_comp_desc[USB_MAXENDPOINTS]; 74012771d8Swdenk } __attribute__ ((packed)); 75012771d8Swdenk 768f8bd565STom Rix /* Configuration information.. */ 778f8bd565STom Rix struct usb_config { 78c60795f4SIlya Yanok struct usb_config_descriptor desc; 79012771d8Swdenk 80b12242acSSergey Temerkhanov __u8 no_of_if; /* number of interfaces */ 818f8bd565STom Rix struct usb_interface if_desc[USB_MAXINTERFACES]; 82012771d8Swdenk } __attribute__ ((packed)); 83012771d8Swdenk 8448867208SRemy Bohmer enum { 8548867208SRemy Bohmer /* Maximum packet size; encoded as 0,1,2,3 = 8,16,32,64 */ 8648867208SRemy Bohmer PACKET_SIZE_8 = 0, 8748867208SRemy Bohmer PACKET_SIZE_16 = 1, 8848867208SRemy Bohmer PACKET_SIZE_32 = 2, 8948867208SRemy Bohmer PACKET_SIZE_64 = 3, 9048867208SRemy Bohmer }; 91012771d8Swdenk 92de31213fSSimon Glass /** 93de31213fSSimon Glass * struct usb_device - information about a USB device 94de31213fSSimon Glass * 95de31213fSSimon Glass * With driver model both UCLASS_USB (the USB controllers) and UCLASS_USB_HUB 96de31213fSSimon Glass * (the hubs) have this as parent data. Hubs are children of controllers or 97de31213fSSimon Glass * other hubs and there is always a single root hub for each controller. 98de31213fSSimon Glass * Therefore struct usb_device can always be accessed with 99de31213fSSimon Glass * dev_get_parentdata(dev), where dev is a USB device. 100de31213fSSimon Glass * 101de31213fSSimon Glass * Pointers exist for obtaining both the device (could be any uclass) and 102de31213fSSimon Glass * controller (UCLASS_USB) from this structure. The controller does not have 103de31213fSSimon Glass * a struct usb_device since it is not a device. 104de31213fSSimon Glass */ 105012771d8Swdenk struct usb_device { 106012771d8Swdenk int devnum; /* Device number on USB bus */ 1073e126484SMichael Trimarchi int speed; /* full/low/high */ 108012771d8Swdenk char mf[32]; /* manufacturer */ 109012771d8Swdenk char prod[32]; /* product */ 110012771d8Swdenk char serial[32]; /* serial number */ 111012771d8Swdenk 11248867208SRemy Bohmer /* Maximum packet size; one of: PACKET_SIZE_* */ 11348867208SRemy Bohmer int maxpacketsize; 11448867208SRemy Bohmer /* one bit for each endpoint ([0] = IN, [1] = OUT) */ 11548867208SRemy Bohmer unsigned int toggle[2]; 116de39f8c1SMichael Trimarchi /* endpoint halts; one bit per endpoint # & direction; 117de39f8c1SMichael Trimarchi * [0] = IN, [1] = OUT 118de39f8c1SMichael Trimarchi */ 11948867208SRemy Bohmer unsigned int halted[2]; 120012771d8Swdenk int epmaxpacketin[16]; /* INput endpoint specific maximums */ 121012771d8Swdenk int epmaxpacketout[16]; /* OUTput endpoint specific maximums */ 122012771d8Swdenk 123012771d8Swdenk int configno; /* selected config number */ 124f5766139SPuneet Saxena /* Device Descriptor */ 125f5766139SPuneet Saxena struct usb_device_descriptor descriptor 126f5766139SPuneet Saxena __attribute__((aligned(ARCH_DMA_MINALIGN))); 1278f8bd565STom Rix struct usb_config config; /* config descriptor */ 128012771d8Swdenk 129012771d8Swdenk int have_langid; /* whether string_langid is valid yet */ 130012771d8Swdenk int string_langid; /* language ID for strings */ 131012771d8Swdenk int (*irq_handle)(struct usb_device *dev); 132012771d8Swdenk unsigned long irq_status; 133012771d8Swdenk int irq_act_len; /* transfered bytes */ 134012771d8Swdenk void *privptr; 135012771d8Swdenk /* 136012771d8Swdenk * Child devices - if this is a hub device 137012771d8Swdenk * Each instance needs its own set of data structures. 138012771d8Swdenk */ 139012771d8Swdenk unsigned long status; 140904f2a83SHans de Goede unsigned long int_pending; /* 1 bit per ep, used by int_queue */ 141012771d8Swdenk int act_len; /* transfered bytes */ 142012771d8Swdenk int maxchild; /* Number of ports if hub */ 143de31213fSSimon Glass int portnr; /* Port number, 1=first */ 144de31213fSSimon Glass #ifndef CONFIG_DM_USB 145de31213fSSimon Glass /* parent hub, or NULL if this is the root hub */ 146012771d8Swdenk struct usb_device *parent; 147012771d8Swdenk struct usb_device *children[USB_MAXCHILDREN]; 148c7e3b2b5SLucas Stach void *controller; /* hardware controller private data */ 149de31213fSSimon Glass #endif 1505853e133SVivek Gautam /* slot_id - for xHCI enabled devices */ 1515853e133SVivek Gautam unsigned int slot_id; 152de31213fSSimon Glass #ifdef CONFIG_DM_USB 153de31213fSSimon Glass struct udevice *dev; /* Pointer to associated device */ 154de31213fSSimon Glass struct udevice *controller_dev; /* Pointer to associated controller */ 155de31213fSSimon Glass #endif 156012771d8Swdenk }; 157012771d8Swdenk 1588460b89aSHans de Goede struct int_queue; 1598460b89aSHans de Goede 160bba67914STroy Kisky /* 161bba67914STroy Kisky * You can initialize platform's USB host or device 162bba67914STroy Kisky * ports by passing this enum as an argument to 163bba67914STroy Kisky * board_usb_init(). 164bba67914STroy Kisky */ 165bba67914STroy Kisky enum usb_init_type { 166bba67914STroy Kisky USB_INIT_HOST, 167bba67914STroy Kisky USB_INIT_DEVICE 168bba67914STroy Kisky }; 169bba67914STroy Kisky 170012771d8Swdenk /********************************************************************** 171012771d8Swdenk * this is how the lowlevel part communicate with the outer world 172012771d8Swdenk */ 173012771d8Swdenk 174822af351SRodolfo Giometti #if defined(CONFIG_USB_UHCI) || defined(CONFIG_USB_OHCI) || \ 17551ab142bSmichael defined(CONFIG_USB_EHCI) || defined(CONFIG_USB_OHCI_NEW) || \ 1763e126484SMichael Trimarchi defined(CONFIG_USB_SL811HS) || defined(CONFIG_USB_ISP116X_HCD) || \ 177f298e4b6STom Rix defined(CONFIG_USB_R8A66597_HCD) || defined(CONFIG_USB_DAVINCI) || \ 178e608f221SBryan Wu defined(CONFIG_USB_OMAP3) || defined(CONFIG_USB_DA8XX) || \ 17937931f02SIlya Yanok defined(CONFIG_USB_BLACKFIN) || defined(CONFIG_USB_AM35X) || \ 180673a524bSIlya Yanok defined(CONFIG_USB_MUSB_DSPS) || defined(CONFIG_USB_MUSB_AM35X) || \ 18128a15ef7SHans de Goede defined(CONFIG_USB_MUSB_OMAP2PLUS) || defined(CONFIG_USB_MUSB_SUNXI) || \ 18228a15ef7SHans de Goede defined(CONFIG_USB_XHCI) || defined(CONFIG_USB_DWC2) 183822af351SRodolfo Giometti 18406d513ecSTroy Kisky int usb_lowlevel_init(int index, enum usb_init_type init, void **controller); 185c7e3b2b5SLucas Stach int usb_lowlevel_stop(int index); 186de31213fSSimon Glass 187de31213fSSimon Glass #if defined(CONFIG_MUSB_HOST) || defined(CONFIG_DM_USB) 188de31213fSSimon Glass int usb_reset_root_port(void); 18990cdc103SHans de Goede #else 19090cdc103SHans de Goede #define usb_reset_root_port() 19190cdc103SHans de Goede #endif 192c7e3b2b5SLucas Stach 193de39f8c1SMichael Trimarchi int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, 194de39f8c1SMichael Trimarchi void *buffer, int transfer_len); 195012771d8Swdenk int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, 196012771d8Swdenk int transfer_len, struct devrequest *setup); 197012771d8Swdenk int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer, 198012771d8Swdenk int transfer_len, int interval); 199012771d8Swdenk 200904f2a83SHans de Goede #if defined CONFIG_USB_EHCI || defined CONFIG_MUSB_HOST 2018460b89aSHans de Goede struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe, 2028bb6c1d1SHans de Goede int queuesize, int elementsize, void *buffer, int interval); 2038460b89aSHans de Goede int destroy_int_queue(struct usb_device *dev, struct int_queue *queue); 2048460b89aSHans de Goede void *poll_int_queue(struct usb_device *dev, struct int_queue *queue); 2058460b89aSHans de Goede #endif 2068460b89aSHans de Goede 207012771d8Swdenk /* Defines */ 208012771d8Swdenk #define USB_UHCI_VEND_ID 0x8086 209012771d8Swdenk #define USB_UHCI_DEV_ID 0x7112 210012771d8Swdenk 211e5f24753SLukasz Dalek /* 212e5f24753SLukasz Dalek * PXA25x can only act as USB device. There are drivers 213e5f24753SLukasz Dalek * which works with USB CDC gadgets implementations. 214e5f24753SLukasz Dalek * Some of them have common routines which can be used 215e5f24753SLukasz Dalek * in boards init functions e.g. udc_disconnect() used for 216e5f24753SLukasz Dalek * forced device disconnection from host. 217e5f24753SLukasz Dalek */ 218e5f24753SLukasz Dalek #elif defined(CONFIG_USB_GADGET_PXA2XX) 219e5f24753SLukasz Dalek 220e5f24753SLukasz Dalek extern void udc_disconnect(void); 221e5f24753SLukasz Dalek 222012771d8Swdenk #endif 223012771d8Swdenk 22416297cfbSMateusz Zalega /* 22516297cfbSMateusz Zalega * board-specific hardware initialization, called by 22616297cfbSMateusz Zalega * usb drivers and u-boot commands 22716297cfbSMateusz Zalega * 22816297cfbSMateusz Zalega * @param index USB controller number 22916297cfbSMateusz Zalega * @param init initializes controller as USB host or device 23016297cfbSMateusz Zalega */ 231bba67914STroy Kisky int board_usb_init(int index, enum usb_init_type init); 23216297cfbSMateusz Zalega 23316297cfbSMateusz Zalega /* 23416297cfbSMateusz Zalega * can be used to clean up after failed USB initialization attempt 23516297cfbSMateusz Zalega * vide: board_usb_init() 23616297cfbSMateusz Zalega * 23716297cfbSMateusz Zalega * @param index USB controller number for selective cleanup 238bba67914STroy Kisky * @param init usb_init_type passed to board_usb_init() 23916297cfbSMateusz Zalega */ 240bba67914STroy Kisky int board_usb_cleanup(int index, enum usb_init_type init); 24116297cfbSMateusz Zalega 242012771d8Swdenk #ifdef CONFIG_USB_STORAGE 243012771d8Swdenk 244012771d8Swdenk #define USB_MAX_STOR_DEV 5 245012771d8Swdenk block_dev_desc_t *usb_stor_get_dev(int index); 246012771d8Swdenk int usb_stor_scan(int mode); 247e813eae3SAnatolij Gustschin int usb_stor_info(void); 248012771d8Swdenk 249012771d8Swdenk #endif 250012771d8Swdenk 25189d48367SSimon Glass #ifdef CONFIG_USB_HOST_ETHER 25289d48367SSimon Glass 25389d48367SSimon Glass #define USB_MAX_ETH_DEV 5 25489d48367SSimon Glass int usb_host_eth_scan(int mode); 25589d48367SSimon Glass 25689d48367SSimon Glass #endif 25789d48367SSimon Glass 258012771d8Swdenk #ifdef CONFIG_USB_KEYBOARD 259012771d8Swdenk 260012771d8Swdenk int drv_usb_kbd_init(void); 2618a8a2257SHans de Goede int usb_kbd_deregister(int force); 262012771d8Swdenk 263012771d8Swdenk #endif 264012771d8Swdenk /* routines */ 265012771d8Swdenk int usb_init(void); /* initialize the USB Controller */ 266012771d8Swdenk int usb_stop(void); /* stop the USB Controller */ 267012771d8Swdenk 268012771d8Swdenk 269012771d8Swdenk int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol); 270de39f8c1SMichael Trimarchi int usb_set_idle(struct usb_device *dev, int ifnum, int duration, 271de39f8c1SMichael Trimarchi int report_id); 272012771d8Swdenk int usb_control_msg(struct usb_device *dev, unsigned int pipe, 273012771d8Swdenk unsigned char request, unsigned char requesttype, 274012771d8Swdenk unsigned short value, unsigned short index, 275012771d8Swdenk void *data, unsigned short size, int timeout); 276012771d8Swdenk int usb_bulk_msg(struct usb_device *dev, unsigned int pipe, 277012771d8Swdenk void *data, int len, int *actual_length, int timeout); 278012771d8Swdenk int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe, 279012771d8Swdenk void *buffer, int transfer_len, int interval); 28089d48367SSimon Glass int usb_disable_asynch(int disable); 281012771d8Swdenk int usb_maxpacket(struct usb_device *dev, unsigned long pipe); 282de39f8c1SMichael Trimarchi int usb_get_configuration_no(struct usb_device *dev, unsigned char *buffer, 283de39f8c1SMichael Trimarchi int cfgno); 284de39f8c1SMichael Trimarchi int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type, 285de39f8c1SMichael Trimarchi unsigned char id, void *buf, int size); 286012771d8Swdenk int usb_get_class_descriptor(struct usb_device *dev, int ifnum, 287de39f8c1SMichael Trimarchi unsigned char type, unsigned char id, void *buf, 288de39f8c1SMichael Trimarchi int size); 289012771d8Swdenk int usb_clear_halt(struct usb_device *dev, int pipe); 290012771d8Swdenk int usb_string(struct usb_device *dev, int index, char *buf, size_t size); 291012771d8Swdenk int usb_set_interface(struct usb_device *dev, int interface, int alternate); 292012771d8Swdenk 293012771d8Swdenk /* big endian -> little endian conversion */ 294149dded2Swdenk /* some CPUs are already little endian e.g. the ARM920T */ 295ae3b770eSMarkus Klotzbuecher #define __swap_16(x) \ 2963f85ce27Swdenk ({ unsigned short x_ = (unsigned short)x; \ 2973f85ce27Swdenk (unsigned short)( \ 2983f85ce27Swdenk ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \ 2993f85ce27Swdenk }) 300ae3b770eSMarkus Klotzbuecher #define __swap_32(x) \ 3013f85ce27Swdenk ({ unsigned long x_ = (unsigned long)x; \ 3023f85ce27Swdenk (unsigned long)( \ 3033f85ce27Swdenk ((x_ & 0x000000FFUL) << 24) | \ 3043f85ce27Swdenk ((x_ & 0x0000FF00UL) << 8) | \ 3053f85ce27Swdenk ((x_ & 0x00FF0000UL) >> 8) | \ 3063f85ce27Swdenk ((x_ & 0xFF000000UL) >> 24)); \ 3073f85ce27Swdenk }) 308ae3b770eSMarkus Klotzbuecher 309c7d703f3SMike Frysinger #ifdef __LITTLE_ENDIAN 310ae3b770eSMarkus Klotzbuecher # define swap_16(x) (x) 311ae3b770eSMarkus Klotzbuecher # define swap_32(x) (x) 312ae3b770eSMarkus Klotzbuecher #else 313ae3b770eSMarkus Klotzbuecher # define swap_16(x) __swap_16(x) 314ae3b770eSMarkus Klotzbuecher # define swap_32(x) __swap_32(x) 315c7d703f3SMike Frysinger #endif 316012771d8Swdenk 317012771d8Swdenk /* 318012771d8Swdenk * Calling this entity a "pipe" is glorifying it. A USB pipe 319012771d8Swdenk * is something embarrassingly simple: it basically consists 320012771d8Swdenk * of the following information: 321012771d8Swdenk * - device number (7 bits) 322012771d8Swdenk * - endpoint number (4 bits) 323012771d8Swdenk * - current Data0/1 state (1 bit) 324012771d8Swdenk * - direction (1 bit) 3253e126484SMichael Trimarchi * - speed (2 bits) 326012771d8Swdenk * - max packet size (2 bits: 8, 16, 32 or 64) 327012771d8Swdenk * - pipe type (2 bits: control, interrupt, bulk, isochronous) 328012771d8Swdenk * 329012771d8Swdenk * That's 18 bits. Really. Nothing more. And the USB people have 330012771d8Swdenk * documented these eighteen bits as some kind of glorious 331012771d8Swdenk * virtual data structure. 332012771d8Swdenk * 333012771d8Swdenk * Let's not fall in that trap. We'll just encode it as a simple 334012771d8Swdenk * unsigned int. The encoding is: 335012771d8Swdenk * 336012771d8Swdenk * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64) 337de39f8c1SMichael Trimarchi * - direction: bit 7 (0 = Host-to-Device [Out], 338de39f8c1SMichael Trimarchi * (1 = Device-to-Host [In]) 339012771d8Swdenk * - device: bits 8-14 340012771d8Swdenk * - endpoint: bits 15-18 341012771d8Swdenk * - Data0/1: bit 19 342de39f8c1SMichael Trimarchi * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 343de39f8c1SMichael Trimarchi * 10 = control, 11 = bulk) 344012771d8Swdenk * 345012771d8Swdenk * Why? Because it's arbitrary, and whatever encoding we select is really 346012771d8Swdenk * up to us. This one happens to share a lot of bit positions with the UHCI 347012771d8Swdenk * specification, so that much of the uhci driver can just mask the bits 348012771d8Swdenk * appropriately. 349012771d8Swdenk */ 350012771d8Swdenk /* Create various pipes... */ 351012771d8Swdenk #define create_pipe(dev,endpoint) \ 352d0fe1128SSergei Shtylyov (((dev)->devnum << 8) | ((endpoint) << 15) | \ 353c60795f4SIlya Yanok (dev)->maxpacketsize) 3543e126484SMichael Trimarchi #define default_pipe(dev) ((dev)->speed << 26) 355012771d8Swdenk 356de39f8c1SMichael Trimarchi #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \ 357de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 358de39f8c1SMichael Trimarchi #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \ 359de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 360de39f8c1SMichael Trimarchi USB_DIR_IN) 361de39f8c1SMichael Trimarchi #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \ 362de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 363de39f8c1SMichael Trimarchi #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \ 364de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 365de39f8c1SMichael Trimarchi USB_DIR_IN) 366de39f8c1SMichael Trimarchi #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \ 367de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 368de39f8c1SMichael Trimarchi #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \ 369de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 370de39f8c1SMichael Trimarchi USB_DIR_IN) 371de39f8c1SMichael Trimarchi #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \ 372de39f8c1SMichael Trimarchi create_pipe(dev, endpoint)) 373de39f8c1SMichael Trimarchi #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \ 374de39f8c1SMichael Trimarchi create_pipe(dev, endpoint) | \ 375de39f8c1SMichael Trimarchi USB_DIR_IN) 376de39f8c1SMichael Trimarchi #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \ 377de39f8c1SMichael Trimarchi default_pipe(dev)) 378de39f8c1SMichael Trimarchi #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \ 379de39f8c1SMichael Trimarchi default_pipe(dev) | \ 380de39f8c1SMichael Trimarchi USB_DIR_IN) 381012771d8Swdenk 382012771d8Swdenk /* The D0/D1 toggle bits */ 383012771d8Swdenk #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1) 384012771d8Swdenk #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep)) 385de39f8c1SMichael Trimarchi #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \ 386de39f8c1SMichael Trimarchi ((dev)->toggle[out] & \ 387de39f8c1SMichael Trimarchi ~(1 << ep)) | ((bit) << ep)) 388012771d8Swdenk 389012771d8Swdenk /* Endpoint halt control/status */ 390012771d8Swdenk #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1) 391012771d8Swdenk #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep))) 392012771d8Swdenk #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep))) 393012771d8Swdenk #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep))) 394012771d8Swdenk 395de39f8c1SMichael Trimarchi #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \ 396de39f8c1SMichael Trimarchi USB_PID_OUT) 397012771d8Swdenk 398012771d8Swdenk #define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1) 399012771d8Swdenk #define usb_pipein(pipe) (((pipe) >> 7) & 1) 400012771d8Swdenk #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f) 401012771d8Swdenk #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff) 402012771d8Swdenk #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf) 403012771d8Swdenk #define usb_pipedata(pipe) (((pipe) >> 19) & 1) 404012771d8Swdenk #define usb_pipetype(pipe) (((pipe) >> 30) & 3) 405012771d8Swdenk #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS) 406012771d8Swdenk #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT) 407012771d8Swdenk #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL) 408012771d8Swdenk #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK) 409012771d8Swdenk 4105853e133SVivek Gautam #define usb_pipe_ep_index(pipe) \ 4115853e133SVivek Gautam usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \ 4125853e133SVivek Gautam ((usb_pipeendpoint(pipe) * 2) - \ 4135853e133SVivek Gautam (usb_pipein(pipe) ? 0 : 1)) 414012771d8Swdenk 415012771d8Swdenk /************************************************************************* 416012771d8Swdenk * Hub Stuff 417012771d8Swdenk */ 418012771d8Swdenk struct usb_port_status { 419012771d8Swdenk unsigned short wPortStatus; 420012771d8Swdenk unsigned short wPortChange; 421012771d8Swdenk } __attribute__ ((packed)); 422012771d8Swdenk 423012771d8Swdenk struct usb_hub_status { 424012771d8Swdenk unsigned short wHubStatus; 425012771d8Swdenk unsigned short wHubChange; 426012771d8Swdenk } __attribute__ ((packed)); 427012771d8Swdenk 428012771d8Swdenk 429012771d8Swdenk /* Hub descriptor */ 430012771d8Swdenk struct usb_hub_descriptor { 431012771d8Swdenk unsigned char bLength; 432012771d8Swdenk unsigned char bDescriptorType; 433012771d8Swdenk unsigned char bNbrPorts; 434012771d8Swdenk unsigned short wHubCharacteristics; 435012771d8Swdenk unsigned char bPwrOn2PwrGood; 436012771d8Swdenk unsigned char bHubContrCurrent; 437012771d8Swdenk unsigned char DeviceRemovable[(USB_MAXCHILDREN+1+7)/8]; 438012771d8Swdenk unsigned char PortPowerCtrlMask[(USB_MAXCHILDREN+1+7)/8]; 439012771d8Swdenk /* DeviceRemovable and PortPwrCtrlMask want to be variable-length 440012771d8Swdenk bitmaps that hold max 255 entries. (bit0 is ignored) */ 441012771d8Swdenk } __attribute__ ((packed)); 442012771d8Swdenk 443012771d8Swdenk 444012771d8Swdenk struct usb_hub_device { 445012771d8Swdenk struct usb_device *pusb_dev; 446012771d8Swdenk struct usb_hub_descriptor desc; 447012771d8Swdenk }; 448012771d8Swdenk 449de31213fSSimon Glass #ifdef CONFIG_DM_USB 450de31213fSSimon Glass /** 451de31213fSSimon Glass * struct usb_platdata - Platform data about a USB controller 452de31213fSSimon Glass * 453de31213fSSimon Glass * Given a USB controller (UCLASS_USB) dev this is dev_get_platdata(dev) 454de31213fSSimon Glass */ 455de31213fSSimon Glass struct usb_platdata { 456de31213fSSimon Glass enum usb_init_type init_type; 457de31213fSSimon Glass }; 458de31213fSSimon Glass 459de31213fSSimon Glass /** 460de31213fSSimon Glass * struct usb_dev_platdata - Platform data about a USB device 461de31213fSSimon Glass * 462de31213fSSimon Glass * Given a USB device dev this structure is dev_get_parent_platdata(dev). 463de31213fSSimon Glass * This is used by sandbox to provide emulation data also. 464de31213fSSimon Glass * 465de31213fSSimon Glass * @id: ID used to match this device 466de31213fSSimon Glass * @speed: Stores the speed associated with a USB device 467de31213fSSimon Glass * @devnum: Device address on the USB bus 468de31213fSSimon Glass * @slot_id: USB3 slot ID, which is separate from the device address 469de31213fSSimon Glass * @portnr: Port number of this device on its parent hub, numbered from 1 470de31213fSSimon Glass * (0 mean this device is the root hub) 471de31213fSSimon Glass * @strings: List of descriptor strings (for sandbox emulation purposes) 472de31213fSSimon Glass * @desc_list: List of descriptors (for sandbox emulation purposes) 473de31213fSSimon Glass */ 474de31213fSSimon Glass struct usb_dev_platdata { 475de31213fSSimon Glass struct usb_device_id id; 476de31213fSSimon Glass enum usb_device_speed speed; 477de31213fSSimon Glass int devnum; 478de31213fSSimon Glass int slot_id; 479de31213fSSimon Glass int portnr; /* Hub port number, 1..n */ 480de31213fSSimon Glass #ifdef CONFIG_SANDBOX 481de31213fSSimon Glass struct usb_string *strings; 482de31213fSSimon Glass /* NULL-terminated list of descriptor pointers */ 483de31213fSSimon Glass struct usb_generic_descriptor **desc_list; 484de31213fSSimon Glass #endif 485de31213fSSimon Glass int configno; 486de31213fSSimon Glass }; 487de31213fSSimon Glass 488de31213fSSimon Glass /** 489de31213fSSimon Glass * struct usb_bus_priv - information about the USB controller 490de31213fSSimon Glass * 491de31213fSSimon Glass * Given a USB controller (UCLASS_USB) 'dev', this is 492de31213fSSimon Glass * dev_get_uclass_priv(dev). 493de31213fSSimon Glass * 494de31213fSSimon Glass * @next_addr: Next device address to allocate minus 1. Incremented by 1 495de31213fSSimon Glass * each time a new device address is set, so this holds the 496de31213fSSimon Glass * number of devices on the bus 497de31213fSSimon Glass * @desc_before_addr: true if we can read a device descriptor before it 498de31213fSSimon Glass * has been assigned an address. For XHCI this is not possible 499de31213fSSimon Glass * so this will be false. 500de31213fSSimon Glass */ 501de31213fSSimon Glass struct usb_bus_priv { 502de31213fSSimon Glass int next_addr; 503de31213fSSimon Glass bool desc_before_addr; 504de31213fSSimon Glass }; 505de31213fSSimon Glass 506de31213fSSimon Glass /** 507de31213fSSimon Glass * struct dm_usb_ops - USB controller operations 508de31213fSSimon Glass * 509de31213fSSimon Glass * This defines the operations supoorted on a USB controller. Common 510de31213fSSimon Glass * arguments are: 511de31213fSSimon Glass * 512de31213fSSimon Glass * @bus: USB bus (i.e. controller), which is in UCLASS_USB. 513de31213fSSimon Glass * @udev: USB device parent data. Controllers are not expected to need 514de31213fSSimon Glass * this, since the device address on the bus is encoded in @pipe. 515de31213fSSimon Glass * It is used for sandbox, and can be handy for debugging and 516de31213fSSimon Glass * logging. 517de31213fSSimon Glass * @pipe: An assortment of bitfields which provide address and packet 518de31213fSSimon Glass * type information. See create_pipe() above for encoding 519de31213fSSimon Glass * details 520de31213fSSimon Glass * @buffer: A buffer to use for sending/receiving. This should be 521de31213fSSimon Glass * DMA-aligned. 522de31213fSSimon Glass * @length: Buffer length in bytes 523de31213fSSimon Glass */ 524de31213fSSimon Glass struct dm_usb_ops { 525de31213fSSimon Glass /** 526de31213fSSimon Glass * control() - Send a control message 527de31213fSSimon Glass * 528de31213fSSimon Glass * Most parameters are as above. 529de31213fSSimon Glass * 530de31213fSSimon Glass * @setup: Additional setup information required by the message 531de31213fSSimon Glass */ 532de31213fSSimon Glass int (*control)(struct udevice *bus, struct usb_device *udev, 533de31213fSSimon Glass unsigned long pipe, void *buffer, int length, 534de31213fSSimon Glass struct devrequest *setup); 535de31213fSSimon Glass /** 536de31213fSSimon Glass * bulk() - Send a bulk message 537de31213fSSimon Glass * 538de31213fSSimon Glass * Parameters are as above. 539de31213fSSimon Glass */ 540de31213fSSimon Glass int (*bulk)(struct udevice *bus, struct usb_device *udev, 541de31213fSSimon Glass unsigned long pipe, void *buffer, int length); 542de31213fSSimon Glass /** 543de31213fSSimon Glass * interrupt() - Send an interrupt message 544de31213fSSimon Glass * 545de31213fSSimon Glass * Most parameters are as above. 546de31213fSSimon Glass * 547de31213fSSimon Glass * @interval: Interrupt interval 548de31213fSSimon Glass */ 549de31213fSSimon Glass int (*interrupt)(struct udevice *bus, struct usb_device *udev, 550de31213fSSimon Glass unsigned long pipe, void *buffer, int length, 551de31213fSSimon Glass int interval); 552de31213fSSimon Glass /** 553de31213fSSimon Glass * alloc_device() - Allocate a new device context (XHCI) 554de31213fSSimon Glass * 555de31213fSSimon Glass * Before sending packets to a new device on an XHCI bus, a device 556de31213fSSimon Glass * context must be created. If this method is not NULL it will be 557de31213fSSimon Glass * called before the device is enumerated (even before its descriptor 558de31213fSSimon Glass * is read). This should be NULL for EHCI, which does not need this. 559de31213fSSimon Glass */ 560de31213fSSimon Glass int (*alloc_device)(struct udevice *bus, struct usb_device *udev); 561de31213fSSimon Glass }; 562de31213fSSimon Glass 563de31213fSSimon Glass #define usb_get_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops) 564de31213fSSimon Glass #define usb_get_emul_ops(dev) ((struct dm_usb_ops *)(dev)->driver->ops) 565de31213fSSimon Glass 566de31213fSSimon Glass #ifdef CONFIG_MUSB_HOST 567de31213fSSimon Glass int usb_reset_root_port(void); 568de31213fSSimon Glass #endif 569de31213fSSimon Glass 570de31213fSSimon Glass /** 571de31213fSSimon Glass * usb_get_dev_index() - look up a device index number 572de31213fSSimon Glass * 573de31213fSSimon Glass * Look up devices using their index number (starting at 0). This works since 574de31213fSSimon Glass * in U-Boot device addresses are allocated starting at 1 with no gaps. 575de31213fSSimon Glass * 576de31213fSSimon Glass * TODO(sjg@chromium.org): Remove this function when usb_ether.c is modified 577de31213fSSimon Glass * to work better with driver model. 578de31213fSSimon Glass * 579de31213fSSimon Glass * @bus: USB bus to check 580de31213fSSimon Glass * @index: Index number of device to find (0=first). This is just the 581de31213fSSimon Glass * device address less 1. 582de31213fSSimon Glass */ 583de31213fSSimon Glass struct usb_device *usb_get_dev_index(struct udevice *bus, int index); 584de31213fSSimon Glass 585de31213fSSimon Glass /** 586de31213fSSimon Glass * usb_legacy_port_reset() - Legacy function to reset a hub port 587de31213fSSimon Glass * 588de31213fSSimon Glass * @hub: Hub device 589de31213fSSimon Glass * @portnr: Port number (1=first) 590de31213fSSimon Glass */ 591de31213fSSimon Glass int usb_legacy_port_reset(struct usb_device *hub, int portnr); 592de31213fSSimon Glass 593de31213fSSimon Glass /** 594de31213fSSimon Glass * usb_setup_device() - set up a device ready for use 595de31213fSSimon Glass * 596de31213fSSimon Glass * @dev: USB device pointer. This need not be a real device - it is 597de31213fSSimon Glass * common for it to just be a local variable with its ->dev 598de31213fSSimon Glass * member (i.e. @dev->dev) set to the parent device 599de31213fSSimon Glass * @do_read: true to read the device descriptor before an address is set 600de31213fSSimon Glass * (should be false for XHCI buses, true otherwise) 601de31213fSSimon Glass * @parent: Parent device (either UCLASS_USB or UCLASS_USB_HUB) 602de31213fSSimon Glass * @portnr: Port number on hub (1=first) or 0 for none 603de31213fSSimon Glass * @return 0 if OK, -ve on error */ 604de31213fSSimon Glass int usb_setup_device(struct usb_device *dev, bool do_read, 605de31213fSSimon Glass struct usb_device *parent, int portnr); 606de31213fSSimon Glass 607de31213fSSimon Glass /** 608de31213fSSimon Glass * usb_hub_scan() - Scan a hub and find its devices 609de31213fSSimon Glass * 610de31213fSSimon Glass * @hub: Hub device to scan 611de31213fSSimon Glass */ 612de31213fSSimon Glass int usb_hub_scan(struct udevice *hub); 613de31213fSSimon Glass 614de31213fSSimon Glass /** 615de31213fSSimon Glass * usb_scan_device() - Scan a device on a bus 616de31213fSSimon Glass * 617de31213fSSimon Glass * Scan a device on a bus. It has already been detected and is ready to 618de31213fSSimon Glass * be enumerated. This may be either the root hub (@parent is a bus) or a 619de31213fSSimon Glass * normal device (@parent is a hub) 620de31213fSSimon Glass * 621de31213fSSimon Glass * @parent: Parent device 622de31213fSSimon Glass * @port: Hub port number (numbered from 1) 623de31213fSSimon Glass * @speed: USB speed to use for this device 624de31213fSSimon Glass * @devp: Returns pointer to device if all is well 625de31213fSSimon Glass * @return 0 if OK, -ve on error 626de31213fSSimon Glass */ 627de31213fSSimon Glass int usb_scan_device(struct udevice *parent, int port, 628de31213fSSimon Glass enum usb_device_speed speed, struct udevice **devp); 629de31213fSSimon Glass 630de31213fSSimon Glass /** 631de31213fSSimon Glass * usb_get_bus() - Find the bus for a device 632de31213fSSimon Glass * 633de31213fSSimon Glass * Search up through parents to find the bus this device is connected to. This 634de31213fSSimon Glass * will be a device with uclass UCLASS_USB. 635de31213fSSimon Glass * 636de31213fSSimon Glass * @dev: Device to check 637de31213fSSimon Glass * @busp: Returns bus, or NULL if not found 638de31213fSSimon Glass * @return 0 if OK, -EXDEV is somehow this bus does not have a controller (this 639de31213fSSimon Glass * indicates a critical error in the USB stack 640de31213fSSimon Glass */ 641de31213fSSimon Glass int usb_get_bus(struct udevice *dev, struct udevice **busp); 642de31213fSSimon Glass 643de31213fSSimon Glass /** 644de31213fSSimon Glass * usb_select_config() - Set up a device ready for use 645de31213fSSimon Glass * 646de31213fSSimon Glass * This function assumes that the device already has an address and a driver 647de31213fSSimon Glass * bound, and is ready to be set up. 648de31213fSSimon Glass * 649de31213fSSimon Glass * This re-reads the device and configuration descriptors and sets the 650de31213fSSimon Glass * configuration 651de31213fSSimon Glass * 652de31213fSSimon Glass * @dev: Device to set up 653de31213fSSimon Glass */ 654de31213fSSimon Glass int usb_select_config(struct usb_device *dev); 655de31213fSSimon Glass 656de31213fSSimon Glass /** 657de31213fSSimon Glass * usb_child_pre_probe() - Pre-probe function for USB devices 658de31213fSSimon Glass * 659de31213fSSimon Glass * This is called on all children of hubs and USB controllers (i.e. UCLASS_USB 660de31213fSSimon Glass * and UCLASS_USB_HUB) when a new device is about to be probed. It sets up the 661de31213fSSimon Glass * device from the saved platform data and calls usb_select_config() to 662de31213fSSimon Glass * finish set up. 663de31213fSSimon Glass * 664de31213fSSimon Glass * Once this is done, the device's normal driver can take over, knowing the 665de31213fSSimon Glass * device is accessible on the USB bus. 666de31213fSSimon Glass * 667de31213fSSimon Glass * This function is for use only by the internal USB stack. 668de31213fSSimon Glass * 669de31213fSSimon Glass * @dev: Device to set up 670de31213fSSimon Glass */ 671de31213fSSimon Glass int usb_child_pre_probe(struct udevice *dev); 672de31213fSSimon Glass 673de31213fSSimon Glass struct ehci_ctrl; 674de31213fSSimon Glass 675de31213fSSimon Glass /** 676de31213fSSimon Glass * usb_setup_ehci_gadget() - Set up a USB device as a gadget 677de31213fSSimon Glass * 678de31213fSSimon Glass * TODO(sjg@chromium.org): Tidy this up when USB gadgets can use driver model 679de31213fSSimon Glass * 680de31213fSSimon Glass * This provides a way to tell a controller to start up as a USB device 681de31213fSSimon Glass * instead of as a host. It is untested. 682de31213fSSimon Glass */ 683de31213fSSimon Glass int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp); 684de31213fSSimon Glass 685de31213fSSimon Glass /** 686de31213fSSimon Glass * usb_stor_reset() - Prepare to scan USB storage devices 687de31213fSSimon Glass * 688de31213fSSimon Glass * Empty the list of USB storage devices in preparation for scanning them. 689de31213fSSimon Glass * This must be called before a USB scan. 690de31213fSSimon Glass */ 691de31213fSSimon Glass void usb_stor_reset(void); 692de31213fSSimon Glass 693de31213fSSimon Glass #else /* !CONFIG_DM_USB */ 694de31213fSSimon Glass 695de31213fSSimon Glass struct usb_device *usb_get_dev_index(int index); 696de31213fSSimon Glass 697de31213fSSimon Glass #endif 698de31213fSSimon Glass 699de31213fSSimon Glass bool usb_device_has_child_on_port(struct usb_device *parent, int port); 700de31213fSSimon Glass 70123faf2bcSMarek Vasut int usb_hub_probe(struct usb_device *dev, int ifnum); 70223faf2bcSMarek Vasut void usb_hub_reset(void); 703*862e75c0SSimon Glass 704*862e75c0SSimon Glass /** 705*862e75c0SSimon Glass * legacy_hub_port_reset() - reset a port given its usb_device pointer 706*862e75c0SSimon Glass * 707*862e75c0SSimon Glass * Reset a hub port and see if a device is present on that port, providing 708*862e75c0SSimon Glass * sufficient time for it to show itself. The port status is returned. 709*862e75c0SSimon Glass * 710*862e75c0SSimon Glass * With driver model this moves to hub_port_reset() and is passed a struct 711*862e75c0SSimon Glass * udevice. 712*862e75c0SSimon Glass * 713*862e75c0SSimon Glass * @dev: USB device to reset 714*862e75c0SSimon Glass * @port: Port number to reset (note ports are numbered from 0 here) 715*862e75c0SSimon Glass * @portstat: Returns port status 716*862e75c0SSimon Glass */ 717*862e75c0SSimon Glass int legacy_hub_port_reset(struct usb_device *dev, int port, 71823faf2bcSMarek Vasut unsigned short *portstat); 71923faf2bcSMarek Vasut 720*862e75c0SSimon Glass int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat); 721*862e75c0SSimon Glass 72279b58887SSimon Glass /** 72379b58887SSimon Glass * usb_alloc_new_device() - Allocate a new device 72479b58887SSimon Glass * 72579b58887SSimon Glass * @devp: returns a pointer of a new device structure. With driver model this 72679b58887SSimon Glass * is a device pointer, but with legacy USB this pointer is 72779b58887SSimon Glass * driver-specific. 72879b58887SSimon Glass * @return 0 if OK, -ENOSPC if we have found out of room for new devices 72979b58887SSimon Glass */ 73079b58887SSimon Glass int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp); 73179b58887SSimon Glass 73279b58887SSimon Glass /** 73379b58887SSimon Glass * usb_free_device() - Free a partially-inited device 73479b58887SSimon Glass * 73579b58887SSimon Glass * This is an internal function. It is used to reverse the action of 73679b58887SSimon Glass * usb_alloc_new_device() when we hit a problem during init. 73779b58887SSimon Glass */ 73879b58887SSimon Glass void usb_free_device(struct udevice *controller); 739c7e3b2b5SLucas Stach 74023faf2bcSMarek Vasut int usb_new_device(struct usb_device *dev); 74179b58887SSimon Glass 7425853e133SVivek Gautam int usb_alloc_device(struct usb_device *dev); 74323faf2bcSMarek Vasut 744012771d8Swdenk #endif /*_USB_H_ */ 745