/*1* usbif.h2*3* USB I/O interface for Xen guest OSes.4*5* Copyright (C) 2009, FUJITSU LABORATORIES LTD.6* Author: Noboru Iwamatsu <[email protected]>7*8* Permission is hereby granted, free of charge, to any person obtaining a copy9* of this software and associated documentation files (the "Software"), to10* deal in the Software without restriction, including without limitation the11* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or12* sell copies of the Software, and to permit persons to whom the Software is13* furnished to do so, subject to the following conditions:14*15* The above copyright notice and this permission notice shall be included in16* all copies or substantial portions of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE21* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER22* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING23* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER24* DEALINGS IN THE SOFTWARE.25*/2627#ifndef __XEN_PUBLIC_IO_USBIF_H__28#define __XEN_PUBLIC_IO_USBIF_H__2930#include "ring.h"31#include "../grant_table.h"3233/*34* Detailed Interface Description35* ==============================36* The pvUSB interface is using a split driver design: a frontend driver in37* the guest and a backend driver in a driver domain (normally dom0) having38* access to the physical USB device(s) being passed to the guest.39*40* The frontend and backend drivers use XenStore to initiate the connection41* between them, the I/O activity is handled via two shared ring pages and an42* event channel. As the interface between frontend and backend is at the USB43* host connector level, multiple (up to 31) physical USB devices can be44* handled by a single connection.45*46* The Xen pvUSB device name is "qusb", so the frontend's XenStore entries are47* to be found under "device/qusb", while the backend's XenStore entries are48* under "backend/<guest-dom-id>/qusb".49*50* When a new pvUSB connection is established, the frontend needs to setup the51* two shared ring pages for communication and the event channel. The ring52* pages need to be made available to the backend via the grant table53* interface.54*55* One of the shared ring pages is used by the backend to inform the frontend56* about USB device plug events (device to be added or removed). This is the57* "conn-ring".58*59* The other ring page is used for USB I/O communication (requests and60* responses). This is the "urb-ring".61*62* Feature and Parameter Negotiation63* =================================64* The two halves of a Xen pvUSB driver utilize nodes within the XenStore to65* communicate capabilities and to negotiate operating parameters. This66* section enumerates these nodes which reside in the respective front and67* backend portions of the XenStore, following the XenBus convention.68*69* Any specified default value is in effect if the corresponding XenBus node70* is not present in the XenStore.71*72* XenStore nodes in sections marked "PRIVATE" are solely for use by the73* driver side whose XenBus tree contains them.74*75*****************************************************************************76* Backend XenBus Nodes77*****************************************************************************78*79*------------------ Backend Device Identification (PRIVATE) ------------------80*81* num-ports82* Values: unsigned [1...31]83*84* Number of ports for this (virtual) USB host connector.85*86* usb-ver87* Values: unsigned [1...2]88*89* USB version of this host connector: 1 = USB 1.1, 2 = USB 2.0.90*91* port/[1...31]92* Values: string93*94* Physical USB device connected to the given port, e.g. "3-1.5".95*96*****************************************************************************97* Frontend XenBus Nodes98*****************************************************************************99*100*----------------------- Request Transport Parameters -----------------------101*102* event-channel103* Values: unsigned104*105* The identifier of the Xen event channel used to signal activity106* in the ring buffer.107*108* urb-ring-ref109* Values: unsigned110*111* The Xen grant reference granting permission for the backend to map112* the sole page in a single page sized ring buffer. This is the ring113* buffer for urb requests.114*115* conn-ring-ref116* Values: unsigned117*118* The Xen grant reference granting permission for the backend to map119* the sole page in a single page sized ring buffer. This is the ring120* buffer for connection/disconnection requests.121*122* protocol123* Values: string (XEN_IO_PROTO_ABI_*)124* Default Value: XEN_IO_PROTO_ABI_NATIVE125*126* The machine ABI rules governing the format of all ring request and127* response structures.128*129* Protocol Description130* ====================131*132*-------------------------- USB device plug events --------------------------133*134* USB device plug events are send via the "conn-ring" shared page. As only135* events are being sent, the respective requests from the frontend to the136* backend are just dummy ones.137* The events sent to the frontend have the following layout:138* 0 1 2 3 octet139* +----------------+----------------+----------------+----------------+140* | id | portnum | speed | 4141* +----------------+----------------+----------------+----------------+142* id - uint16_t, event id (taken from the actual frontend dummy request)143* portnum - uint8_t, port number (1 ... 31)144* speed - uint8_t, device USBIF_SPEED_*, USBIF_SPEED_NONE == unplug145*146* The dummy request:147* 0 1 octet148* +----------------+----------------+149* | id | 2150* +----------------+----------------+151* id - uint16_t, guest supplied value (no need for being unique)152*153*-------------------------- USB I/O request ---------------------------------154*155* A single USB I/O request on the "urb-ring" has the following layout:156* 0 1 2 3 octet157* +----------------+----------------+----------------+----------------+158* | id | nr_buffer_segs | 4159* +----------------+----------------+----------------+----------------+160* | pipe | 8161* +----------------+----------------+----------------+----------------+162* | transfer_flags | buffer_length | 12163* +----------------+----------------+----------------+----------------+164* | request type specific | 16165* | data | 20166* +----------------+----------------+----------------+----------------+167* | seg[0] | 24168* | data | 28169* +----------------+----------------+----------------+----------------+170* |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|171* +----------------+----------------+----------------+----------------+172* | seg[USBIF_MAX_SEGMENTS_PER_REQUEST - 1] | 144173* | data | 148174* +----------------+----------------+----------------+----------------+175* Bit field bit number 0 is always least significant bit, undefined bits must176* be zero.177* id - uint16_t, guest supplied value178* nr_buffer_segs - uint16_t, number of segment entries in seg[] array179* pipe - uint32_t, bit field with multiple information:180* bits 0-4: port request to send to181* bit 5: unlink request with specified id (cancel I/O) if set (see below)182* bit 7: direction (1 = read from device)183* bits 8-14: device number on port184* bits 15-18: endpoint of device185* bits 30-31: request type: 00 = isochronous, 01 = interrupt,186* 10 = control, 11 = bulk187* transfer_flags - uint16_t, bit field with processing flags:188* bit 0: less data than specified allowed189* buffer_length - uint16_t, total length of data190* request type specific data - 8 bytes, see below191* seg[] - array with 8 byte elements, see below192*193* Request type specific data for isochronous request:194* 0 1 2 3 octet195* +----------------+----------------+----------------+----------------+196* | interval | start_frame | 4197* +----------------+----------------+----------------+----------------+198* | number_of_packets | nr_frame_desc_segs | 8199* +----------------+----------------+----------------+----------------+200* interval - uint16_t, time interval in msecs between frames201* start_frame - uint16_t, start frame number202* number_of_packets - uint16_t, number of packets to transfer203* nr_frame_desc_segs - uint16_t number of seg[] frame descriptors elements204*205* Request type specific data for interrupt request:206* 0 1 2 3 octet207* +----------------+----------------+----------------+----------------+208* | interval | 0 | 4209* +----------------+----------------+----------------+----------------+210* | 0 | 8211* +----------------+----------------+----------------+----------------+212* interval - uint16_t, time in msecs until interruption213*214* Request type specific data for control request:215* 0 1 2 3 octet216* +----------------+----------------+----------------+----------------+217* | data of setup packet | 4218* | | 8219* +----------------+----------------+----------------+----------------+220*221* Request type specific data for bulk request:222* 0 1 2 3 octet223* +----------------+----------------+----------------+----------------+224* | 0 | 4225* | 0 | 8226* +----------------+----------------+----------------+----------------+227*228* Request type specific data for unlink request:229* 0 1 2 3 octet230* +----------------+----------------+----------------+----------------+231* | unlink_id | 0 | 4232* +----------------+----------------+----------------+----------------+233* | 0 | 8234* +----------------+----------------+----------------+----------------+235* unlink_id - uint16_t, request id of request to terminate236*237* seg[] array element layout:238* 0 1 2 3 octet239* +----------------+----------------+----------------+----------------+240* | gref | 4241* +----------------+----------------+----------------+----------------+242* | offset | length | 8243* +----------------+----------------+----------------+----------------+244* gref - uint32_t, grant reference of buffer page245* offset - uint16_t, offset of buffer start in page246* length - uint16_t, length of buffer in page247*248*-------------------------- USB I/O response --------------------------------249*250* 0 1 2 3 octet251* +----------------+----------------+----------------+----------------+252* | id | start_frame | 4253* +----------------+----------------+----------------+----------------+254* | status | 8255* +----------------+----------------+----------------+----------------+256* | actual_length | 12257* +----------------+----------------+----------------+----------------+258* | error_count | 16259* +----------------+----------------+----------------+----------------+260* id - uint16_t, id of the request this response belongs to261* start_frame - uint16_t, start_frame this response (iso requests only)262* status - int32_t, USBIF_STATUS_* (non-iso requests)263* actual_length - uint32_t, actual size of data transferred264* error_count - uint32_t, number of errors (iso requests)265*/266267enum usb_spec_version {268USB_VER_UNKNOWN = 0,269USB_VER_USB11,270USB_VER_USB20,271USB_VER_USB30, /* not supported yet */272};273274/*275* USB pipe in usbif_request276*277* - port number: bits 0-4278* (USB_MAXCHILDREN is 31)279*280* - operation flag: bit 5281* (0 = submit urb,282* 1 = unlink urb)283*284* - direction: bit 7285* (0 = Host-to-Device [Out]286* 1 = Device-to-Host [In])287*288* - device address: bits 8-14289*290* - endpoint: bits 15-18291*292* - pipe type: bits 30-31293* (00 = isochronous, 01 = interrupt,294* 10 = control, 11 = bulk)295*/296297#define USBIF_PIPE_PORT_MASK 0x0000001f298#define USBIF_PIPE_UNLINK 0x00000020299#define USBIF_PIPE_DIR 0x00000080300#define USBIF_PIPE_DEV_MASK 0x0000007f301#define USBIF_PIPE_DEV_SHIFT 8302#define USBIF_PIPE_EP_MASK 0x0000000f303#define USBIF_PIPE_EP_SHIFT 15304#define USBIF_PIPE_TYPE_MASK 0x00000003305#define USBIF_PIPE_TYPE_SHIFT 30306#define USBIF_PIPE_TYPE_ISOC 0307#define USBIF_PIPE_TYPE_INT 1308#define USBIF_PIPE_TYPE_CTRL 2309#define USBIF_PIPE_TYPE_BULK 3310311#define usbif_pipeportnum(pipe) ((pipe) & USBIF_PIPE_PORT_MASK)312#define usbif_setportnum_pipe(pipe, portnum) ((pipe) | (portnum))313314#define usbif_pipeunlink(pipe) ((pipe) & USBIF_PIPE_UNLINK)315#define usbif_pipesubmit(pipe) (!usbif_pipeunlink(pipe))316#define usbif_setunlink_pipe(pipe) ((pipe) | USBIF_PIPE_UNLINK)317318#define usbif_pipein(pipe) ((pipe) & USBIF_PIPE_DIR)319#define usbif_pipeout(pipe) (!usbif_pipein(pipe))320321#define usbif_pipedevice(pipe) \322(((pipe) >> USBIF_PIPE_DEV_SHIFT) & USBIF_PIPE_DEV_MASK)323324#define usbif_pipeendpoint(pipe) \325(((pipe) >> USBIF_PIPE_EP_SHIFT) & USBIF_PIPE_EP_MASK)326327#define usbif_pipetype(pipe) \328(((pipe) >> USBIF_PIPE_TYPE_SHIFT) & USBIF_PIPE_TYPE_MASK)329#define usbif_pipeisoc(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_ISOC)330#define usbif_pipeint(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_INT)331#define usbif_pipectrl(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_CTRL)332#define usbif_pipebulk(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_BULK)333334#define USBIF_MAX_SEGMENTS_PER_REQUEST (16)335#define USBIF_MAX_PORTNR 31336#define USBIF_RING_SIZE 4096337338/*339* RING for transferring urbs.340*/341struct usbif_request_segment {342grant_ref_t gref;343uint16_t offset;344uint16_t length;345};346347struct usbif_urb_request {348uint16_t id; /* request id */349uint16_t nr_buffer_segs; /* number of urb->transfer_buffer segments */350351/* basic urb parameter */352uint32_t pipe;353uint16_t transfer_flags;354#define USBIF_SHORT_NOT_OK 0x0001355uint16_t buffer_length;356union {357uint8_t ctrl[8]; /* setup_packet (Ctrl) */358359struct {360uint16_t interval; /* maximum (1024*8) in usb core */361uint16_t start_frame; /* start frame */362uint16_t number_of_packets; /* number of ISO packet */363uint16_t nr_frame_desc_segs; /* number of iso_frame_desc segments */364} isoc;365366struct {367uint16_t interval; /* maximum (1024*8) in usb core */368uint16_t pad[3];369} intr;370371struct {372uint16_t unlink_id; /* unlink request id */373uint16_t pad[3];374} unlink;375376} u;377378/* urb data segments */379struct usbif_request_segment seg[USBIF_MAX_SEGMENTS_PER_REQUEST];380};381typedef struct usbif_urb_request usbif_urb_request_t;382383struct usbif_urb_response {384uint16_t id; /* request id */385uint16_t start_frame; /* start frame (ISO) */386int32_t status; /* status (non-ISO) */387#define USBIF_STATUS_OK 0388#define USBIF_STATUS_NODEV (-19)389#define USBIF_STATUS_INVAL (-22)390#define USBIF_STATUS_STALL (-32)391#define USBIF_STATUS_IOERROR (-71)392#define USBIF_STATUS_BABBLE (-75)393#define USBIF_STATUS_SHUTDOWN (-108)394int32_t actual_length; /* actual transfer length */395int32_t error_count; /* number of ISO errors */396};397typedef struct usbif_urb_response usbif_urb_response_t;398399DEFINE_RING_TYPES(usbif_urb, struct usbif_urb_request, struct usbif_urb_response);400#define USB_URB_RING_SIZE __CONST_RING_SIZE(usbif_urb, USBIF_RING_SIZE)401402/*403* RING for notifying connect/disconnect events to frontend404*/405struct usbif_conn_request {406uint16_t id;407};408typedef struct usbif_conn_request usbif_conn_request_t;409410struct usbif_conn_response {411uint16_t id; /* request id */412uint8_t portnum; /* port number */413uint8_t speed; /* usb_device_speed */414#define USBIF_SPEED_NONE 0415#define USBIF_SPEED_LOW 1416#define USBIF_SPEED_FULL 2417#define USBIF_SPEED_HIGH 3418};419typedef struct usbif_conn_response usbif_conn_response_t;420421DEFINE_RING_TYPES(usbif_conn, struct usbif_conn_request, struct usbif_conn_response);422#define USB_CONN_RING_SIZE __CONST_RING_SIZE(usbif_conn, USBIF_RING_SIZE)423424#endif /* __XEN_PUBLIC_IO_USBIF_H__ */425426427