Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: MIT */
0002 
0003 /*
0004  * usbif.h
0005  *
0006  * USB I/O interface for Xen guest OSes.
0007  *
0008  * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
0009  * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
0010  */
0011 
0012 #ifndef __XEN_PUBLIC_IO_USBIF_H__
0013 #define __XEN_PUBLIC_IO_USBIF_H__
0014 
0015 #include "ring.h"
0016 #include "../grant_table.h"
0017 
0018 /*
0019  * Detailed Interface Description
0020  * ==============================
0021  * The pvUSB interface is using a split driver design: a frontend driver in
0022  * the guest and a backend driver in a driver domain (normally dom0) having
0023  * access to the physical USB device(s) being passed to the guest.
0024  *
0025  * The frontend and backend drivers use XenStore to initiate the connection
0026  * between them, the I/O activity is handled via two shared ring pages and an
0027  * event channel. As the interface between frontend and backend is at the USB
0028  * host connector level, multiple (up to 31) physical USB devices can be
0029  * handled by a single connection.
0030  *
0031  * The Xen pvUSB device name is "qusb", so the frontend's XenStore entries are
0032  * to be found under "device/qusb", while the backend's XenStore entries are
0033  * under "backend/<guest-dom-id>/qusb".
0034  *
0035  * When a new pvUSB connection is established, the frontend needs to setup the
0036  * two shared ring pages for communication and the event channel. The ring
0037  * pages need to be made available to the backend via the grant table
0038  * interface.
0039  *
0040  * One of the shared ring pages is used by the backend to inform the frontend
0041  * about USB device plug events (device to be added or removed). This is the
0042  * "conn-ring".
0043  *
0044  * The other ring page is used for USB I/O communication (requests and
0045  * responses). This is the "urb-ring".
0046  *
0047  * Feature and Parameter Negotiation
0048  * =================================
0049  * The two halves of a Xen pvUSB driver utilize nodes within the XenStore to
0050  * communicate capabilities and to negotiate operating parameters. This
0051  * section enumerates these nodes which reside in the respective front and
0052  * backend portions of the XenStore, following the XenBus convention.
0053  *
0054  * Any specified default value is in effect if the corresponding XenBus node
0055  * is not present in the XenStore.
0056  *
0057  * XenStore nodes in sections marked "PRIVATE" are solely for use by the
0058  * driver side whose XenBus tree contains them.
0059  *
0060  *****************************************************************************
0061  *                            Backend XenBus Nodes
0062  *****************************************************************************
0063  *
0064  *------------------ Backend Device Identification (PRIVATE) ------------------
0065  *
0066  * num-ports
0067  *      Values:         unsigned [1...31]
0068  *
0069  *      Number of ports for this (virtual) USB host connector.
0070  *
0071  * usb-ver
0072  *      Values:         unsigned [1...2]
0073  *
0074  *      USB version of this host connector: 1 = USB 1.1, 2 = USB 2.0.
0075  *
0076  * port/[1...31]
0077  *      Values:         string
0078  *
0079  *      Physical USB device connected to the given port, e.g. "3-1.5".
0080  *
0081  *****************************************************************************
0082  *                            Frontend XenBus Nodes
0083  *****************************************************************************
0084  *
0085  *----------------------- Request Transport Parameters -----------------------
0086  *
0087  * event-channel
0088  *      Values:         unsigned
0089  *
0090  *      The identifier of the Xen event channel used to signal activity
0091  *      in the ring buffer.
0092  *
0093  * urb-ring-ref
0094  *      Values:         unsigned
0095  *
0096  *      The Xen grant reference granting permission for the backend to map
0097  *      the sole page in a single page sized ring buffer. This is the ring
0098  *      buffer for urb requests.
0099  *
0100  * conn-ring-ref
0101  *      Values:         unsigned
0102  *
0103  *      The Xen grant reference granting permission for the backend to map
0104  *      the sole page in a single page sized ring buffer. This is the ring
0105  *      buffer for connection/disconnection requests.
0106  *
0107  * protocol
0108  *      Values:         string (XEN_IO_PROTO_ABI_*)
0109  *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
0110  *
0111  *      The machine ABI rules governing the format of all ring request and
0112  *      response structures.
0113  *
0114  * Protocol Description
0115  * ====================
0116  *
0117  *-------------------------- USB device plug events --------------------------
0118  *
0119  * USB device plug events are send via the "conn-ring" shared page. As only
0120  * events are being sent, the respective requests from the frontend to the
0121  * backend are just dummy ones.
0122  * The events sent to the frontend have the following layout:
0123  *         0                1                 2               3        octet
0124  * +----------------+----------------+----------------+----------------+
0125  * |               id                |    portnum     |     speed      | 4
0126  * +----------------+----------------+----------------+----------------+
0127  *   id - uint16_t, event id (taken from the actual frontend dummy request)
0128  *   portnum - uint8_t, port number (1 ... 31)
0129  *   speed - uint8_t, device XENUSB_SPEED_*, XENUSB_SPEED_NONE == unplug
0130  *
0131  * The dummy request:
0132  *         0                1        octet
0133  * +----------------+----------------+
0134  * |               id                | 2
0135  * +----------------+----------------+
0136  *   id - uint16_t, guest supplied value (no need for being unique)
0137  *
0138  *-------------------------- USB I/O request ---------------------------------
0139  *
0140  * A single USB I/O request on the "urb-ring" has the following layout:
0141  *         0                1                 2               3        octet
0142  * +----------------+----------------+----------------+----------------+
0143  * |               id                |         nr_buffer_segs          | 4
0144  * +----------------+----------------+----------------+----------------+
0145  * |                               pipe                                | 8
0146  * +----------------+----------------+----------------+----------------+
0147  * |         transfer_flags          |          buffer_length          | 12
0148  * +----------------+----------------+----------------+----------------+
0149  * |                       request type specific                       | 16
0150  * |                               data                                | 20
0151  * +----------------+----------------+----------------+----------------+
0152  * |                              seg[0]                               | 24
0153  * |                               data                                | 28
0154  * +----------------+----------------+----------------+----------------+
0155  * |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|
0156  * +----------------+----------------+----------------+----------------+
0157  * |             seg[XENUSB_MAX_SEGMENTS_PER_REQUEST - 1]              | 144
0158  * |                               data                                | 148
0159  * +----------------+----------------+----------------+----------------+
0160  * Bit field bit number 0 is always least significant bit, undefined bits must
0161  * be zero.
0162  *   id - uint16_t, guest supplied value
0163  *   nr_buffer_segs - uint16_t, number of segment entries in seg[] array
0164  *   pipe - uint32_t, bit field with multiple information:
0165  *     bits 0-4: port request to send to
0166  *     bit 5: unlink request with specified id (cancel I/O) if set (see below)
0167  *     bit 7: direction (1 = read from device)
0168  *     bits 8-14: device number on port
0169  *     bits 15-18: endpoint of device
0170  *     bits 30-31: request type: 00 = isochronous, 01 = interrupt,
0171  *                               10 = control, 11 = bulk
0172  *   transfer_flags - uint16_t, bit field with processing flags:
0173  *     bit 0: less data than specified allowed
0174  *   buffer_length - uint16_t, total length of data
0175  *   request type specific data - 8 bytes, see below
0176  *   seg[] - array with 8 byte elements, see below
0177  *
0178  * Request type specific data for isochronous request:
0179  *         0                1                 2               3        octet
0180  * +----------------+----------------+----------------+----------------+
0181  * |            interval             |           start_frame           | 4
0182  * +----------------+----------------+----------------+----------------+
0183  * |       number_of_packets         |       nr_frame_desc_segs        | 8
0184  * +----------------+----------------+----------------+----------------+
0185  *   interval - uint16_t, time interval in msecs between frames
0186  *   start_frame - uint16_t, start frame number
0187  *   number_of_packets - uint16_t, number of packets to transfer
0188  *   nr_frame_desc_segs - uint16_t number of seg[] frame descriptors elements
0189  *
0190  * Request type specific data for interrupt request:
0191  *         0                1                 2               3        octet
0192  * +----------------+----------------+----------------+----------------+
0193  * |            interval             |                0                | 4
0194  * +----------------+----------------+----------------+----------------+
0195  * |                                 0                                 | 8
0196  * +----------------+----------------+----------------+----------------+
0197  *   interval - uint16_t, time in msecs until interruption
0198  *
0199  * Request type specific data for control request:
0200  *         0                1                 2               3        octet
0201  * +----------------+----------------+----------------+----------------+
0202  * |                      data of setup packet                         | 4
0203  * |                                                                   | 8
0204  * +----------------+----------------+----------------+----------------+
0205  *
0206  * Request type specific data for bulk request:
0207  *         0                1                 2               3        octet
0208  * +----------------+----------------+----------------+----------------+
0209  * |                                 0                                 | 4
0210  * |                                 0                                 | 8
0211  * +----------------+----------------+----------------+----------------+
0212  *
0213  * Request type specific data for unlink request:
0214  *         0                1                 2               3        octet
0215  * +----------------+----------------+----------------+----------------+
0216  * |           unlink_id             |                0                | 4
0217  * +----------------+----------------+----------------+----------------+
0218  * |                                 0                                 | 8
0219  * +----------------+----------------+----------------+----------------+
0220  *   unlink_id - uint16_t, request id of request to terminate
0221  *
0222  * seg[] array element layout:
0223  *         0                1                 2               3        octet
0224  * +----------------+----------------+----------------+----------------+
0225  * |                               gref                                | 4
0226  * +----------------+----------------+----------------+----------------+
0227  * |             offset              |             length              | 8
0228  * +----------------+----------------+----------------+----------------+
0229  *   gref - uint32_t, grant reference of buffer page
0230  *   offset - uint16_t, offset of buffer start in page
0231  *   length - uint16_t, length of buffer in page
0232  *
0233  *-------------------------- USB I/O response --------------------------------
0234  *
0235  *         0                1                 2               3        octet
0236  * +----------------+----------------+----------------+----------------+
0237  * |               id                |          start_frame            | 4
0238  * +----------------+----------------+----------------+----------------+
0239  * |                              status                               | 8
0240  * +----------------+----------------+----------------+----------------+
0241  * |                          actual_length                            | 12
0242  * +----------------+----------------+----------------+----------------+
0243  * |                           error_count                             | 16
0244  * +----------------+----------------+----------------+----------------+
0245  *   id - uint16_t, id of the request this response belongs to
0246  *   start_frame - uint16_t, start_frame this response (iso requests only)
0247  *   status - int32_t, XENUSB_STATUS_* (non-iso requests)
0248  *   actual_length - uint32_t, actual size of data transferred
0249  *   error_count - uint32_t, number of errors (iso requests)
0250  */
0251 
0252 enum xenusb_spec_version {
0253     XENUSB_VER_UNKNOWN = 0,
0254     XENUSB_VER_USB11,
0255     XENUSB_VER_USB20,
0256     XENUSB_VER_USB30,   /* not supported yet */
0257 };
0258 
0259 /*
0260  *  USB pipe in xenusb_request
0261  *
0262  *  - port number:      bits 0-4
0263  *                              (USB_MAXCHILDREN is 31)
0264  *
0265  *  - operation flag:   bit 5
0266  *                              (0 = submit urb,
0267  *                               1 = unlink urb)
0268  *
0269  *  - direction:        bit 7
0270  *                              (0 = Host-to-Device [Out]
0271  *                               1 = Device-to-Host [In])
0272  *
0273  *  - device address:   bits 8-14
0274  *
0275  *  - endpoint:         bits 15-18
0276  *
0277  *  - pipe type:        bits 30-31
0278  *                              (00 = isochronous, 01 = interrupt,
0279  *                               10 = control, 11 = bulk)
0280  */
0281 
0282 #define XENUSB_PIPE_PORT_MASK   0x0000001f
0283 #define XENUSB_PIPE_UNLINK  0x00000020
0284 #define XENUSB_PIPE_DIR     0x00000080
0285 #define XENUSB_PIPE_DEV_MASK    0x0000007f
0286 #define XENUSB_PIPE_DEV_SHIFT   8
0287 #define XENUSB_PIPE_EP_MASK 0x0000000f
0288 #define XENUSB_PIPE_EP_SHIFT    15
0289 #define XENUSB_PIPE_TYPE_MASK   0x00000003
0290 #define XENUSB_PIPE_TYPE_SHIFT  30
0291 #define XENUSB_PIPE_TYPE_ISOC   0
0292 #define XENUSB_PIPE_TYPE_INT    1
0293 #define XENUSB_PIPE_TYPE_CTRL   2
0294 #define XENUSB_PIPE_TYPE_BULK   3
0295 
0296 #define xenusb_pipeportnum(pipe)        ((pipe) & XENUSB_PIPE_PORT_MASK)
0297 #define xenusb_setportnum_pipe(pipe, portnum)   ((pipe) | (portnum))
0298 
0299 #define xenusb_pipeunlink(pipe)         ((pipe) & XENUSB_PIPE_UNLINK)
0300 #define xenusb_pipesubmit(pipe)         (!xenusb_pipeunlink(pipe))
0301 #define xenusb_setunlink_pipe(pipe)     ((pipe) | XENUSB_PIPE_UNLINK)
0302 
0303 #define xenusb_pipein(pipe)         ((pipe) & XENUSB_PIPE_DIR)
0304 #define xenusb_pipeout(pipe)            (!xenusb_pipein(pipe))
0305 
0306 #define xenusb_pipedevice(pipe)         \
0307     (((pipe) >> XENUSB_PIPE_DEV_SHIFT) & XENUSB_PIPE_DEV_MASK)
0308 
0309 #define xenusb_pipeendpoint(pipe)       \
0310     (((pipe) >> XENUSB_PIPE_EP_SHIFT) & XENUSB_PIPE_EP_MASK)
0311 
0312 #define xenusb_pipetype(pipe)           \
0313     (((pipe) >> XENUSB_PIPE_TYPE_SHIFT) & XENUSB_PIPE_TYPE_MASK)
0314 #define xenusb_pipeisoc(pipe)   (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_ISOC)
0315 #define xenusb_pipeint(pipe)    (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_INT)
0316 #define xenusb_pipectrl(pipe)   (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_CTRL)
0317 #define xenusb_pipebulk(pipe)   (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_BULK)
0318 
0319 #define XENUSB_MAX_SEGMENTS_PER_REQUEST (16)
0320 #define XENUSB_MAX_PORTNR       31
0321 #define XENUSB_RING_SIZE        4096
0322 
0323 /*
0324  * RING for transferring urbs.
0325  */
0326 struct xenusb_request_segment {
0327     grant_ref_t gref;
0328     uint16_t offset;
0329     uint16_t length;
0330 };
0331 
0332 struct xenusb_urb_request {
0333     uint16_t id;            /* request id */
0334     uint16_t nr_buffer_segs;    /* number of urb->transfer_buffer segments */
0335 
0336     /* basic urb parameter */
0337     uint32_t pipe;
0338     uint16_t transfer_flags;
0339 #define XENUSB_SHORT_NOT_OK 0x0001
0340     uint16_t buffer_length;
0341     union {
0342         uint8_t ctrl[8];    /* setup_packet (Ctrl) */
0343 
0344         struct {
0345             uint16_t interval;  /* maximum (1024*8) in usb core */
0346             uint16_t start_frame;   /* start frame */
0347             uint16_t number_of_packets; /* number of ISO packet */
0348             uint16_t nr_frame_desc_segs;    /* number of iso_frame_desc segments */
0349         } isoc;
0350 
0351         struct {
0352             uint16_t interval;  /* maximum (1024*8) in usb core */
0353             uint16_t pad[3];
0354         } intr;
0355 
0356         struct {
0357             uint16_t unlink_id; /* unlink request id */
0358             uint16_t pad[3];
0359         } unlink;
0360 
0361     } u;
0362 
0363     /* urb data segments */
0364     struct xenusb_request_segment seg[XENUSB_MAX_SEGMENTS_PER_REQUEST];
0365 };
0366 
0367 struct xenusb_urb_response {
0368     uint16_t id;        /* request id */
0369     uint16_t start_frame;   /* start frame (ISO) */
0370     int32_t status;     /* status (non-ISO) */
0371 #define XENUSB_STATUS_OK    0
0372 #define XENUSB_STATUS_NODEV (-19)
0373 #define XENUSB_STATUS_INVAL (-22)
0374 #define XENUSB_STATUS_STALL (-32)
0375 #define XENUSB_STATUS_IOERROR   (-71)
0376 #define XENUSB_STATUS_BABBLE    (-75)
0377 #define XENUSB_STATUS_SHUTDOWN  (-108)
0378     int32_t actual_length;  /* actual transfer length */
0379     int32_t error_count;    /* number of ISO errors */
0380 };
0381 
0382 DEFINE_RING_TYPES(xenusb_urb, struct xenusb_urb_request, struct xenusb_urb_response);
0383 #define XENUSB_URB_RING_SIZE __CONST_RING_SIZE(xenusb_urb, XENUSB_RING_SIZE)
0384 
0385 /*
0386  * RING for notifying connect/disconnect events to frontend
0387  */
0388 struct xenusb_conn_request {
0389     uint16_t id;
0390 };
0391 
0392 struct xenusb_conn_response {
0393     uint16_t id;        /* request id */
0394     uint8_t portnum;    /* port number */
0395     uint8_t speed;      /* usb_device_speed */
0396 #define XENUSB_SPEED_NONE   0
0397 #define XENUSB_SPEED_LOW    1
0398 #define XENUSB_SPEED_FULL   2
0399 #define XENUSB_SPEED_HIGH   3
0400 };
0401 
0402 DEFINE_RING_TYPES(xenusb_conn, struct xenusb_conn_request, struct xenusb_conn_response);
0403 #define XENUSB_CONN_RING_SIZE __CONST_RING_SIZE(xenusb_conn, XENUSB_RING_SIZE)
0404 
0405 #endif /* __XEN_PUBLIC_IO_USBIF_H__ */