Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 1 | #ifndef QEMU_USB_H |
| 2 | #define QEMU_USB_H |
| 3 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 4 | /* |
| 5 | * QEMU USB API |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6 | * |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 7 | * Copyright (c) 2005 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 8 | * |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 27 | |
Markus Armbruster | d484205 | 2019-08-12 07:23:46 +0200 | [diff] [blame] | 28 | #include "exec/memory.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 29 | #include "hw/qdev-core.h" |
Markus Armbruster | daf015e | 2016-03-09 10:52:44 +0100 | [diff] [blame] | 30 | #include "qemu/iov.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 31 | #include "qemu/queue.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 32 | #include "qom/object.h" |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 33 | |
Brad Hards | 8e25781 | 2011-06-02 11:18:46 +1000 | [diff] [blame] | 34 | /* Constants related to the USB / PCI interaction */ |
| 35 | #define USB_SBRN 0x60 /* Serial Bus Release Number Register */ |
| 36 | #define USB_RELEASE_1 0x10 /* USB 1.0 */ |
| 37 | #define USB_RELEASE_2 0x20 /* USB 2.0 */ |
| 38 | #define USB_RELEASE_3 0x30 /* USB 3.0 */ |
| 39 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 40 | #define USB_TOKEN_SETUP 0x2d |
| 41 | #define USB_TOKEN_IN 0x69 /* device -> host */ |
| 42 | #define USB_TOKEN_OUT 0xe1 /* host -> device */ |
| 43 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 44 | #define USB_RET_SUCCESS (0) |
Hans de Goede | 36dfe32 | 2012-10-24 18:14:07 +0200 | [diff] [blame] | 45 | #define USB_RET_NODEV (-1) |
| 46 | #define USB_RET_NAK (-2) |
| 47 | #define USB_RET_STALL (-3) |
| 48 | #define USB_RET_BABBLE (-4) |
| 49 | #define USB_RET_IOERROR (-5) |
| 50 | #define USB_RET_ASYNC (-6) |
| 51 | #define USB_RET_ADD_TO_QUEUE (-7) |
Hans de Goede | 0cae7b1 | 2012-10-24 18:14:08 +0200 | [diff] [blame] | 52 | #define USB_RET_REMOVE_FROM_QUEUE (-8) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 53 | |
| 54 | #define USB_SPEED_LOW 0 |
| 55 | #define USB_SPEED_FULL 1 |
| 56 | #define USB_SPEED_HIGH 2 |
Gerd Hoffmann | 843d4e0 | 2010-12-03 17:30:13 +0100 | [diff] [blame] | 57 | #define USB_SPEED_SUPER 3 |
| 58 | |
| 59 | #define USB_SPEED_MASK_LOW (1 << USB_SPEED_LOW) |
| 60 | #define USB_SPEED_MASK_FULL (1 << USB_SPEED_FULL) |
| 61 | #define USB_SPEED_MASK_HIGH (1 << USB_SPEED_HIGH) |
| 62 | #define USB_SPEED_MASK_SUPER (1 << USB_SPEED_SUPER) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 63 | |
| 64 | #define USB_STATE_NOTATTACHED 0 |
| 65 | #define USB_STATE_ATTACHED 1 |
| 66 | //#define USB_STATE_POWERED 2 |
| 67 | #define USB_STATE_DEFAULT 3 |
| 68 | //#define USB_STATE_ADDRESS 4 |
| 69 | //#define USB_STATE_CONFIGURED 5 |
| 70 | #define USB_STATE_SUSPENDED 6 |
| 71 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 72 | #define USB_CLASS_AUDIO 1 |
| 73 | #define USB_CLASS_COMM 2 |
| 74 | #define USB_CLASS_HID 3 |
| 75 | #define USB_CLASS_PHYSICAL 5 |
| 76 | #define USB_CLASS_STILL_IMAGE 6 |
| 77 | #define USB_CLASS_PRINTER 7 |
| 78 | #define USB_CLASS_MASS_STORAGE 8 |
| 79 | #define USB_CLASS_HUB 9 |
| 80 | #define USB_CLASS_CDC_DATA 0x0a |
| 81 | #define USB_CLASS_CSCID 0x0b |
| 82 | #define USB_CLASS_CONTENT_SEC 0x0d |
| 83 | #define USB_CLASS_APP_SPEC 0xfe |
| 84 | #define USB_CLASS_VENDOR_SPEC 0xff |
| 85 | |
H. Peter Anvin | b870472 | 2010-09-10 14:47:56 -0700 | [diff] [blame] | 86 | #define USB_SUBCLASS_UNDEFINED 0 |
| 87 | #define USB_SUBCLASS_AUDIO_CONTROL 1 |
| 88 | #define USB_SUBCLASS_AUDIO_STREAMING 2 |
| 89 | #define USB_SUBCLASS_AUDIO_MIDISTREAMING 3 |
| 90 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 91 | #define USB_DIR_OUT 0 |
| 92 | #define USB_DIR_IN 0x80 |
| 93 | |
| 94 | #define USB_TYPE_MASK (0x03 << 5) |
| 95 | #define USB_TYPE_STANDARD (0x00 << 5) |
| 96 | #define USB_TYPE_CLASS (0x01 << 5) |
| 97 | #define USB_TYPE_VENDOR (0x02 << 5) |
| 98 | #define USB_TYPE_RESERVED (0x03 << 5) |
| 99 | |
| 100 | #define USB_RECIP_MASK 0x1f |
| 101 | #define USB_RECIP_DEVICE 0x00 |
| 102 | #define USB_RECIP_INTERFACE 0x01 |
| 103 | #define USB_RECIP_ENDPOINT 0x02 |
| 104 | #define USB_RECIP_OTHER 0x03 |
| 105 | |
| 106 | #define DeviceRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) |
| 107 | #define DeviceOutRequest ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8) |
Gerd Hoffmann | 2b81ba5 | 2013-11-19 12:37:58 +0100 | [diff] [blame] | 108 | #define VendorDeviceRequest ((USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8) |
| 109 | #define VendorDeviceOutRequest \ |
| 110 | ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8) |
| 111 | |
| 112 | #define InterfaceRequest \ |
bellard | 59ae540 | 2005-11-05 16:57:08 +0000 | [diff] [blame] | 113 | ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8) |
| 114 | #define InterfaceOutRequest \ |
| 115 | ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8) |
Max Reitz | f3571b1 | 2010-03-14 12:19:03 +0100 | [diff] [blame] | 116 | #define ClassInterfaceRequest \ |
| 117 | ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)<<8) |
| 118 | #define ClassInterfaceOutRequest \ |
| 119 | ((USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE)<<8) |
Gerd Hoffmann | 2b81ba5 | 2013-11-19 12:37:58 +0100 | [diff] [blame] | 120 | #define VendorInterfaceRequest \ |
| 121 | ((USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE)<<8) |
| 122 | #define VendorInterfaceOutRequest \ |
| 123 | ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE)<<8) |
| 124 | |
| 125 | #define EndpointRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8) |
| 126 | #define EndpointOutRequest \ |
| 127 | ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8) |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 128 | |
| 129 | #define USB_REQ_GET_STATUS 0x00 |
| 130 | #define USB_REQ_CLEAR_FEATURE 0x01 |
| 131 | #define USB_REQ_SET_FEATURE 0x03 |
| 132 | #define USB_REQ_SET_ADDRESS 0x05 |
| 133 | #define USB_REQ_GET_DESCRIPTOR 0x06 |
| 134 | #define USB_REQ_SET_DESCRIPTOR 0x07 |
| 135 | #define USB_REQ_GET_CONFIGURATION 0x08 |
| 136 | #define USB_REQ_SET_CONFIGURATION 0x09 |
| 137 | #define USB_REQ_GET_INTERFACE 0x0A |
| 138 | #define USB_REQ_SET_INTERFACE 0x0B |
| 139 | #define USB_REQ_SYNCH_FRAME 0x0C |
Gerd Hoffmann | 811ad5d | 2017-01-31 14:52:07 +0100 | [diff] [blame] | 140 | #define USB_REQ_SET_SEL 0x30 |
| 141 | #define USB_REQ_SET_ISOCH_DELAY 0x31 |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 142 | |
| 143 | #define USB_DEVICE_SELF_POWERED 0 |
| 144 | #define USB_DEVICE_REMOTE_WAKEUP 1 |
| 145 | |
| 146 | #define USB_DT_DEVICE 0x01 |
| 147 | #define USB_DT_CONFIG 0x02 |
| 148 | #define USB_DT_STRING 0x03 |
| 149 | #define USB_DT_INTERFACE 0x04 |
| 150 | #define USB_DT_ENDPOINT 0x05 |
Gerd Hoffmann | 25620cb | 2010-12-08 17:35:22 +0100 | [diff] [blame] | 151 | #define USB_DT_DEVICE_QUALIFIER 0x06 |
| 152 | #define USB_DT_OTHER_SPEED_CONFIG 0x07 |
Gerd Hoffmann | a7fb71d | 2011-06-23 17:15:43 +0200 | [diff] [blame] | 153 | #define USB_DT_DEBUG 0x0A |
Brad Hards | c6d3ad0 | 2011-04-03 15:33:18 +1000 | [diff] [blame] | 154 | #define USB_DT_INTERFACE_ASSOC 0x0B |
Gerd Hoffmann | 2077469 | 2012-08-28 17:46:29 +0200 | [diff] [blame] | 155 | #define USB_DT_BOS 0x0F |
| 156 | #define USB_DT_DEVICE_CAPABILITY 0x10 |
H. Peter Anvin | b870472 | 2010-09-10 14:47:56 -0700 | [diff] [blame] | 157 | #define USB_DT_CS_INTERFACE 0x24 |
| 158 | #define USB_DT_CS_ENDPOINT 0x25 |
Gerd Hoffmann | b43a285 | 2012-08-28 17:28:03 +0200 | [diff] [blame] | 159 | #define USB_DT_ENDPOINT_COMPANION 0x30 |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 160 | |
Gerd Hoffmann | 2077469 | 2012-08-28 17:46:29 +0200 | [diff] [blame] | 161 | #define USB_DEV_CAP_WIRELESS 0x01 |
| 162 | #define USB_DEV_CAP_USB2_EXT 0x02 |
| 163 | #define USB_DEV_CAP_SUPERSPEED 0x03 |
| 164 | |
Pantelis Koukousoulas | bd93976 | 2013-12-16 09:42:49 +0200 | [diff] [blame] | 165 | #define USB_CFG_ATT_ONE (1 << 7) /* should always be set */ |
| 166 | #define USB_CFG_ATT_SELFPOWER (1 << 6) |
| 167 | #define USB_CFG_ATT_WAKEUP (1 << 5) |
| 168 | #define USB_CFG_ATT_BATTERY (1 << 4) |
| 169 | |
balrog | 942ac05 | 2008-04-22 03:15:10 +0000 | [diff] [blame] | 170 | #define USB_ENDPOINT_XFER_CONTROL 0 |
| 171 | #define USB_ENDPOINT_XFER_ISOC 1 |
| 172 | #define USB_ENDPOINT_XFER_BULK 2 |
| 173 | #define USB_ENDPOINT_XFER_INT 3 |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 174 | #define USB_ENDPOINT_XFER_INVALID 255 |
balrog | 942ac05 | 2008-04-22 03:15:10 +0000 | [diff] [blame] | 175 | |
Gerd Hoffmann | 7c37e6a | 2012-07-03 10:15:08 +0200 | [diff] [blame] | 176 | #define USB_INTERFACE_INVALID 255 |
| 177 | |
Gerd Hoffmann | 07771f6 | 2011-05-23 17:37:12 +0200 | [diff] [blame] | 178 | typedef struct USBBusOps USBBusOps; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 179 | typedef struct USBPort USBPort; |
| 180 | typedef struct USBDevice USBDevice; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 181 | typedef struct USBPacket USBPacket; |
Hans de Goede | a552a96 | 2012-10-31 13:47:09 +0100 | [diff] [blame] | 182 | typedef struct USBCombinedPacket USBCombinedPacket; |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 183 | typedef struct USBEndpoint USBEndpoint; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 184 | |
Gerd Hoffmann | 37fb59d | 2010-11-17 11:03:53 +0100 | [diff] [blame] | 185 | typedef struct USBDesc USBDesc; |
| 186 | typedef struct USBDescID USBDescID; |
| 187 | typedef struct USBDescDevice USBDescDevice; |
| 188 | typedef struct USBDescConfig USBDescConfig; |
Brad Hards | 6e625fc | 2011-04-03 15:33:21 +1000 | [diff] [blame] | 189 | typedef struct USBDescIfaceAssoc USBDescIfaceAssoc; |
Gerd Hoffmann | 37fb59d | 2010-11-17 11:03:53 +0100 | [diff] [blame] | 190 | typedef struct USBDescIface USBDescIface; |
| 191 | typedef struct USBDescEndpoint USBDescEndpoint; |
| 192 | typedef struct USBDescOther USBDescOther; |
Gerd Hoffmann | 132a3f5 | 2010-11-26 12:25:32 +0100 | [diff] [blame] | 193 | typedef struct USBDescString USBDescString; |
Gerd Hoffmann | 5319dc7 | 2013-11-20 07:32:31 +0100 | [diff] [blame] | 194 | typedef struct USBDescMSOS USBDescMSOS; |
Gerd Hoffmann | 132a3f5 | 2010-11-26 12:25:32 +0100 | [diff] [blame] | 195 | |
| 196 | struct USBDescString { |
| 197 | uint8_t index; |
| 198 | char *str; |
| 199 | QLIST_ENTRY(USBDescString) next; |
| 200 | }; |
Gerd Hoffmann | 37fb59d | 2010-11-17 11:03:53 +0100 | [diff] [blame] | 201 | |
Gerd Hoffmann | 1de14d4 | 2011-08-30 13:21:27 +0200 | [diff] [blame] | 202 | #define USB_MAX_ENDPOINTS 15 |
| 203 | #define USB_MAX_INTERFACES 16 |
| 204 | |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 205 | struct USBEndpoint { |
Gerd Hoffmann | 63095ab | 2012-01-12 13:24:22 +0100 | [diff] [blame] | 206 | uint8_t nr; |
| 207 | uint8_t pid; |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 208 | uint8_t type; |
Gerd Hoffmann | 82f02fe | 2011-08-29 12:57:48 +0200 | [diff] [blame] | 209 | uint8_t ifnum; |
Gerd Hoffmann | f003397 | 2011-08-31 16:09:27 +0200 | [diff] [blame] | 210 | int max_packet_size; |
Hans de Goede | 04b300f | 2013-11-19 14:36:56 +0100 | [diff] [blame] | 211 | int max_streams; |
Gerd Hoffmann | 7936e0f | 2012-03-01 14:39:28 +0100 | [diff] [blame] | 212 | bool pipeline; |
Hans de Goede | 0132b4b | 2012-08-17 15:24:49 +0200 | [diff] [blame] | 213 | bool halted; |
Gerd Hoffmann | 25d5de7 | 2011-12-13 15:58:19 +0100 | [diff] [blame] | 214 | USBDevice *dev; |
Gerd Hoffmann | db4be87 | 2012-01-12 14:26:13 +0100 | [diff] [blame] | 215 | QTAILQ_HEAD(, USBPacket) queue; |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 216 | }; |
| 217 | |
Gerd Hoffmann | eeb0cf9 | 2011-08-22 09:09:51 +0200 | [diff] [blame] | 218 | enum USBDeviceFlags { |
| 219 | USB_DEV_FLAG_FULL_PATH, |
Hans de Goede | be41efd | 2012-11-17 12:47:15 +0100 | [diff] [blame] | 220 | USB_DEV_FLAG_IS_HOST, |
Gerd Hoffmann | 5319dc7 | 2013-11-20 07:32:31 +0100 | [diff] [blame] | 221 | USB_DEV_FLAG_MSOS_DESC_ENABLE, |
| 222 | USB_DEV_FLAG_MSOS_DESC_IN_USE, |
Gerd Hoffmann | eeb0cf9 | 2011-08-22 09:09:51 +0200 | [diff] [blame] | 223 | }; |
| 224 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 225 | /* definition of a USB device */ |
| 226 | struct USBDevice { |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 227 | DeviceState qdev; |
Gerd Hoffmann | 618c169 | 2010-12-01 11:27:05 +0100 | [diff] [blame] | 228 | USBPort *port; |
Gerd Hoffmann | 5f69076 | 2010-12-10 11:43:35 +0100 | [diff] [blame] | 229 | char *port_path; |
Gerd Hoffmann | 71938a0 | 2013-06-12 13:01:49 +0200 | [diff] [blame] | 230 | char *serial; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 231 | void *opaque; |
Gerd Hoffmann | eeb0cf9 | 2011-08-22 09:09:51 +0200 | [diff] [blame] | 232 | uint32_t flags; |
aliguori | 89b9b79 | 2008-08-21 19:29:38 +0000 | [diff] [blame] | 233 | |
Hans de Goede | ba3f9bf | 2011-05-27 14:27:18 +0200 | [diff] [blame] | 234 | /* Actual connected speed */ |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 235 | int speed; |
Hans de Goede | ba3f9bf | 2011-05-27 14:27:18 +0200 | [diff] [blame] | 236 | /* Supported speeds, not in info because it may be variable (hostdevs) */ |
| 237 | int speedmask; |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 238 | uint8_t addr; |
Markus Armbruster | 0fe6d12 | 2009-12-09 17:07:51 +0100 | [diff] [blame] | 239 | char product_desc[32]; |
Gerd Hoffmann | 61e094c | 2009-10-26 15:56:48 +0100 | [diff] [blame] | 240 | int auto_attach; |
Gerd Hoffmann | eb19d2b | 2016-06-15 11:46:56 +0200 | [diff] [blame] | 241 | bool attached; |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 242 | |
Gerd Hoffmann | c1ecb40 | 2010-12-10 14:20:46 +0100 | [diff] [blame] | 243 | int32_t state; |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 244 | uint8_t setup_buf[8]; |
Hans de Goede | 19f3322 | 2011-02-02 17:46:00 +0100 | [diff] [blame] | 245 | uint8_t data_buf[4096]; |
Gerd Hoffmann | c1ecb40 | 2010-12-10 14:20:46 +0100 | [diff] [blame] | 246 | int32_t remote_wakeup; |
| 247 | int32_t setup_state; |
| 248 | int32_t setup_len; |
| 249 | int32_t setup_index; |
Gerd Hoffmann | 132a3f5 | 2010-11-26 12:25:32 +0100 | [diff] [blame] | 250 | |
Gerd Hoffmann | 25d5de7 | 2011-12-13 15:58:19 +0100 | [diff] [blame] | 251 | USBEndpoint ep_ctl; |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 252 | USBEndpoint ep_in[USB_MAX_ENDPOINTS]; |
| 253 | USBEndpoint ep_out[USB_MAX_ENDPOINTS]; |
| 254 | |
Gerd Hoffmann | 132a3f5 | 2010-11-26 12:25:32 +0100 | [diff] [blame] | 255 | QLIST_HEAD(, USBDescString) strings; |
Hans de Goede | 386ab48 | 2012-11-17 12:47:16 +0100 | [diff] [blame] | 256 | const USBDesc *usb_desc; /* Overrides class usb_desc if not NULL */ |
Gerd Hoffmann | a980a06 | 2010-11-26 20:20:41 +0100 | [diff] [blame] | 257 | const USBDescDevice *device; |
Gerd Hoffmann | 6536051 | 2011-08-30 11:11:29 +0200 | [diff] [blame] | 258 | |
| 259 | int configuration; |
| 260 | int ninterfaces; |
Gerd Hoffmann | 1de14d4 | 2011-08-30 13:21:27 +0200 | [diff] [blame] | 261 | int altsetting[USB_MAX_INTERFACES]; |
Gerd Hoffmann | a980a06 | 2010-11-26 20:20:41 +0100 | [diff] [blame] | 262 | const USBDescConfig *config; |
Gerd Hoffmann | 1de14d4 | 2011-08-30 13:21:27 +0200 | [diff] [blame] | 263 | const USBDescIface *ifaces[USB_MAX_INTERFACES]; |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 266 | #define TYPE_USB_DEVICE "usb-device" |
Eduardo Habkost | a489d19 | 2020-09-16 14:25:18 -0400 | [diff] [blame] | 267 | OBJECT_DECLARE_TYPE(USBDevice, USBDeviceClass, USB_DEVICE) |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 268 | |
Gonglei | 7d553f2 | 2014-09-19 14:48:24 +0800 | [diff] [blame] | 269 | typedef void (*USBDeviceRealize)(USBDevice *dev, Error **errp); |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 270 | typedef void (*USBDeviceUnrealize)(USBDevice *dev); |
Gonglei | 7d553f2 | 2014-09-19 14:48:24 +0800 | [diff] [blame] | 271 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 272 | struct USBDeviceClass { |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 273 | DeviceClass parent_class; |
| 274 | |
Gonglei | 7d553f2 | 2014-09-19 14:48:24 +0800 | [diff] [blame] | 275 | USBDeviceRealize realize; |
| 276 | USBDeviceUnrealize unrealize; |
| 277 | |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 278 | /* |
Gerd Hoffmann | 73796fe | 2012-01-10 16:59:28 +0100 | [diff] [blame] | 279 | * Walk (enabled) downstream ports, check for a matching device. |
| 280 | * Only hubs implement this. |
| 281 | */ |
| 282 | USBDevice *(*find_device)(USBDevice *dev, uint8_t addr); |
| 283 | |
| 284 | /* |
Gerd Hoffmann | eb5e680 | 2011-05-16 10:34:53 +0200 | [diff] [blame] | 285 | * Called when a packet is canceled. |
| 286 | */ |
| 287 | void (*cancel_packet)(USBDevice *dev, USBPacket *p); |
| 288 | |
| 289 | /* |
Gerd Hoffmann | b6f77fb | 2010-12-03 17:59:36 +0100 | [diff] [blame] | 290 | * Attach the device |
| 291 | */ |
| 292 | void (*handle_attach)(USBDevice *dev); |
| 293 | |
| 294 | /* |
aliguori | 89b9b79 | 2008-08-21 19:29:38 +0000 | [diff] [blame] | 295 | * Reset the device |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 296 | */ |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 297 | void (*handle_reset)(USBDevice *dev); |
aliguori | 89b9b79 | 2008-08-21 19:29:38 +0000 | [diff] [blame] | 298 | |
| 299 | /* |
| 300 | * Process control request. |
| 301 | * Called from handle_packet(). |
| 302 | * |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 303 | * Status gets stored in p->status, and if p->status == USB_RET_SUCCESS |
Stefan Weil | 993d46c | 2012-11-23 07:26:04 +0100 | [diff] [blame] | 304 | * then the number of bytes transferred is stored in p->actual_length |
aliguori | 89b9b79 | 2008-08-21 19:29:38 +0000 | [diff] [blame] | 305 | */ |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 306 | void (*handle_control)(USBDevice *dev, USBPacket *p, int request, int value, |
| 307 | int index, int length, uint8_t *data); |
aliguori | 89b9b79 | 2008-08-21 19:29:38 +0000 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Process data transfers (both BULK and ISOC). |
| 311 | * Called from handle_packet(). |
| 312 | * |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 313 | * Status gets stored in p->status, and if p->status == USB_RET_SUCCESS |
Stefan Weil | 993d46c | 2012-11-23 07:26:04 +0100 | [diff] [blame] | 314 | * then the number of bytes transferred is stored in p->actual_length |
aliguori | 89b9b79 | 2008-08-21 19:29:38 +0000 | [diff] [blame] | 315 | */ |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 316 | void (*handle_data)(USBDevice *dev, USBPacket *p); |
Gerd Hoffmann | 0958b4c | 2009-10-26 15:56:45 +0100 | [diff] [blame] | 317 | |
Gerd Hoffmann | 1de14d4 | 2011-08-30 13:21:27 +0200 | [diff] [blame] | 318 | void (*set_interface)(USBDevice *dev, int interface, |
| 319 | int alt_old, int alt_new); |
| 320 | |
Hans de Goede | 36dfe32 | 2012-10-24 18:14:07 +0200 | [diff] [blame] | 321 | /* |
| 322 | * Called when the hcd is done queuing packets for an endpoint, only |
| 323 | * necessary for devices which can return USB_RET_ADD_TO_QUEUE. |
| 324 | */ |
| 325 | void (*flush_ep_queue)(USBDevice *dev, USBEndpoint *ep); |
| 326 | |
Hans de Goede | f79738b | 2012-12-14 14:35:40 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Called by the hcd to let the device know the queue for an endpoint |
| 329 | * has been unlinked / stopped. Optional may be NULL. |
| 330 | */ |
| 331 | void (*ep_stopped)(USBDevice *dev, USBEndpoint *ep); |
| 332 | |
Hans de Goede | 3b444ea | 2013-11-19 14:36:57 +0100 | [diff] [blame] | 333 | /* |
| 334 | * Called by the hcd to alloc / free streams on a bulk endpoint. |
| 335 | * Optional may be NULL. |
| 336 | */ |
| 337 | int (*alloc_streams)(USBDevice *dev, USBEndpoint **eps, int nr_eps, |
| 338 | int streams); |
| 339 | void (*free_streams)(USBDevice *dev, USBEndpoint **eps, int nr_eps); |
| 340 | |
Markus Armbruster | 0638469 | 2009-12-09 17:07:52 +0100 | [diff] [blame] | 341 | const char *product_desc; |
Gerd Hoffmann | 37fb59d | 2010-11-17 11:03:53 +0100 | [diff] [blame] | 342 | const USBDesc *usb_desc; |
Gerd Hoffmann | 1e351dc | 2016-06-15 11:46:57 +0200 | [diff] [blame] | 343 | bool attached_settable; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 344 | }; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 345 | |
Gerd Hoffmann | 0d86d2b | 2010-12-01 11:08:44 +0100 | [diff] [blame] | 346 | typedef struct USBPortOps { |
Gerd Hoffmann | 618c169 | 2010-12-01 11:27:05 +0100 | [diff] [blame] | 347 | void (*attach)(USBPort *port); |
| 348 | void (*detach)(USBPort *port); |
Hans de Goede | 4706ab6 | 2011-06-24 12:31:11 +0200 | [diff] [blame] | 349 | /* |
| 350 | * This gets called when a device downstream from the device attached to |
| 351 | * the port (iow attached through a hub) gets detached. |
| 352 | */ |
| 353 | void (*child_detach)(USBPort *port, USBDevice *child); |
Hans de Goede | d47e59b | 2011-06-21 11:52:28 +0200 | [diff] [blame] | 354 | void (*wakeup)(USBPort *port); |
| 355 | /* |
| 356 | * Note that port->dev will be different then the device from which |
Gerd Hoffmann | f53c398 | 2012-01-12 12:51:48 +0100 | [diff] [blame] | 357 | * the packet originated when a hub is involved. |
Hans de Goede | d47e59b | 2011-06-21 11:52:28 +0200 | [diff] [blame] | 358 | */ |
| 359 | void (*complete)(USBPort *port, USBPacket *p); |
Gerd Hoffmann | 0d86d2b | 2010-12-01 11:08:44 +0100 | [diff] [blame] | 360 | } USBPortOps; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 361 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 362 | /* USB port on which a device can be connected */ |
| 363 | struct USBPort { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 364 | USBDevice *dev; |
Gerd Hoffmann | 843d4e0 | 2010-12-03 17:30:13 +0100 | [diff] [blame] | 365 | int speedmask; |
Gerd Hoffmann | c24e4aa | 2013-03-20 11:40:02 +0100 | [diff] [blame] | 366 | int hubcount; |
Gerd Hoffmann | c7a2196 | 2010-12-10 11:37:45 +0100 | [diff] [blame] | 367 | char path[16]; |
Gerd Hoffmann | 0d86d2b | 2010-12-01 11:08:44 +0100 | [diff] [blame] | 368 | USBPortOps *ops; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 369 | void *opaque; |
| 370 | int index; /* internal port index, may be used with the opaque */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 371 | QTAILQ_ENTRY(USBPort) next; |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 372 | }; |
| 373 | |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 374 | typedef void USBCallback(USBPacket * packet, void *opaque); |
| 375 | |
Gerd Hoffmann | f53c398 | 2012-01-12 12:51:48 +0100 | [diff] [blame] | 376 | typedef enum USBPacketState { |
| 377 | USB_PACKET_UNDEFINED = 0, |
| 378 | USB_PACKET_SETUP, |
Gerd Hoffmann | db4be87 | 2012-01-12 14:26:13 +0100 | [diff] [blame] | 379 | USB_PACKET_QUEUED, |
Gerd Hoffmann | f53c398 | 2012-01-12 12:51:48 +0100 | [diff] [blame] | 380 | USB_PACKET_ASYNC, |
| 381 | USB_PACKET_COMPLETE, |
| 382 | USB_PACKET_CANCELED, |
| 383 | } USBPacketState; |
| 384 | |
Gerd Hoffmann | db4be87 | 2012-01-12 14:26:13 +0100 | [diff] [blame] | 385 | /* Structure used to hold information about an active USB packet. */ |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 386 | struct USBPacket { |
| 387 | /* Data fields for use by the driver. */ |
| 388 | int pid; |
Gerd Hoffmann | e983395 | 2012-08-23 13:30:13 +0200 | [diff] [blame] | 389 | uint64_t id; |
Gerd Hoffmann | f53c398 | 2012-01-12 12:51:48 +0100 | [diff] [blame] | 390 | USBEndpoint *ep; |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 391 | unsigned int stream; |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 392 | QEMUIOVector iov; |
Gerd Hoffmann | 1b4b29a | 2012-03-02 13:22:29 +0100 | [diff] [blame] | 393 | uint64_t parameter; /* control transfers */ |
Hans de Goede | 6ba43f1 | 2012-10-24 18:14:09 +0200 | [diff] [blame] | 394 | bool short_not_ok; |
Hans de Goede | a6fb2dd | 2012-10-24 18:14:10 +0200 | [diff] [blame] | 395 | bool int_req; |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 396 | int status; /* USB_RET_* status code */ |
Stefan Weil | 993d46c | 2012-11-23 07:26:04 +0100 | [diff] [blame] | 397 | int actual_length; /* Number of bytes actually transferred */ |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 398 | /* Internal use by the USB layer. */ |
Gerd Hoffmann | f53c398 | 2012-01-12 12:51:48 +0100 | [diff] [blame] | 399 | USBPacketState state; |
Hans de Goede | a552a96 | 2012-10-31 13:47:09 +0100 | [diff] [blame] | 400 | USBCombinedPacket *combined; |
Gerd Hoffmann | db4be87 | 2012-01-12 14:26:13 +0100 | [diff] [blame] | 401 | QTAILQ_ENTRY(USBPacket) queue; |
Hans de Goede | a552a96 | 2012-10-31 13:47:09 +0100 | [diff] [blame] | 402 | QTAILQ_ENTRY(USBPacket) combined_entry; |
| 403 | }; |
| 404 | |
| 405 | struct USBCombinedPacket { |
| 406 | USBPacket *first; |
Paolo Bonzini | eae3eb3 | 2018-12-06 13:10:34 +0100 | [diff] [blame] | 407 | QTAILQ_HEAD(, USBPacket) packets; |
Hans de Goede | a552a96 | 2012-10-31 13:47:09 +0100 | [diff] [blame] | 408 | QEMUIOVector iov; |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 411 | void usb_packet_init(USBPacket *p); |
Gerd Hoffmann | db4be87 | 2012-01-12 14:26:13 +0100 | [diff] [blame] | 412 | void usb_packet_set_state(USBPacket *p, USBPacketState state); |
Gerd Hoffmann | 5ac2731 | 2012-03-08 12:27:47 +0100 | [diff] [blame] | 413 | void usb_packet_check_state(USBPacket *p, USBPacketState expected); |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 414 | void usb_packet_setup(USBPacket *p, int pid, |
| 415 | USBEndpoint *ep, unsigned int stream, |
| 416 | uint64_t id, bool short_not_ok, bool int_req); |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 417 | void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len); |
| 418 | int usb_packet_map(USBPacket *p, QEMUSGList *sgl); |
David Gibson | e2f8992 | 2012-06-27 14:50:42 +1000 | [diff] [blame] | 419 | void usb_packet_unmap(USBPacket *p, QEMUSGList *sgl); |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 420 | void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes); |
| 421 | void usb_packet_skip(USBPacket *p, size_t bytes); |
Gerd Hoffmann | 6a98d1c | 2013-01-24 15:38:23 +0100 | [diff] [blame] | 422 | size_t usb_packet_size(USBPacket *p); |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 423 | void usb_packet_cleanup(USBPacket *p); |
| 424 | |
Gerd Hoffmann | f53c398 | 2012-01-12 12:51:48 +0100 | [diff] [blame] | 425 | static inline bool usb_packet_is_inflight(USBPacket *p) |
| 426 | { |
Gerd Hoffmann | db4be87 | 2012-01-12 14:26:13 +0100 | [diff] [blame] | 427 | return (p->state == USB_PACKET_QUEUED || |
| 428 | p->state == USB_PACKET_ASYNC); |
Gerd Hoffmann | f53c398 | 2012-01-12 12:51:48 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Gerd Hoffmann | 73796fe | 2012-01-10 16:59:28 +0100 | [diff] [blame] | 431 | USBDevice *usb_find_device(USBPort *port, uint8_t addr); |
| 432 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 433 | void usb_handle_packet(USBDevice *dev, USBPacket *p); |
Gerd Hoffmann | 4ff658f | 2011-05-12 13:48:13 +0200 | [diff] [blame] | 434 | void usb_packet_complete(USBDevice *dev, USBPacket *p); |
Hans de Goede | d0ff81b | 2012-10-24 18:14:06 +0200 | [diff] [blame] | 435 | void usb_packet_complete_one(USBDevice *dev, USBPacket *p); |
Gerd Hoffmann | 4ff658f | 2011-05-12 13:48:13 +0200 | [diff] [blame] | 436 | void usb_cancel_packet(USBPacket * p); |
Gerd Hoffmann | 53aa8c0 | 2011-05-12 13:20:39 +0200 | [diff] [blame] | 437 | |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 438 | void usb_ep_init(USBDevice *dev); |
Gerd Hoffmann | 19deaa0 | 2012-07-03 10:11:21 +0200 | [diff] [blame] | 439 | void usb_ep_reset(USBDevice *dev); |
Gerd Hoffmann | 5b6780d | 2011-08-29 13:45:25 +0200 | [diff] [blame] | 440 | void usb_ep_dump(USBDevice *dev); |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 441 | struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep); |
| 442 | uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep); |
| 443 | void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type); |
Gerd Hoffmann | 82f02fe | 2011-08-29 12:57:48 +0200 | [diff] [blame] | 444 | void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum); |
Gerd Hoffmann | f003397 | 2011-08-31 16:09:27 +0200 | [diff] [blame] | 445 | void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep, |
| 446 | uint16_t raw); |
Hans de Goede | 04b300f | 2013-11-19 14:36:56 +0100 | [diff] [blame] | 447 | void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw); |
Gerd Hoffmann | e382d96 | 2012-12-12 13:40:59 +0100 | [diff] [blame] | 448 | void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted); |
Hans de Goede | c13a9e6 | 2012-08-28 09:43:18 +0200 | [diff] [blame] | 449 | USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep, |
| 450 | uint64_t id); |
Gerd Hoffmann | d8e17ef | 2011-08-29 12:49:46 +0200 | [diff] [blame] | 451 | |
Hans de Goede | a552a96 | 2012-10-31 13:47:09 +0100 | [diff] [blame] | 452 | void usb_ep_combine_input_packets(USBEndpoint *ep); |
| 453 | void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p); |
| 454 | void usb_combined_packet_cancel(USBDevice *dev, USBPacket *p); |
| 455 | |
Gerd Hoffmann | b791c3b | 2014-05-23 16:20:54 +0200 | [diff] [blame] | 456 | void usb_pick_speed(USBPort *port); |
Gerd Hoffmann | 891fb2c | 2011-09-01 13:56:37 +0200 | [diff] [blame] | 457 | void usb_attach(USBPort *port); |
| 458 | void usb_detach(USBPort *port); |
Gerd Hoffmann | d28f4e2 | 2012-01-06 15:23:10 +0100 | [diff] [blame] | 459 | void usb_port_reset(USBPort *port); |
| 460 | void usb_device_reset(USBDevice *dev); |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 461 | void usb_wakeup(USBEndpoint *ep, unsigned int stream); |
Hans de Goede | 50b7963 | 2011-02-02 17:36:29 +0100 | [diff] [blame] | 462 | void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p); |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 463 | |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 464 | /* usb-linux.c */ |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 465 | void hmp_info_usbhost(Monitor *mon, const QDict *qdict); |
Thomas Huth | b99260e | 2016-12-14 22:44:17 +0100 | [diff] [blame] | 466 | bool usb_host_dev_is_scsi_storage(USBDevice *usbdev); |
bellard | 59ae540 | 2005-11-05 16:57:08 +0000 | [diff] [blame] | 467 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 468 | /* usb ports of the VM */ |
| 469 | |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 470 | #define VM_USB_HUB_SIZE 8 |
| 471 | |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 472 | /* usb-bus.c */ |
| 473 | |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 474 | #define TYPE_USB_BUS "usb-bus" |
Eduardo Habkost | 8063396 | 2020-09-16 14:25:19 -0400 | [diff] [blame] | 475 | OBJECT_DECLARE_SIMPLE_TYPE(USBBus, USB_BUS) |
Anthony Liguori | 0d93692 | 2012-05-02 09:00:20 +0200 | [diff] [blame] | 476 | |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 477 | struct USBBus { |
| 478 | BusState qbus; |
Gerd Hoffmann | 07771f6 | 2011-05-23 17:37:12 +0200 | [diff] [blame] | 479 | USBBusOps *ops; |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 480 | int busnr; |
| 481 | int nfree; |
| 482 | int nused; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 483 | QTAILQ_HEAD(, USBPort) free; |
| 484 | QTAILQ_HEAD(, USBPort) used; |
| 485 | QTAILQ_ENTRY(USBBus) next; |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 486 | }; |
| 487 | |
Gerd Hoffmann | 07771f6 | 2011-05-23 17:37:12 +0200 | [diff] [blame] | 488 | struct USBBusOps { |
Markus Armbruster | f4bbaaf | 2015-02-17 14:28:02 +0100 | [diff] [blame] | 489 | void (*register_companion)(USBBus *bus, USBPort *ports[], |
| 490 | uint32_t portcount, uint32_t firstport, |
| 491 | Error **errp); |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 492 | void (*wakeup_endpoint)(USBBus *bus, USBEndpoint *ep, unsigned int stream); |
Gerd Hoffmann | 07771f6 | 2011-05-23 17:37:12 +0200 | [diff] [blame] | 493 | }; |
| 494 | |
Andreas Färber | c889b3a | 2013-08-23 20:32:04 +0200 | [diff] [blame] | 495 | void usb_bus_new(USBBus *bus, size_t bus_size, |
| 496 | USBBusOps *ops, DeviceState *host); |
Gonglei | e5a9bec | 2014-06-04 16:31:46 +0800 | [diff] [blame] | 497 | void usb_bus_release(USBBus *bus); |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 498 | USBBus *usb_bus_find(int busnr); |
Anthony Liguori | ba02430 | 2011-12-08 14:56:53 -0600 | [diff] [blame] | 499 | void usb_legacy_register(const char *typename, const char *usbdevice_name, |
Markus Armbruster | 590ce74 | 2020-06-10 07:32:16 +0200 | [diff] [blame] | 500 | USBDevice *(*usbdevice_init)(const char *params)); |
Markus Armbruster | 32aaaeb | 2020-06-10 07:32:15 +0200 | [diff] [blame] | 501 | USBDevice *usb_new(const char *name); |
| 502 | bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **errp); |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 503 | USBDevice *usb_create_simple(USBBus *bus, const char *name); |
Gerd Hoffmann | 0958b4c | 2009-10-26 15:56:45 +0100 | [diff] [blame] | 504 | USBDevice *usbdevice_create(const char *cmdline); |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 505 | void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, |
Gerd Hoffmann | ace1318 | 2011-01-12 11:34:50 +0100 | [diff] [blame] | 506 | USBPortOps *ops, int speedmask); |
Markus Armbruster | f4bbaaf | 2015-02-17 14:28:02 +0100 | [diff] [blame] | 507 | void usb_register_companion(const char *masterbus, USBPort *ports[], |
| 508 | uint32_t portcount, uint32_t firstport, |
| 509 | void *opaque, USBPortOps *ops, int speedmask, |
| 510 | Error **errp); |
Gerd Hoffmann | c7a2196 | 2010-12-10 11:37:45 +0100 | [diff] [blame] | 511 | void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr); |
Gerd Hoffmann | a8e662b | 2009-09-25 21:42:39 +0200 | [diff] [blame] | 512 | void usb_unregister_port(USBBus *bus, USBPort *port); |
Gonglei | 7d553f2 | 2014-09-19 14:48:24 +0800 | [diff] [blame] | 513 | void usb_claim_port(USBDevice *dev, Error **errp); |
Gerd Hoffmann | 891fb2c | 2011-09-01 13:56:37 +0200 | [diff] [blame] | 514 | void usb_release_port(USBDevice *dev); |
Gonglei | 7d553f2 | 2014-09-19 14:48:24 +0800 | [diff] [blame] | 515 | void usb_device_attach(USBDevice *dev, Error **errp); |
Gerd Hoffmann | a8e662b | 2009-09-25 21:42:39 +0200 | [diff] [blame] | 516 | int usb_device_detach(USBDevice *dev); |
Gonglei | 594a536 | 2014-09-19 15:25:20 +0800 | [diff] [blame] | 517 | void usb_check_attach(USBDevice *dev, Error **errp); |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 518 | |
| 519 | static inline USBBus *usb_bus_from_device(USBDevice *d) |
| 520 | { |
| 521 | return DO_UPCAST(USBBus, qbus, d->qdev.parent_bus); |
| 522 | } |
Paolo Bonzini | 701a8f7 | 2012-01-13 17:07:20 +0100 | [diff] [blame] | 523 | |
| 524 | extern const VMStateDescription vmstate_usb_device; |
| 525 | |
| 526 | #define VMSTATE_USB_DEVICE(_field, _state) { \ |
| 527 | .name = (stringify(_field)), \ |
| 528 | .size = sizeof(USBDevice), \ |
| 529 | .vmsd = &vmstate_usb_device, \ |
| 530 | .flags = VMS_STRUCT, \ |
| 531 | .offset = vmstate_offset_value(_state, _field, USBDevice), \ |
| 532 | } |
| 533 | |
Gerd Hoffmann | 73796fe | 2012-01-10 16:59:28 +0100 | [diff] [blame] | 534 | USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr); |
| 535 | |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 536 | void usb_device_cancel_packet(USBDevice *dev, USBPacket *p); |
| 537 | |
| 538 | void usb_device_handle_attach(USBDevice *dev); |
| 539 | |
| 540 | void usb_device_handle_reset(USBDevice *dev); |
| 541 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 542 | void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request, |
| 543 | int val, int index, int length, uint8_t *data); |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 544 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 545 | void usb_device_handle_data(USBDevice *dev, USBPacket *p); |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 546 | |
| 547 | void usb_device_set_interface(USBDevice *dev, int interface, |
| 548 | int alt_old, int alt_new); |
| 549 | |
Hans de Goede | 36dfe32 | 2012-10-24 18:14:07 +0200 | [diff] [blame] | 550 | void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep); |
| 551 | |
Hans de Goede | f79738b | 2012-12-14 14:35:40 +0100 | [diff] [blame] | 552 | void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep); |
| 553 | |
Hans de Goede | 3b444ea | 2013-11-19 14:36:57 +0100 | [diff] [blame] | 554 | int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps, |
| 555 | int streams); |
| 556 | void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps); |
| 557 | |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 558 | const char *usb_device_get_product_desc(USBDevice *dev); |
| 559 | |
| 560 | const USBDesc *usb_device_get_usb_desc(USBDevice *dev); |
| 561 | |
Hans de Goede | b2d1fe6 | 2012-12-19 15:08:33 +0100 | [diff] [blame] | 562 | /* quirks.c */ |
Paolo Bonzini | 701a8f7 | 2012-01-13 17:07:20 +0100 | [diff] [blame] | 563 | |
Hans de Goede | b2d1fe6 | 2012-12-19 15:08:33 +0100 | [diff] [blame] | 564 | /* In bulk endpoints are streaming data sources (iow behave like isoc eps) */ |
| 565 | #define USB_QUIRK_BUFFER_BULK_IN 0x01 |
| 566 | /* Bulk pkts in FTDI format, need special handling when combining packets */ |
| 567 | #define USB_QUIRK_IS_FTDI 0x02 |
| 568 | |
| 569 | int usb_get_quirks(uint16_t vendor_id, uint16_t product_id, |
| 570 | uint8_t interface_class, uint8_t interface_subclass, |
| 571 | uint8_t interface_protocol); |
| 572 | |
| 573 | #endif |