xref: /qemu/include/hw/usb.h (revision 405cf80c)
10d09e41aSPaolo Bonzini #ifndef QEMU_USB_H
20d09e41aSPaolo Bonzini #define QEMU_USB_H
30d09e41aSPaolo Bonzini 
40d09e41aSPaolo Bonzini /*
50d09e41aSPaolo Bonzini  * QEMU USB API
60d09e41aSPaolo Bonzini  *
70d09e41aSPaolo Bonzini  * Copyright (c) 2005 Fabrice Bellard
80d09e41aSPaolo Bonzini  *
90d09e41aSPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
100d09e41aSPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
110d09e41aSPaolo Bonzini  * in the Software without restriction, including without limitation the rights
120d09e41aSPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
130d09e41aSPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
140d09e41aSPaolo Bonzini  * furnished to do so, subject to the following conditions:
150d09e41aSPaolo Bonzini  *
160d09e41aSPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
170d09e41aSPaolo Bonzini  * all copies or substantial portions of the Software.
180d09e41aSPaolo Bonzini  *
190d09e41aSPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
200d09e41aSPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
210d09e41aSPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
220d09e41aSPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
230d09e41aSPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
240d09e41aSPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
250d09e41aSPaolo Bonzini  * THE SOFTWARE.
260d09e41aSPaolo Bonzini  */
270d09e41aSPaolo Bonzini 
28d4842052SMarkus Armbruster #include "exec/memory.h"
29a27bd6c7SMarkus Armbruster #include "hw/qdev-core.h"
30daf015efSMarkus Armbruster #include "qemu/iov.h"
310d09e41aSPaolo Bonzini #include "qemu/queue.h"
32db1015e9SEduardo Habkost #include "qom/object.h"
330d09e41aSPaolo Bonzini 
340d09e41aSPaolo Bonzini /* Constants related to the USB / PCI interaction */
350d09e41aSPaolo Bonzini #define USB_SBRN    0x60 /* Serial Bus Release Number Register */
360d09e41aSPaolo Bonzini #define USB_RELEASE_1  0x10 /* USB 1.0 */
370d09e41aSPaolo Bonzini #define USB_RELEASE_2  0x20 /* USB 2.0 */
380d09e41aSPaolo Bonzini #define USB_RELEASE_3  0x30 /* USB 3.0 */
390d09e41aSPaolo Bonzini 
400d09e41aSPaolo Bonzini #define USB_TOKEN_SETUP 0x2d
410d09e41aSPaolo Bonzini #define USB_TOKEN_IN    0x69 /* device -> host */
420d09e41aSPaolo Bonzini #define USB_TOKEN_OUT   0xe1 /* host -> device */
430d09e41aSPaolo Bonzini 
440d09e41aSPaolo Bonzini #define USB_RET_SUCCESS           (0)
450d09e41aSPaolo Bonzini #define USB_RET_NODEV             (-1)
460d09e41aSPaolo Bonzini #define USB_RET_NAK               (-2)
470d09e41aSPaolo Bonzini #define USB_RET_STALL             (-3)
480d09e41aSPaolo Bonzini #define USB_RET_BABBLE            (-4)
490d09e41aSPaolo Bonzini #define USB_RET_IOERROR           (-5)
500d09e41aSPaolo Bonzini #define USB_RET_ASYNC             (-6)
510d09e41aSPaolo Bonzini #define USB_RET_ADD_TO_QUEUE      (-7)
520d09e41aSPaolo Bonzini #define USB_RET_REMOVE_FROM_QUEUE (-8)
530d09e41aSPaolo Bonzini 
540d09e41aSPaolo Bonzini #define USB_SPEED_LOW   0
550d09e41aSPaolo Bonzini #define USB_SPEED_FULL  1
560d09e41aSPaolo Bonzini #define USB_SPEED_HIGH  2
570d09e41aSPaolo Bonzini #define USB_SPEED_SUPER 3
580d09e41aSPaolo Bonzini 
590d09e41aSPaolo Bonzini #define USB_SPEED_MASK_LOW   (1 << USB_SPEED_LOW)
600d09e41aSPaolo Bonzini #define USB_SPEED_MASK_FULL  (1 << USB_SPEED_FULL)
610d09e41aSPaolo Bonzini #define USB_SPEED_MASK_HIGH  (1 << USB_SPEED_HIGH)
620d09e41aSPaolo Bonzini #define USB_SPEED_MASK_SUPER (1 << USB_SPEED_SUPER)
630d09e41aSPaolo Bonzini 
640d09e41aSPaolo Bonzini #define USB_STATE_NOTATTACHED 0
650d09e41aSPaolo Bonzini #define USB_STATE_ATTACHED    1
660d09e41aSPaolo Bonzini //#define USB_STATE_POWERED     2
670d09e41aSPaolo Bonzini #define USB_STATE_DEFAULT     3
680d09e41aSPaolo Bonzini //#define USB_STATE_ADDRESS     4
690d09e41aSPaolo Bonzini //#define	USB_STATE_CONFIGURED  5
700d09e41aSPaolo Bonzini #define USB_STATE_SUSPENDED   6
710d09e41aSPaolo Bonzini 
720d09e41aSPaolo Bonzini #define USB_CLASS_AUDIO			1
730d09e41aSPaolo Bonzini #define USB_CLASS_COMM			2
740d09e41aSPaolo Bonzini #define USB_CLASS_HID			3
750d09e41aSPaolo Bonzini #define USB_CLASS_PHYSICAL		5
760d09e41aSPaolo Bonzini #define USB_CLASS_STILL_IMAGE		6
770d09e41aSPaolo Bonzini #define USB_CLASS_PRINTER		7
780d09e41aSPaolo Bonzini #define USB_CLASS_MASS_STORAGE		8
790d09e41aSPaolo Bonzini #define USB_CLASS_HUB			9
800d09e41aSPaolo Bonzini #define USB_CLASS_CDC_DATA		0x0a
810d09e41aSPaolo Bonzini #define USB_CLASS_CSCID			0x0b
820d09e41aSPaolo Bonzini #define USB_CLASS_CONTENT_SEC		0x0d
830d09e41aSPaolo Bonzini #define USB_CLASS_APP_SPEC		0xfe
840d09e41aSPaolo Bonzini #define USB_CLASS_VENDOR_SPEC		0xff
850d09e41aSPaolo Bonzini 
860d09e41aSPaolo Bonzini #define USB_SUBCLASS_UNDEFINED          0
870d09e41aSPaolo Bonzini #define USB_SUBCLASS_AUDIO_CONTROL      1
880d09e41aSPaolo Bonzini #define USB_SUBCLASS_AUDIO_STREAMING    2
890d09e41aSPaolo Bonzini #define USB_SUBCLASS_AUDIO_MIDISTREAMING 3
900d09e41aSPaolo Bonzini 
910d09e41aSPaolo Bonzini #define USB_DIR_OUT			0
920d09e41aSPaolo Bonzini #define USB_DIR_IN			0x80
930d09e41aSPaolo Bonzini 
940d09e41aSPaolo Bonzini #define USB_TYPE_MASK			(0x03 << 5)
950d09e41aSPaolo Bonzini #define USB_TYPE_STANDARD		(0x00 << 5)
960d09e41aSPaolo Bonzini #define USB_TYPE_CLASS			(0x01 << 5)
970d09e41aSPaolo Bonzini #define USB_TYPE_VENDOR			(0x02 << 5)
980d09e41aSPaolo Bonzini #define USB_TYPE_RESERVED		(0x03 << 5)
990d09e41aSPaolo Bonzini 
1000d09e41aSPaolo Bonzini #define USB_RECIP_MASK			0x1f
1010d09e41aSPaolo Bonzini #define USB_RECIP_DEVICE		0x00
1020d09e41aSPaolo Bonzini #define USB_RECIP_INTERFACE		0x01
1030d09e41aSPaolo Bonzini #define USB_RECIP_ENDPOINT		0x02
1040d09e41aSPaolo Bonzini #define USB_RECIP_OTHER			0x03
1050d09e41aSPaolo Bonzini 
1060d09e41aSPaolo Bonzini #define DeviceRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
1070d09e41aSPaolo Bonzini #define DeviceOutRequest ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
1082b81ba53SGerd Hoffmann #define VendorDeviceRequest ((USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
1092b81ba53SGerd Hoffmann #define VendorDeviceOutRequest \
1102b81ba53SGerd Hoffmann         ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8)
1112b81ba53SGerd Hoffmann 
1120d09e41aSPaolo Bonzini #define InterfaceRequest                                        \
1130d09e41aSPaolo Bonzini         ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
1140d09e41aSPaolo Bonzini #define InterfaceOutRequest \
1150d09e41aSPaolo Bonzini         ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
1160d09e41aSPaolo Bonzini #define ClassInterfaceRequest \
1170d09e41aSPaolo Bonzini         ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)<<8)
1180d09e41aSPaolo Bonzini #define ClassInterfaceOutRequest \
1190d09e41aSPaolo Bonzini         ((USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE)<<8)
1202b81ba53SGerd Hoffmann #define VendorInterfaceRequest \
1212b81ba53SGerd Hoffmann         ((USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE)<<8)
1222b81ba53SGerd Hoffmann #define VendorInterfaceOutRequest \
1232b81ba53SGerd Hoffmann         ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE)<<8)
1242b81ba53SGerd Hoffmann 
1252b81ba53SGerd Hoffmann #define EndpointRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8)
1262b81ba53SGerd Hoffmann #define EndpointOutRequest \
1272b81ba53SGerd Hoffmann         ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8)
1280d09e41aSPaolo Bonzini 
1290d09e41aSPaolo Bonzini #define USB_REQ_GET_STATUS		0x00
1300d09e41aSPaolo Bonzini #define USB_REQ_CLEAR_FEATURE		0x01
1310d09e41aSPaolo Bonzini #define USB_REQ_SET_FEATURE		0x03
1320d09e41aSPaolo Bonzini #define USB_REQ_SET_ADDRESS		0x05
1330d09e41aSPaolo Bonzini #define USB_REQ_GET_DESCRIPTOR		0x06
1340d09e41aSPaolo Bonzini #define USB_REQ_SET_DESCRIPTOR		0x07
1350d09e41aSPaolo Bonzini #define USB_REQ_GET_CONFIGURATION	0x08
1360d09e41aSPaolo Bonzini #define USB_REQ_SET_CONFIGURATION	0x09
1370d09e41aSPaolo Bonzini #define USB_REQ_GET_INTERFACE		0x0A
1380d09e41aSPaolo Bonzini #define USB_REQ_SET_INTERFACE		0x0B
1390d09e41aSPaolo Bonzini #define USB_REQ_SYNCH_FRAME		0x0C
140811ad5d8SGerd Hoffmann #define USB_REQ_SET_SEL                 0x30
141811ad5d8SGerd Hoffmann #define USB_REQ_SET_ISOCH_DELAY         0x31
1420d09e41aSPaolo Bonzini 
1430d09e41aSPaolo Bonzini #define USB_DEVICE_SELF_POWERED		0
1440d09e41aSPaolo Bonzini #define USB_DEVICE_REMOTE_WAKEUP	1
1450d09e41aSPaolo Bonzini 
1460d09e41aSPaolo Bonzini #define USB_DT_DEVICE			0x01
1470d09e41aSPaolo Bonzini #define USB_DT_CONFIG			0x02
1480d09e41aSPaolo Bonzini #define USB_DT_STRING			0x03
1490d09e41aSPaolo Bonzini #define USB_DT_INTERFACE		0x04
1500d09e41aSPaolo Bonzini #define USB_DT_ENDPOINT			0x05
1510d09e41aSPaolo Bonzini #define USB_DT_DEVICE_QUALIFIER         0x06
1520d09e41aSPaolo Bonzini #define USB_DT_OTHER_SPEED_CONFIG       0x07
1530d09e41aSPaolo Bonzini #define USB_DT_DEBUG                    0x0A
1540d09e41aSPaolo Bonzini #define USB_DT_INTERFACE_ASSOC          0x0B
1550d09e41aSPaolo Bonzini #define USB_DT_BOS                      0x0F
1560d09e41aSPaolo Bonzini #define USB_DT_DEVICE_CAPABILITY        0x10
1570d09e41aSPaolo Bonzini #define USB_DT_CS_INTERFACE             0x24
1580d09e41aSPaolo Bonzini #define USB_DT_CS_ENDPOINT              0x25
1590d09e41aSPaolo Bonzini #define USB_DT_ENDPOINT_COMPANION       0x30
1600d09e41aSPaolo Bonzini 
1610d09e41aSPaolo Bonzini #define USB_DEV_CAP_WIRELESS            0x01
1620d09e41aSPaolo Bonzini #define USB_DEV_CAP_USB2_EXT            0x02
1630d09e41aSPaolo Bonzini #define USB_DEV_CAP_SUPERSPEED          0x03
1640d09e41aSPaolo Bonzini 
165bd93976aSPantelis Koukousoulas #define USB_CFG_ATT_ONE              (1 << 7) /* should always be set */
166bd93976aSPantelis Koukousoulas #define USB_CFG_ATT_SELFPOWER        (1 << 6)
167bd93976aSPantelis Koukousoulas #define USB_CFG_ATT_WAKEUP           (1 << 5)
168bd93976aSPantelis Koukousoulas #define USB_CFG_ATT_BATTERY          (1 << 4)
169bd93976aSPantelis Koukousoulas 
1700d09e41aSPaolo Bonzini #define USB_ENDPOINT_XFER_CONTROL	0
1710d09e41aSPaolo Bonzini #define USB_ENDPOINT_XFER_ISOC		1
1720d09e41aSPaolo Bonzini #define USB_ENDPOINT_XFER_BULK		2
1730d09e41aSPaolo Bonzini #define USB_ENDPOINT_XFER_INT		3
1740d09e41aSPaolo Bonzini #define USB_ENDPOINT_XFER_INVALID     255
1750d09e41aSPaolo Bonzini 
1760d09e41aSPaolo Bonzini #define USB_INTERFACE_INVALID         255
1770d09e41aSPaolo Bonzini 
1780d09e41aSPaolo Bonzini typedef struct USBBusOps USBBusOps;
1790d09e41aSPaolo Bonzini typedef struct USBPort USBPort;
1800d09e41aSPaolo Bonzini typedef struct USBDevice USBDevice;
1810d09e41aSPaolo Bonzini typedef struct USBPacket USBPacket;
1820d09e41aSPaolo Bonzini typedef struct USBCombinedPacket USBCombinedPacket;
1830d09e41aSPaolo Bonzini typedef struct USBEndpoint USBEndpoint;
1840d09e41aSPaolo Bonzini 
1850d09e41aSPaolo Bonzini typedef struct USBDesc USBDesc;
1860d09e41aSPaolo Bonzini typedef struct USBDescID USBDescID;
1870d09e41aSPaolo Bonzini typedef struct USBDescDevice USBDescDevice;
1880d09e41aSPaolo Bonzini typedef struct USBDescConfig USBDescConfig;
1890d09e41aSPaolo Bonzini typedef struct USBDescIfaceAssoc USBDescIfaceAssoc;
1900d09e41aSPaolo Bonzini typedef struct USBDescIface USBDescIface;
1910d09e41aSPaolo Bonzini typedef struct USBDescEndpoint USBDescEndpoint;
1920d09e41aSPaolo Bonzini typedef struct USBDescOther USBDescOther;
1930d09e41aSPaolo Bonzini typedef struct USBDescString USBDescString;
1945319dc7bSGerd Hoffmann typedef struct USBDescMSOS USBDescMSOS;
1950d09e41aSPaolo Bonzini 
1960d09e41aSPaolo Bonzini struct USBDescString {
1970d09e41aSPaolo Bonzini     uint8_t index;
1980d09e41aSPaolo Bonzini     char *str;
1990d09e41aSPaolo Bonzini     QLIST_ENTRY(USBDescString) next;
2000d09e41aSPaolo Bonzini };
2010d09e41aSPaolo Bonzini 
2020d09e41aSPaolo Bonzini #define USB_MAX_ENDPOINTS  15
2030d09e41aSPaolo Bonzini #define USB_MAX_INTERFACES 16
2040d09e41aSPaolo Bonzini 
2050d09e41aSPaolo Bonzini struct USBEndpoint {
2060d09e41aSPaolo Bonzini     uint8_t nr;
2070d09e41aSPaolo Bonzini     uint8_t pid;
2080d09e41aSPaolo Bonzini     uint8_t type;
2090d09e41aSPaolo Bonzini     uint8_t ifnum;
2100d09e41aSPaolo Bonzini     int max_packet_size;
21104b300f8SHans de Goede     int max_streams;
2120d09e41aSPaolo Bonzini     bool pipeline;
2130d09e41aSPaolo Bonzini     bool halted;
2140d09e41aSPaolo Bonzini     USBDevice *dev;
2150d09e41aSPaolo Bonzini     QTAILQ_HEAD(, USBPacket) queue;
2160d09e41aSPaolo Bonzini };
2170d09e41aSPaolo Bonzini 
2180d09e41aSPaolo Bonzini enum USBDeviceFlags {
2190d09e41aSPaolo Bonzini     USB_DEV_FLAG_IS_HOST,
2205319dc7bSGerd Hoffmann     USB_DEV_FLAG_MSOS_DESC_ENABLE,
2215319dc7bSGerd Hoffmann     USB_DEV_FLAG_MSOS_DESC_IN_USE,
2220d09e41aSPaolo Bonzini };
2230d09e41aSPaolo Bonzini 
2240d09e41aSPaolo Bonzini /* definition of a USB device */
2250d09e41aSPaolo Bonzini struct USBDevice {
2260d09e41aSPaolo Bonzini     DeviceState qdev;
2270d09e41aSPaolo Bonzini     USBPort *port;
2280d09e41aSPaolo Bonzini     char *port_path;
22971938a09SGerd Hoffmann     char *serial;
2300d09e41aSPaolo Bonzini     void *opaque;
2310d09e41aSPaolo Bonzini     uint32_t flags;
2320d09e41aSPaolo Bonzini 
2330f6dba14SGerd Hoffmann     char *pcap_filename;
2340f6dba14SGerd Hoffmann     FILE *pcap;
2350f6dba14SGerd Hoffmann 
2360d09e41aSPaolo Bonzini     /* Actual connected speed */
2370d09e41aSPaolo Bonzini     int speed;
2380d09e41aSPaolo Bonzini     /* Supported speeds, not in info because it may be variable (hostdevs) */
2390d09e41aSPaolo Bonzini     int speedmask;
2400d09e41aSPaolo Bonzini     uint8_t addr;
2410d09e41aSPaolo Bonzini     char product_desc[32];
2420d09e41aSPaolo Bonzini     int auto_attach;
243eb19d2b9SGerd Hoffmann     bool attached;
2440d09e41aSPaolo Bonzini 
2450d09e41aSPaolo Bonzini     int32_t state;
2460d09e41aSPaolo Bonzini     uint8_t setup_buf[8];
2470d09e41aSPaolo Bonzini     uint8_t data_buf[4096];
2480d09e41aSPaolo Bonzini     int32_t remote_wakeup;
2490d09e41aSPaolo Bonzini     int32_t setup_state;
2500d09e41aSPaolo Bonzini     int32_t setup_len;
2510d09e41aSPaolo Bonzini     int32_t setup_index;
2520d09e41aSPaolo Bonzini 
2530d09e41aSPaolo Bonzini     USBEndpoint ep_ctl;
2540d09e41aSPaolo Bonzini     USBEndpoint ep_in[USB_MAX_ENDPOINTS];
2550d09e41aSPaolo Bonzini     USBEndpoint ep_out[USB_MAX_ENDPOINTS];
2560d09e41aSPaolo Bonzini 
2570d09e41aSPaolo Bonzini     QLIST_HEAD(, USBDescString) strings;
2580d09e41aSPaolo Bonzini     const USBDesc *usb_desc; /* Overrides class usb_desc if not NULL */
2590d09e41aSPaolo Bonzini     const USBDescDevice *device;
2600d09e41aSPaolo Bonzini 
2610d09e41aSPaolo Bonzini     int configuration;
2620d09e41aSPaolo Bonzini     int ninterfaces;
2630d09e41aSPaolo Bonzini     int altsetting[USB_MAX_INTERFACES];
2640d09e41aSPaolo Bonzini     const USBDescConfig *config;
2650d09e41aSPaolo Bonzini     const USBDescIface  *ifaces[USB_MAX_INTERFACES];
2660d09e41aSPaolo Bonzini };
2670d09e41aSPaolo Bonzini 
2680d09e41aSPaolo Bonzini #define TYPE_USB_DEVICE "usb-device"
269a489d195SEduardo Habkost OBJECT_DECLARE_TYPE(USBDevice, USBDeviceClass, USB_DEVICE)
2700d09e41aSPaolo Bonzini 
2717d553f27SGonglei typedef void (*USBDeviceRealize)(USBDevice *dev, Error **errp);
272b69c3c21SMarkus Armbruster typedef void (*USBDeviceUnrealize)(USBDevice *dev);
2737d553f27SGonglei 
274db1015e9SEduardo Habkost struct USBDeviceClass {
2750d09e41aSPaolo Bonzini     DeviceClass parent_class;
2760d09e41aSPaolo Bonzini 
2777d553f27SGonglei     USBDeviceRealize realize;
2787d553f27SGonglei     USBDeviceUnrealize unrealize;
2797d553f27SGonglei 
2800d09e41aSPaolo Bonzini     /*
2810d09e41aSPaolo Bonzini      * Walk (enabled) downstream ports, check for a matching device.
2820d09e41aSPaolo Bonzini      * Only hubs implement this.
2830d09e41aSPaolo Bonzini      */
2840d09e41aSPaolo Bonzini     USBDevice *(*find_device)(USBDevice *dev, uint8_t addr);
2850d09e41aSPaolo Bonzini 
2860d09e41aSPaolo Bonzini     /*
2870d09e41aSPaolo Bonzini      * Called when a packet is canceled.
2880d09e41aSPaolo Bonzini      */
2890d09e41aSPaolo Bonzini     void (*cancel_packet)(USBDevice *dev, USBPacket *p);
2900d09e41aSPaolo Bonzini 
2910d09e41aSPaolo Bonzini     /*
2920d09e41aSPaolo Bonzini      * Attach the device
2930d09e41aSPaolo Bonzini      */
2940d09e41aSPaolo Bonzini     void (*handle_attach)(USBDevice *dev);
2950d09e41aSPaolo Bonzini 
2960d09e41aSPaolo Bonzini     /*
2970d09e41aSPaolo Bonzini      * Reset the device
2980d09e41aSPaolo Bonzini      */
2990d09e41aSPaolo Bonzini     void (*handle_reset)(USBDevice *dev);
3000d09e41aSPaolo Bonzini 
3010d09e41aSPaolo Bonzini     /*
3020d09e41aSPaolo Bonzini      * Process control request.
3030d09e41aSPaolo Bonzini      * Called from handle_packet().
3040d09e41aSPaolo Bonzini      *
3050d09e41aSPaolo Bonzini      * Status gets stored in p->status, and if p->status == USB_RET_SUCCESS
3060d09e41aSPaolo Bonzini      * then the number of bytes transferred is stored in p->actual_length
3070d09e41aSPaolo Bonzini      */
3080d09e41aSPaolo Bonzini     void (*handle_control)(USBDevice *dev, USBPacket *p, int request, int value,
3090d09e41aSPaolo Bonzini                            int index, int length, uint8_t *data);
3100d09e41aSPaolo Bonzini 
3110d09e41aSPaolo Bonzini     /*
3120d09e41aSPaolo Bonzini      * Process data transfers (both BULK and ISOC).
3130d09e41aSPaolo Bonzini      * Called from handle_packet().
3140d09e41aSPaolo Bonzini      *
3150d09e41aSPaolo Bonzini      * Status gets stored in p->status, and if p->status == USB_RET_SUCCESS
3160d09e41aSPaolo Bonzini      * then the number of bytes transferred is stored in p->actual_length
3170d09e41aSPaolo Bonzini      */
3180d09e41aSPaolo Bonzini     void (*handle_data)(USBDevice *dev, USBPacket *p);
3190d09e41aSPaolo Bonzini 
3200d09e41aSPaolo Bonzini     void (*set_interface)(USBDevice *dev, int interface,
3210d09e41aSPaolo Bonzini                           int alt_old, int alt_new);
3220d09e41aSPaolo Bonzini 
3230d09e41aSPaolo Bonzini     /*
3240d09e41aSPaolo Bonzini      * Called when the hcd is done queuing packets for an endpoint, only
3250d09e41aSPaolo Bonzini      * necessary for devices which can return USB_RET_ADD_TO_QUEUE.
3260d09e41aSPaolo Bonzini      */
3270d09e41aSPaolo Bonzini     void (*flush_ep_queue)(USBDevice *dev, USBEndpoint *ep);
3280d09e41aSPaolo Bonzini 
3290d09e41aSPaolo Bonzini     /*
3300d09e41aSPaolo Bonzini      * Called by the hcd to let the device know the queue for an endpoint
3310d09e41aSPaolo Bonzini      * has been unlinked / stopped. Optional may be NULL.
3320d09e41aSPaolo Bonzini      */
3330d09e41aSPaolo Bonzini     void (*ep_stopped)(USBDevice *dev, USBEndpoint *ep);
3340d09e41aSPaolo Bonzini 
3353b444eadSHans de Goede     /*
3363b444eadSHans de Goede      * Called by the hcd to alloc / free streams on a bulk endpoint.
3373b444eadSHans de Goede      * Optional may be NULL.
3383b444eadSHans de Goede      */
3393b444eadSHans de Goede     int (*alloc_streams)(USBDevice *dev, USBEndpoint **eps, int nr_eps,
3403b444eadSHans de Goede                          int streams);
3413b444eadSHans de Goede     void (*free_streams)(USBDevice *dev, USBEndpoint **eps, int nr_eps);
3423b444eadSHans de Goede 
3430d09e41aSPaolo Bonzini     const char *product_desc;
3440d09e41aSPaolo Bonzini     const USBDesc *usb_desc;
3451e351dc3SGerd Hoffmann     bool attached_settable;
346db1015e9SEduardo Habkost };
3470d09e41aSPaolo Bonzini 
3480d09e41aSPaolo Bonzini typedef struct USBPortOps {
3490d09e41aSPaolo Bonzini     void (*attach)(USBPort *port);
3500d09e41aSPaolo Bonzini     void (*detach)(USBPort *port);
3510d09e41aSPaolo Bonzini     /*
3520d09e41aSPaolo Bonzini      * This gets called when a device downstream from the device attached to
3530d09e41aSPaolo Bonzini      * the port (iow attached through a hub) gets detached.
3540d09e41aSPaolo Bonzini      */
3550d09e41aSPaolo Bonzini     void (*child_detach)(USBPort *port, USBDevice *child);
3560d09e41aSPaolo Bonzini     void (*wakeup)(USBPort *port);
3570d09e41aSPaolo Bonzini     /*
3580d09e41aSPaolo Bonzini      * Note that port->dev will be different then the device from which
3590d09e41aSPaolo Bonzini      * the packet originated when a hub is involved.
3600d09e41aSPaolo Bonzini      */
3610d09e41aSPaolo Bonzini     void (*complete)(USBPort *port, USBPacket *p);
3620d09e41aSPaolo Bonzini } USBPortOps;
3630d09e41aSPaolo Bonzini 
3640d09e41aSPaolo Bonzini /* USB port on which a device can be connected */
3650d09e41aSPaolo Bonzini struct USBPort {
3660d09e41aSPaolo Bonzini     USBDevice *dev;
3670d09e41aSPaolo Bonzini     int speedmask;
36847b5264eSAnthony Liguori     int hubcount;
3690d09e41aSPaolo Bonzini     char path[16];
3700d09e41aSPaolo Bonzini     USBPortOps *ops;
3710d09e41aSPaolo Bonzini     void *opaque;
3720d09e41aSPaolo Bonzini     int index; /* internal port index, may be used with the opaque */
3730d09e41aSPaolo Bonzini     QTAILQ_ENTRY(USBPort) next;
3740d09e41aSPaolo Bonzini };
3750d09e41aSPaolo Bonzini 
3760d09e41aSPaolo Bonzini typedef void USBCallback(USBPacket * packet, void *opaque);
3770d09e41aSPaolo Bonzini 
3780d09e41aSPaolo Bonzini typedef enum USBPacketState {
3790d09e41aSPaolo Bonzini     USB_PACKET_UNDEFINED = 0,
3800d09e41aSPaolo Bonzini     USB_PACKET_SETUP,
3810d09e41aSPaolo Bonzini     USB_PACKET_QUEUED,
3820d09e41aSPaolo Bonzini     USB_PACKET_ASYNC,
3830d09e41aSPaolo Bonzini     USB_PACKET_COMPLETE,
3840d09e41aSPaolo Bonzini     USB_PACKET_CANCELED,
3850d09e41aSPaolo Bonzini } USBPacketState;
3860d09e41aSPaolo Bonzini 
3870d09e41aSPaolo Bonzini /* Structure used to hold information about an active USB packet.  */
3880d09e41aSPaolo Bonzini struct USBPacket {
3890d09e41aSPaolo Bonzini     /* Data fields for use by the driver.  */
3900d09e41aSPaolo Bonzini     int pid;
3910d09e41aSPaolo Bonzini     uint64_t id;
3920d09e41aSPaolo Bonzini     USBEndpoint *ep;
3930d09e41aSPaolo Bonzini     unsigned int stream;
3940d09e41aSPaolo Bonzini     QEMUIOVector iov;
3950d09e41aSPaolo Bonzini     uint64_t parameter; /* control transfers */
3960d09e41aSPaolo Bonzini     bool short_not_ok;
3970d09e41aSPaolo Bonzini     bool int_req;
3980d09e41aSPaolo Bonzini     int status; /* USB_RET_* status code */
3990d09e41aSPaolo Bonzini     int actual_length; /* Number of bytes actually transferred */
4000d09e41aSPaolo Bonzini     /* Internal use by the USB layer.  */
4010d09e41aSPaolo Bonzini     USBPacketState state;
4020d09e41aSPaolo Bonzini     USBCombinedPacket *combined;
4030d09e41aSPaolo Bonzini     QTAILQ_ENTRY(USBPacket) queue;
4040d09e41aSPaolo Bonzini     QTAILQ_ENTRY(USBPacket) combined_entry;
4050d09e41aSPaolo Bonzini };
4060d09e41aSPaolo Bonzini 
4070d09e41aSPaolo Bonzini struct USBCombinedPacket {
4080d09e41aSPaolo Bonzini     USBPacket *first;
409eae3eb3eSPaolo Bonzini     QTAILQ_HEAD(, USBPacket) packets;
4100d09e41aSPaolo Bonzini     QEMUIOVector iov;
4110d09e41aSPaolo Bonzini };
4120d09e41aSPaolo Bonzini 
4130d09e41aSPaolo Bonzini void usb_packet_init(USBPacket *p);
4140d09e41aSPaolo Bonzini void usb_packet_set_state(USBPacket *p, USBPacketState state);
4150d09e41aSPaolo Bonzini void usb_packet_check_state(USBPacket *p, USBPacketState expected);
4160d09e41aSPaolo Bonzini void usb_packet_setup(USBPacket *p, int pid,
4170d09e41aSPaolo Bonzini                       USBEndpoint *ep, unsigned int stream,
4180d09e41aSPaolo Bonzini                       uint64_t id, bool short_not_ok, bool int_req);
4190d09e41aSPaolo Bonzini void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len);
4200d09e41aSPaolo Bonzini int usb_packet_map(USBPacket *p, QEMUSGList *sgl);
4210d09e41aSPaolo Bonzini void usb_packet_unmap(USBPacket *p, QEMUSGList *sgl);
4220d09e41aSPaolo Bonzini void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes);
4230d09e41aSPaolo Bonzini void usb_packet_skip(USBPacket *p, size_t bytes);
4240d09e41aSPaolo Bonzini size_t usb_packet_size(USBPacket *p);
4250d09e41aSPaolo Bonzini void usb_packet_cleanup(USBPacket *p);
4260d09e41aSPaolo Bonzini 
4270d09e41aSPaolo Bonzini static inline bool usb_packet_is_inflight(USBPacket *p)
4280d09e41aSPaolo Bonzini {
4290d09e41aSPaolo Bonzini     return (p->state == USB_PACKET_QUEUED ||
4300d09e41aSPaolo Bonzini             p->state == USB_PACKET_ASYNC);
4310d09e41aSPaolo Bonzini }
4320d09e41aSPaolo Bonzini 
4330d09e41aSPaolo Bonzini USBDevice *usb_find_device(USBPort *port, uint8_t addr);
4340d09e41aSPaolo Bonzini 
4350d09e41aSPaolo Bonzini void usb_handle_packet(USBDevice *dev, USBPacket *p);
4360d09e41aSPaolo Bonzini void usb_packet_complete(USBDevice *dev, USBPacket *p);
4370d09e41aSPaolo Bonzini void usb_packet_complete_one(USBDevice *dev, USBPacket *p);
4380d09e41aSPaolo Bonzini void usb_cancel_packet(USBPacket * p);
4390d09e41aSPaolo Bonzini 
4400d09e41aSPaolo Bonzini void usb_ep_init(USBDevice *dev);
4410d09e41aSPaolo Bonzini void usb_ep_reset(USBDevice *dev);
4420d09e41aSPaolo Bonzini void usb_ep_dump(USBDevice *dev);
4430d09e41aSPaolo Bonzini struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep);
4440d09e41aSPaolo Bonzini uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep);
4450d09e41aSPaolo Bonzini void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type);
4460d09e41aSPaolo Bonzini void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum);
4470d09e41aSPaolo Bonzini void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
4480d09e41aSPaolo Bonzini                                 uint16_t raw);
44904b300f8SHans de Goede void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw);
4500d09e41aSPaolo Bonzini void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted);
4510d09e41aSPaolo Bonzini USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
4520d09e41aSPaolo Bonzini                                     uint64_t id);
4530d09e41aSPaolo Bonzini 
4540d09e41aSPaolo Bonzini void usb_ep_combine_input_packets(USBEndpoint *ep);
4550d09e41aSPaolo Bonzini void usb_combined_input_packet_complete(USBDevice *dev, USBPacket *p);
4560d09e41aSPaolo Bonzini void usb_combined_packet_cancel(USBDevice *dev, USBPacket *p);
4570d09e41aSPaolo Bonzini 
458b791c3b3SGerd Hoffmann void usb_pick_speed(USBPort *port);
4590d09e41aSPaolo Bonzini void usb_attach(USBPort *port);
4600d09e41aSPaolo Bonzini void usb_detach(USBPort *port);
4610d09e41aSPaolo Bonzini void usb_port_reset(USBPort *port);
4620d09e41aSPaolo Bonzini void usb_device_reset(USBDevice *dev);
4630d09e41aSPaolo Bonzini void usb_wakeup(USBEndpoint *ep, unsigned int stream);
4640d09e41aSPaolo Bonzini void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p);
4650d09e41aSPaolo Bonzini 
4660d09e41aSPaolo Bonzini /* usb-linux.c */
4671ce6be24SMarkus Armbruster void hmp_info_usbhost(Monitor *mon, const QDict *qdict);
468b99260ebSThomas Huth bool usb_host_dev_is_scsi_storage(USBDevice *usbdev);
4690d09e41aSPaolo Bonzini 
4700d09e41aSPaolo Bonzini /* usb ports of the VM */
4710d09e41aSPaolo Bonzini 
4720d09e41aSPaolo Bonzini #define VM_USB_HUB_SIZE 8
4730d09e41aSPaolo Bonzini 
4740d09e41aSPaolo Bonzini /* usb-bus.c */
4750d09e41aSPaolo Bonzini 
4760d09e41aSPaolo Bonzini #define TYPE_USB_BUS "usb-bus"
4778063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(USBBus, USB_BUS)
4780d09e41aSPaolo Bonzini 
4790d09e41aSPaolo Bonzini struct USBBus {
4800d09e41aSPaolo Bonzini     BusState qbus;
4810d09e41aSPaolo Bonzini     USBBusOps *ops;
4820d09e41aSPaolo Bonzini     int busnr;
4830d09e41aSPaolo Bonzini     int nfree;
4840d09e41aSPaolo Bonzini     int nused;
4850d09e41aSPaolo Bonzini     QTAILQ_HEAD(, USBPort) free;
4860d09e41aSPaolo Bonzini     QTAILQ_HEAD(, USBPort) used;
4870d09e41aSPaolo Bonzini     QTAILQ_ENTRY(USBBus) next;
4880d09e41aSPaolo Bonzini };
4890d09e41aSPaolo Bonzini 
4900d09e41aSPaolo Bonzini struct USBBusOps {
491f4bbaaf5SMarkus Armbruster     void (*register_companion)(USBBus *bus, USBPort *ports[],
492f4bbaaf5SMarkus Armbruster                                uint32_t portcount, uint32_t firstport,
493f4bbaaf5SMarkus Armbruster                                Error **errp);
4940d09e41aSPaolo Bonzini     void (*wakeup_endpoint)(USBBus *bus, USBEndpoint *ep, unsigned int stream);
4950d09e41aSPaolo Bonzini };
4960d09e41aSPaolo Bonzini 
497c889b3a5SAndreas Färber void usb_bus_new(USBBus *bus, size_t bus_size,
498c889b3a5SAndreas Färber                  USBBusOps *ops, DeviceState *host);
499e5a9beceSGonglei void usb_bus_release(USBBus *bus);
5000d09e41aSPaolo Bonzini USBBus *usb_bus_find(int busnr);
5010d09e41aSPaolo Bonzini void usb_legacy_register(const char *typename, const char *usbdevice_name,
502*405cf80cSPaolo Bonzini                          USBDevice *(*usbdevice_init)(void));
50332aaaebeSMarkus Armbruster USBDevice *usb_new(const char *name);
50432aaaebeSMarkus Armbruster bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **errp);
5050d09e41aSPaolo Bonzini USBDevice *usb_create_simple(USBBus *bus, const char *name);
5060d09e41aSPaolo Bonzini USBDevice *usbdevice_create(const char *cmdline);
5070d09e41aSPaolo Bonzini void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
5080d09e41aSPaolo Bonzini                        USBPortOps *ops, int speedmask);
509f4bbaaf5SMarkus Armbruster void usb_register_companion(const char *masterbus, USBPort *ports[],
5100d09e41aSPaolo Bonzini                             uint32_t portcount, uint32_t firstport,
511f4bbaaf5SMarkus Armbruster                             void *opaque, USBPortOps *ops, int speedmask,
512f4bbaaf5SMarkus Armbruster                             Error **errp);
5130d09e41aSPaolo Bonzini void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr);
5140d09e41aSPaolo Bonzini void usb_unregister_port(USBBus *bus, USBPort *port);
5157d553f27SGonglei void usb_claim_port(USBDevice *dev, Error **errp);
5160d09e41aSPaolo Bonzini void usb_release_port(USBDevice *dev);
5177d553f27SGonglei void usb_device_attach(USBDevice *dev, Error **errp);
5180d09e41aSPaolo Bonzini int usb_device_detach(USBDevice *dev);
519594a5360SGonglei void usb_check_attach(USBDevice *dev, Error **errp);
5200d09e41aSPaolo Bonzini 
5210d09e41aSPaolo Bonzini static inline USBBus *usb_bus_from_device(USBDevice *d)
5220d09e41aSPaolo Bonzini {
5230d09e41aSPaolo Bonzini     return DO_UPCAST(USBBus, qbus, d->qdev.parent_bus);
5240d09e41aSPaolo Bonzini }
5250d09e41aSPaolo Bonzini 
5260d09e41aSPaolo Bonzini extern const VMStateDescription vmstate_usb_device;
5270d09e41aSPaolo Bonzini 
5280d09e41aSPaolo Bonzini #define VMSTATE_USB_DEVICE(_field, _state) {                         \
5290d09e41aSPaolo Bonzini     .name       = (stringify(_field)),                               \
5300d09e41aSPaolo Bonzini     .size       = sizeof(USBDevice),                                 \
5310d09e41aSPaolo Bonzini     .vmsd       = &vmstate_usb_device,                               \
5320d09e41aSPaolo Bonzini     .flags      = VMS_STRUCT,                                        \
5330d09e41aSPaolo Bonzini     .offset     = vmstate_offset_value(_state, _field, USBDevice),   \
5340d09e41aSPaolo Bonzini }
5350d09e41aSPaolo Bonzini 
5360d09e41aSPaolo Bonzini USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr);
5370d09e41aSPaolo Bonzini 
5380d09e41aSPaolo Bonzini void usb_device_cancel_packet(USBDevice *dev, USBPacket *p);
5390d09e41aSPaolo Bonzini 
5400d09e41aSPaolo Bonzini void usb_device_handle_attach(USBDevice *dev);
5410d09e41aSPaolo Bonzini 
5420d09e41aSPaolo Bonzini void usb_device_handle_reset(USBDevice *dev);
5430d09e41aSPaolo Bonzini 
5440d09e41aSPaolo Bonzini void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
5450d09e41aSPaolo Bonzini                                int val, int index, int length, uint8_t *data);
5460d09e41aSPaolo Bonzini 
5470d09e41aSPaolo Bonzini void usb_device_handle_data(USBDevice *dev, USBPacket *p);
5480d09e41aSPaolo Bonzini 
5490d09e41aSPaolo Bonzini void usb_device_set_interface(USBDevice *dev, int interface,
5500d09e41aSPaolo Bonzini                               int alt_old, int alt_new);
5510d09e41aSPaolo Bonzini 
5520d09e41aSPaolo Bonzini void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep);
5530d09e41aSPaolo Bonzini 
5540d09e41aSPaolo Bonzini void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep);
5550d09e41aSPaolo Bonzini 
5563b444eadSHans de Goede int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
5573b444eadSHans de Goede                              int streams);
5583b444eadSHans de Goede void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps);
5593b444eadSHans de Goede 
5600d09e41aSPaolo Bonzini const char *usb_device_get_product_desc(USBDevice *dev);
5610d09e41aSPaolo Bonzini 
5620d09e41aSPaolo Bonzini const USBDesc *usb_device_get_usb_desc(USBDevice *dev);
5630d09e41aSPaolo Bonzini 
5640d09e41aSPaolo Bonzini /* quirks.c */
5650d09e41aSPaolo Bonzini 
5660d09e41aSPaolo Bonzini /* In bulk endpoints are streaming data sources (iow behave like isoc eps) */
5670d09e41aSPaolo Bonzini #define USB_QUIRK_BUFFER_BULK_IN	0x01
5680d09e41aSPaolo Bonzini /* Bulk pkts in FTDI format, need special handling when combining packets */
5690d09e41aSPaolo Bonzini #define USB_QUIRK_IS_FTDI		0x02
5700d09e41aSPaolo Bonzini 
5710d09e41aSPaolo Bonzini int usb_get_quirks(uint16_t vendor_id, uint16_t product_id,
5720d09e41aSPaolo Bonzini                    uint8_t interface_class, uint8_t interface_subclass,
5730d09e41aSPaolo Bonzini                    uint8_t interface_protocol);
5740d09e41aSPaolo Bonzini 
5750f6dba14SGerd Hoffmann /* pcap.c */
5760f6dba14SGerd Hoffmann void usb_pcap_init(FILE *fp);
5770f6dba14SGerd Hoffmann void usb_pcap_ctrl(USBPacket *p, bool setup);
5780f6dba14SGerd Hoffmann void usb_pcap_data(USBPacket *p, bool setup);
5790f6dba14SGerd Hoffmann 
5800d09e41aSPaolo Bonzini #endif
581