/* SPDX-License-Identifier: MIT */12/*3* usbif.h4*5* USB I/O interface for Xen guest OSes.6*7* Copyright (C) 2009, FUJITSU LABORATORIES LTD.8* Author: Noboru Iwamatsu <[email protected]>9*/1011#ifndef __XEN_PUBLIC_IO_USBIF_H__12#define __XEN_PUBLIC_IO_USBIF_H__1314#include "ring.h"15#include "../grant_table.h"1617/*18* Detailed Interface Description19* ==============================20* The pvUSB interface is using a split driver design: a frontend driver in21* the guest and a backend driver in a driver domain (normally dom0) having22* access to the physical USB device(s) being passed to the guest.23*24* The frontend and backend drivers use XenStore to initiate the connection25* between them, the I/O activity is handled via two shared ring pages and an26* event channel. As the interface between frontend and backend is at the USB27* host connector level, multiple (up to 31) physical USB devices can be28* handled by a single connection.29*30* The Xen pvUSB device name is "qusb", so the frontend's XenStore entries are31* to be found under "device/qusb", while the backend's XenStore entries are32* under "backend/<guest-dom-id>/qusb".33*34* When a new pvUSB connection is established, the frontend needs to setup the35* two shared ring pages for communication and the event channel. The ring36* pages need to be made available to the backend via the grant table37* interface.38*39* One of the shared ring pages is used by the backend to inform the frontend40* about USB device plug events (device to be added or removed). This is the41* "conn-ring".42*43* The other ring page is used for USB I/O communication (requests and44* responses). This is the "urb-ring".45*46* Feature and Parameter Negotiation47* =================================48* The two halves of a Xen pvUSB driver utilize nodes within the XenStore to49* communicate capabilities and to negotiate operating parameters. This50* section enumerates these nodes which reside in the respective front and51* backend portions of the XenStore, following the XenBus convention.52*53* Any specified default value is in effect if the corresponding XenBus node54* is not present in the XenStore.55*56* XenStore nodes in sections marked "PRIVATE" are solely for use by the57* driver side whose XenBus tree contains them.58*59*****************************************************************************60* Backend XenBus Nodes61*****************************************************************************62*63*------------------ Backend Device Identification (PRIVATE) ------------------64*65* num-ports66* Values: unsigned [1...31]67*68* Number of ports for this (virtual) USB host connector.69*70* usb-ver71* Values: unsigned [1...2]72*73* USB version of this host connector: 1 = USB 1.1, 2 = USB 2.0.74*75* port/[1...31]76* Values: string77*78* Physical USB device connected to the given port, e.g. "3-1.5".79*80*****************************************************************************81* Frontend XenBus Nodes82*****************************************************************************83*84*----------------------- Request Transport Parameters -----------------------85*86* event-channel87* Values: unsigned88*89* The identifier of the Xen event channel used to signal activity90* in the ring buffer.91*92* urb-ring-ref93* Values: unsigned94*95* The Xen grant reference granting permission for the backend to map96* the sole page in a single page sized ring buffer. This is the ring97* buffer for urb requests.98*99* conn-ring-ref100* Values: unsigned101*102* The Xen grant reference granting permission for the backend to map103* the sole page in a single page sized ring buffer. This is the ring104* buffer for connection/disconnection requests.105*106* protocol107* Values: string (XEN_IO_PROTO_ABI_*)108* Default Value: XEN_IO_PROTO_ABI_NATIVE109*110* The machine ABI rules governing the format of all ring request and111* response structures.112*113* Protocol Description114* ====================115*116*-------------------------- USB device plug events --------------------------117*118* USB device plug events are send via the "conn-ring" shared page. As only119* events are being sent, the respective requests from the frontend to the120* backend are just dummy ones.121* The events sent to the frontend have the following layout:122* 0 1 2 3 octet123* +----------------+----------------+----------------+----------------+124* | id | portnum | speed | 4125* +----------------+----------------+----------------+----------------+126* id - uint16_t, event id (taken from the actual frontend dummy request)127* portnum - uint8_t, port number (1 ... 31)128* speed - uint8_t, device XENUSB_SPEED_*, XENUSB_SPEED_NONE == unplug129*130* The dummy request:131* 0 1 octet132* +----------------+----------------+133* | id | 2134* +----------------+----------------+135* id - uint16_t, guest supplied value (no need for being unique)136*137*-------------------------- USB I/O request ---------------------------------138*139* A single USB I/O request on the "urb-ring" has the following layout:140* 0 1 2 3 octet141* +----------------+----------------+----------------+----------------+142* | id | nr_buffer_segs | 4143* +----------------+----------------+----------------+----------------+144* | pipe | 8145* +----------------+----------------+----------------+----------------+146* | transfer_flags | buffer_length | 12147* +----------------+----------------+----------------+----------------+148* | request type specific | 16149* | data | 20150* +----------------+----------------+----------------+----------------+151* | seg[0] | 24152* | data | 28153* +----------------+----------------+----------------+----------------+154* |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|155* +----------------+----------------+----------------+----------------+156* | seg[XENUSB_MAX_SEGMENTS_PER_REQUEST - 1] | 144157* | data | 148158* +----------------+----------------+----------------+----------------+159* Bit field bit number 0 is always least significant bit, undefined bits must160* be zero.161* id - uint16_t, guest supplied value162* nr_buffer_segs - uint16_t, number of segment entries in seg[] array163* pipe - uint32_t, bit field with multiple information:164* bits 0-4: port request to send to165* bit 5: unlink request with specified id (cancel I/O) if set (see below)166* bit 7: direction (1 = read from device)167* bits 8-14: device number on port168* bits 15-18: endpoint of device169* bits 30-31: request type: 00 = isochronous, 01 = interrupt,170* 10 = control, 11 = bulk171* transfer_flags - uint16_t, bit field with processing flags:172* bit 0: less data than specified allowed173* buffer_length - uint16_t, total length of data174* request type specific data - 8 bytes, see below175* seg[] - array with 8 byte elements, see below176*177* Request type specific data for isochronous request:178* 0 1 2 3 octet179* +----------------+----------------+----------------+----------------+180* | interval | start_frame | 4181* +----------------+----------------+----------------+----------------+182* | number_of_packets | nr_frame_desc_segs | 8183* +----------------+----------------+----------------+----------------+184* interval - uint16_t, time interval in msecs between frames185* start_frame - uint16_t, start frame number186* number_of_packets - uint16_t, number of packets to transfer187* nr_frame_desc_segs - uint16_t number of seg[] frame descriptors elements188*189* Request type specific data for interrupt request:190* 0 1 2 3 octet191* +----------------+----------------+----------------+----------------+192* | interval | 0 | 4193* +----------------+----------------+----------------+----------------+194* | 0 | 8195* +----------------+----------------+----------------+----------------+196* interval - uint16_t, time in msecs until interruption197*198* Request type specific data for control request:199* 0 1 2 3 octet200* +----------------+----------------+----------------+----------------+201* | data of setup packet | 4202* | | 8203* +----------------+----------------+----------------+----------------+204*205* Request type specific data for bulk request:206* 0 1 2 3 octet207* +----------------+----------------+----------------+----------------+208* | 0 | 4209* | 0 | 8210* +----------------+----------------+----------------+----------------+211*212* Request type specific data for unlink request:213* 0 1 2 3 octet214* +----------------+----------------+----------------+----------------+215* | unlink_id | 0 | 4216* +----------------+----------------+----------------+----------------+217* | 0 | 8218* +----------------+----------------+----------------+----------------+219* unlink_id - uint16_t, request id of request to terminate220*221* seg[] array element layout:222* 0 1 2 3 octet223* +----------------+----------------+----------------+----------------+224* | gref | 4225* +----------------+----------------+----------------+----------------+226* | offset | length | 8227* +----------------+----------------+----------------+----------------+228* gref - uint32_t, grant reference of buffer page229* offset - uint16_t, offset of buffer start in page230* length - uint16_t, length of buffer in page231*232*-------------------------- USB I/O response --------------------------------233*234* 0 1 2 3 octet235* +----------------+----------------+----------------+----------------+236* | id | start_frame | 4237* +----------------+----------------+----------------+----------------+238* | status | 8239* +----------------+----------------+----------------+----------------+240* | actual_length | 12241* +----------------+----------------+----------------+----------------+242* | error_count | 16243* +----------------+----------------+----------------+----------------+244* id - uint16_t, id of the request this response belongs to245* start_frame - uint16_t, start_frame this response (iso requests only)246* status - int32_t, XENUSB_STATUS_* (non-iso requests)247* actual_length - uint32_t, actual size of data transferred248* error_count - uint32_t, number of errors (iso requests)249*/250251enum xenusb_spec_version {252XENUSB_VER_UNKNOWN = 0,253XENUSB_VER_USB11,254XENUSB_VER_USB20,255XENUSB_VER_USB30, /* not supported yet */256};257258/*259* USB pipe in xenusb_request260*261* - port number: bits 0-4262* (USB_MAXCHILDREN is 31)263*264* - operation flag: bit 5265* (0 = submit urb,266* 1 = unlink urb)267*268* - direction: bit 7269* (0 = Host-to-Device [Out]270* 1 = Device-to-Host [In])271*272* - device address: bits 8-14273*274* - endpoint: bits 15-18275*276* - pipe type: bits 30-31277* (00 = isochronous, 01 = interrupt,278* 10 = control, 11 = bulk)279*/280281#define XENUSB_PIPE_PORT_MASK 0x0000001f282#define XENUSB_PIPE_UNLINK 0x00000020283#define XENUSB_PIPE_DIR 0x00000080284#define XENUSB_PIPE_DEV_MASK 0x0000007f285#define XENUSB_PIPE_DEV_SHIFT 8286#define XENUSB_PIPE_EP_MASK 0x0000000f287#define XENUSB_PIPE_EP_SHIFT 15288#define XENUSB_PIPE_TYPE_MASK 0x00000003289#define XENUSB_PIPE_TYPE_SHIFT 30290#define XENUSB_PIPE_TYPE_ISOC 0291#define XENUSB_PIPE_TYPE_INT 1292#define XENUSB_PIPE_TYPE_CTRL 2293#define XENUSB_PIPE_TYPE_BULK 3294295#define xenusb_pipeportnum(pipe) ((pipe) & XENUSB_PIPE_PORT_MASK)296#define xenusb_setportnum_pipe(pipe, portnum) ((pipe) | (portnum))297298#define xenusb_pipeunlink(pipe) ((pipe) & XENUSB_PIPE_UNLINK)299#define xenusb_pipesubmit(pipe) (!xenusb_pipeunlink(pipe))300#define xenusb_setunlink_pipe(pipe) ((pipe) | XENUSB_PIPE_UNLINK)301302#define xenusb_pipein(pipe) ((pipe) & XENUSB_PIPE_DIR)303#define xenusb_pipeout(pipe) (!xenusb_pipein(pipe))304305#define xenusb_pipedevice(pipe) \306(((pipe) >> XENUSB_PIPE_DEV_SHIFT) & XENUSB_PIPE_DEV_MASK)307308#define xenusb_pipeendpoint(pipe) \309(((pipe) >> XENUSB_PIPE_EP_SHIFT) & XENUSB_PIPE_EP_MASK)310311#define xenusb_pipetype(pipe) \312(((pipe) >> XENUSB_PIPE_TYPE_SHIFT) & XENUSB_PIPE_TYPE_MASK)313#define xenusb_pipeisoc(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_ISOC)314#define xenusb_pipeint(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_INT)315#define xenusb_pipectrl(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_CTRL)316#define xenusb_pipebulk(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_BULK)317318#define XENUSB_MAX_SEGMENTS_PER_REQUEST (16)319#define XENUSB_MAX_PORTNR 31320#define XENUSB_RING_SIZE 4096321322/*323* RING for transferring urbs.324*/325struct xenusb_request_segment {326grant_ref_t gref;327uint16_t offset;328uint16_t length;329};330331struct xenusb_urb_request {332uint16_t id; /* request id */333uint16_t nr_buffer_segs; /* number of urb->transfer_buffer segments */334335/* basic urb parameter */336uint32_t pipe;337uint16_t transfer_flags;338#define XENUSB_SHORT_NOT_OK 0x0001339uint16_t buffer_length;340union {341uint8_t ctrl[8]; /* setup_packet (Ctrl) */342343struct {344uint16_t interval; /* maximum (1024*8) in usb core */345uint16_t start_frame; /* start frame */346uint16_t number_of_packets; /* number of ISO packet */347uint16_t nr_frame_desc_segs; /* number of iso_frame_desc segments */348} isoc;349350struct {351uint16_t interval; /* maximum (1024*8) in usb core */352uint16_t pad[3];353} intr;354355struct {356uint16_t unlink_id; /* unlink request id */357uint16_t pad[3];358} unlink;359360} u;361362/* urb data segments */363struct xenusb_request_segment seg[XENUSB_MAX_SEGMENTS_PER_REQUEST];364};365366struct xenusb_urb_response {367uint16_t id; /* request id */368uint16_t start_frame; /* start frame (ISO) */369int32_t status; /* status (non-ISO) */370#define XENUSB_STATUS_OK 0371#define XENUSB_STATUS_NODEV (-19)372#define XENUSB_STATUS_INVAL (-22)373#define XENUSB_STATUS_STALL (-32)374#define XENUSB_STATUS_IOERROR (-71)375#define XENUSB_STATUS_BABBLE (-75)376#define XENUSB_STATUS_SHUTDOWN (-108)377int32_t actual_length; /* actual transfer length */378int32_t error_count; /* number of ISO errors */379};380381DEFINE_RING_TYPES(xenusb_urb, struct xenusb_urb_request, struct xenusb_urb_response);382#define XENUSB_URB_RING_SIZE __CONST_RING_SIZE(xenusb_urb, XENUSB_RING_SIZE)383384/*385* RING for notifying connect/disconnect events to frontend386*/387struct xenusb_conn_request {388uint16_t id;389};390391struct xenusb_conn_response {392uint16_t id; /* request id */393uint8_t portnum; /* port number */394uint8_t speed; /* usb_device_speed */395#define XENUSB_SPEED_NONE 0396#define XENUSB_SPEED_LOW 1397#define XENUSB_SPEED_FULL 2398#define XENUSB_SPEED_HIGH 3399};400401DEFINE_RING_TYPES(xenusb_conn, struct xenusb_conn_request, struct xenusb_conn_response);402#define XENUSB_CONN_RING_SIZE __CONST_RING_SIZE(xenusb_conn, XENUSB_RING_SIZE)403404#endif /* __XEN_PUBLIC_IO_USBIF_H__ */405406407