1 /* SPDX-License-Identifier: MIT */
5 * USB I/O interface for Xen guest OSes.
7 * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
8 * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
11 #ifndef __XEN_PUBLIC_IO_USBIF_H__
12 #define __XEN_PUBLIC_IO_USBIF_H__
15 #include "../grant_table.h"
18 * Detailed Interface Description
19 * ==============================
20 * The pvUSB interface is using a split driver design: a frontend driver in
21 * the guest and a backend driver in a driver domain (normally dom0) having
22 * access to the physical USB device(s) being passed to the guest.
24 * The frontend and backend drivers use XenStore to initiate the connection
25 * between them, the I/O activity is handled via two shared ring pages and an
26 * event channel. As the interface between frontend and backend is at the USB
27 * host connector level, multiple (up to 31) physical USB devices can be
28 * handled by a single connection.
30 * The Xen pvUSB device name is "qusb", so the frontend's XenStore entries are
31 * to be found under "device/qusb", while the backend's XenStore entries are
32 * under "backend/<guest-dom-id>/qusb".
34 * When a new pvUSB connection is established, the frontend needs to setup the
35 * two shared ring pages for communication and the event channel. The ring
36 * pages need to be made available to the backend via the grant table
39 * One of the shared ring pages is used by the backend to inform the frontend
40 * about USB device plug events (device to be added or removed). This is the
43 * The other ring page is used for USB I/O communication (requests and
44 * responses). This is the "urb-ring".
46 * Feature and Parameter Negotiation
47 * =================================
48 * The two halves of a Xen pvUSB driver utilize nodes within the XenStore to
49 * communicate capabilities and to negotiate operating parameters. This
50 * section enumerates these nodes which reside in the respective front and
51 * backend portions of the XenStore, following the XenBus convention.
53 * Any specified default value is in effect if the corresponding XenBus node
54 * is not present in the XenStore.
56 * XenStore nodes in sections marked "PRIVATE" are solely for use by the
57 * driver side whose XenBus tree contains them.
59 *****************************************************************************
60 * Backend XenBus Nodes
61 *****************************************************************************
63 *------------------ Backend Device Identification (PRIVATE) ------------------
66 * Values: unsigned [1...31]
68 * Number of ports for this (virtual) USB host connector.
71 * Values: unsigned [1...2]
73 * USB version of this host connector: 1 = USB 1.1, 2 = USB 2.0.
78 * Physical USB device connected to the given port, e.g. "3-1.5".
80 *****************************************************************************
81 * Frontend XenBus Nodes
82 *****************************************************************************
84 *----------------------- Request Transport Parameters -----------------------
89 * The identifier of the Xen event channel used to signal activity
95 * The Xen grant reference granting permission for the backend to map
96 * the sole page in a single page sized ring buffer. This is the ring
97 * buffer for urb requests.
102 * The Xen grant reference granting permission for the backend to map
103 * the sole page in a single page sized ring buffer. This is the ring
104 * buffer for connection/disconnection requests.
107 * Values: string (XEN_IO_PROTO_ABI_*)
108 * Default Value: XEN_IO_PROTO_ABI_NATIVE
110 * The machine ABI rules governing the format of all ring request and
111 * response structures.
113 * Protocol Description
114 * ====================
116 *-------------------------- USB device plug events --------------------------
118 * USB device plug events are send via the "conn-ring" shared page. As only
119 * events are being sent, the respective requests from the frontend to the
120 * backend are just dummy ones.
121 * The events sent to the frontend have the following layout:
123 * +----------------+----------------+----------------+----------------+
124 * | id | portnum | speed | 4
125 * +----------------+----------------+----------------+----------------+
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 USBIF_SPEED_*, USBIF_SPEED_NONE == unplug
132 * +----------------+----------------+
134 * +----------------+----------------+
135 * id - uint16_t, guest supplied value (no need for being unique)
137 *-------------------------- USB I/O request ---------------------------------
139 * A single USB I/O request on the "urb-ring" has the following layout:
141 * +----------------+----------------+----------------+----------------+
142 * | id | nr_buffer_segs | 4
143 * +----------------+----------------+----------------+----------------+
145 * +----------------+----------------+----------------+----------------+
146 * | transfer_flags | buffer_length | 12
147 * +----------------+----------------+----------------+----------------+
148 * | request type specific | 16
150 * +----------------+----------------+----------------+----------------+
153 * +----------------+----------------+----------------+----------------+
154 * |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|
155 * +----------------+----------------+----------------+----------------+
156 * | seg[USBIF_MAX_SEGMENTS_PER_REQUEST - 1] | 144
158 * +----------------+----------------+----------------+----------------+
159 * Bit field bit number 0 is always least significant bit, undefined bits must
161 * id - uint16_t, guest supplied value
162 * nr_buffer_segs - uint16_t, number of segment entries in seg[] array
163 * pipe - uint32_t, bit field with multiple information:
164 * bits 0-4: port request to send to
165 * 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 port
168 * bits 15-18: endpoint of device
169 * bits 30-31: request type: 00 = isochronous, 01 = interrupt,
170 * 10 = control, 11 = bulk
171 * transfer_flags - uint16_t, bit field with processing flags:
172 * bit 0: less data than specified allowed
173 * buffer_length - uint16_t, total length of data
174 * request type specific data - 8 bytes, see below
175 * seg[] - array with 8 byte elements, see below
177 * Request type specific data for isochronous request:
179 * +----------------+----------------+----------------+----------------+
180 * | interval | start_frame | 4
181 * +----------------+----------------+----------------+----------------+
182 * | number_of_packets | nr_frame_desc_segs | 8
183 * +----------------+----------------+----------------+----------------+
184 * interval - uint16_t, time interval in msecs between frames
185 * start_frame - uint16_t, start frame number
186 * number_of_packets - uint16_t, number of packets to transfer
187 * nr_frame_desc_segs - uint16_t number of seg[] frame descriptors elements
189 * Request type specific data for interrupt request:
191 * +----------------+----------------+----------------+----------------+
193 * +----------------+----------------+----------------+----------------+
195 * +----------------+----------------+----------------+----------------+
196 * interval - uint16_t, time in msecs until interruption
198 * Request type specific data for control request:
200 * +----------------+----------------+----------------+----------------+
201 * | data of setup packet | 4
203 * +----------------+----------------+----------------+----------------+
205 * Request type specific data for bulk request:
207 * +----------------+----------------+----------------+----------------+
210 * +----------------+----------------+----------------+----------------+
212 * Request type specific data for unlink request:
214 * +----------------+----------------+----------------+----------------+
215 * | unlink_id | 0 | 4
216 * +----------------+----------------+----------------+----------------+
218 * +----------------+----------------+----------------+----------------+
219 * unlink_id - uint16_t, request id of request to terminate
221 * seg[] array element layout:
223 * +----------------+----------------+----------------+----------------+
225 * +----------------+----------------+----------------+----------------+
226 * | offset | length | 8
227 * +----------------+----------------+----------------+----------------+
228 * gref - uint32_t, grant reference of buffer page
229 * offset - uint16_t, offset of buffer start in page
230 * length - uint16_t, length of buffer in page
232 *-------------------------- USB I/O response --------------------------------
235 * +----------------+----------------+----------------+----------------+
236 * | id | start_frame | 4
237 * +----------------+----------------+----------------+----------------+
239 * +----------------+----------------+----------------+----------------+
240 * | actual_length | 12
241 * +----------------+----------------+----------------+----------------+
243 * +----------------+----------------+----------------+----------------+
244 * id - uint16_t, id of the request this response belongs to
245 * start_frame - uint16_t, start_frame this response (iso requests only)
246 * status - int32_t, USBIF_STATUS_* (non-iso requests)
247 * actual_length - uint32_t, actual size of data transferred
248 * error_count - uint32_t, number of errors (iso requests)
251 enum usb_spec_version
{
255 USB_VER_USB30
, /* not supported yet */
259 * USB pipe in usbif_request
261 * - port number: bits 0-4
262 * (USB_MAXCHILDREN is 31)
264 * - operation flag: bit 5
269 * (0 = Host-to-Device [Out]
270 * 1 = Device-to-Host [In])
272 * - device address: bits 8-14
274 * - endpoint: bits 15-18
276 * - pipe type: bits 30-31
277 * (00 = isochronous, 01 = interrupt,
278 * 10 = control, 11 = bulk)
281 #define USBIF_PIPE_PORT_MASK 0x0000001f
282 #define USBIF_PIPE_UNLINK 0x00000020
283 #define USBIF_PIPE_DIR 0x00000080
284 #define USBIF_PIPE_DEV_MASK 0x0000007f
285 #define USBIF_PIPE_DEV_SHIFT 8
286 #define USBIF_PIPE_EP_MASK 0x0000000f
287 #define USBIF_PIPE_EP_SHIFT 15
288 #define USBIF_PIPE_TYPE_MASK 0x00000003
289 #define USBIF_PIPE_TYPE_SHIFT 30
290 #define USBIF_PIPE_TYPE_ISOC 0
291 #define USBIF_PIPE_TYPE_INT 1
292 #define USBIF_PIPE_TYPE_CTRL 2
293 #define USBIF_PIPE_TYPE_BULK 3
295 #define usbif_pipeportnum(pipe) ((pipe) & USBIF_PIPE_PORT_MASK)
296 #define usbif_setportnum_pipe(pipe, portnum) ((pipe) | (portnum))
298 #define usbif_pipeunlink(pipe) ((pipe) & USBIF_PIPE_UNLINK)
299 #define usbif_pipesubmit(pipe) (!usbif_pipeunlink(pipe))
300 #define usbif_setunlink_pipe(pipe) ((pipe) | USBIF_PIPE_UNLINK)
302 #define usbif_pipein(pipe) ((pipe) & USBIF_PIPE_DIR)
303 #define usbif_pipeout(pipe) (!usbif_pipein(pipe))
305 #define usbif_pipedevice(pipe) \
306 (((pipe) >> USBIF_PIPE_DEV_SHIFT) & USBIF_PIPE_DEV_MASK)
308 #define usbif_pipeendpoint(pipe) \
309 (((pipe) >> USBIF_PIPE_EP_SHIFT) & USBIF_PIPE_EP_MASK)
311 #define usbif_pipetype(pipe) \
312 (((pipe) >> USBIF_PIPE_TYPE_SHIFT) & USBIF_PIPE_TYPE_MASK)
313 #define usbif_pipeisoc(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_ISOC)
314 #define usbif_pipeint(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_INT)
315 #define usbif_pipectrl(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_CTRL)
316 #define usbif_pipebulk(pipe) (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_BULK)
318 #define USBIF_MAX_SEGMENTS_PER_REQUEST (16)
319 #define USBIF_MAX_PORTNR 31
320 #define USBIF_RING_SIZE 4096
323 * RING for transferring urbs.
325 struct usbif_request_segment
{
331 struct usbif_urb_request
{
332 uint16_t id
; /* request id */
333 uint16_t nr_buffer_segs
; /* number of urb->transfer_buffer segments */
335 /* basic urb parameter */
337 uint16_t transfer_flags
;
338 #define USBIF_SHORT_NOT_OK 0x0001
339 uint16_t buffer_length
;
341 uint8_t ctrl
[8]; /* setup_packet (Ctrl) */
344 uint16_t interval
; /* maximum (1024*8) in usb core */
345 uint16_t start_frame
; /* start frame */
346 uint16_t number_of_packets
; /* number of ISO packet */
347 uint16_t nr_frame_desc_segs
; /* number of iso_frame_desc segments */
351 uint16_t interval
; /* maximum (1024*8) in usb core */
356 uint16_t unlink_id
; /* unlink request id */
362 /* urb data segments */
363 struct usbif_request_segment seg
[USBIF_MAX_SEGMENTS_PER_REQUEST
];
365 typedef struct usbif_urb_request usbif_urb_request_t
;
367 struct usbif_urb_response
{
368 uint16_t id
; /* request id */
369 uint16_t start_frame
; /* start frame (ISO) */
370 int32_t status
; /* status (non-ISO) */
371 #define USBIF_STATUS_OK 0
372 #define USBIF_STATUS_NODEV (-19)
373 #define USBIF_STATUS_INVAL (-22)
374 #define USBIF_STATUS_STALL (-32)
375 #define USBIF_STATUS_IOERROR (-71)
376 #define USBIF_STATUS_BABBLE (-75)
377 #define USBIF_STATUS_SHUTDOWN (-108)
378 int32_t actual_length
; /* actual transfer length */
379 int32_t error_count
; /* number of ISO errors */
381 typedef struct usbif_urb_response usbif_urb_response_t
;
383 DEFINE_RING_TYPES(usbif_urb
, struct usbif_urb_request
, struct usbif_urb_response
);
384 #define USB_URB_RING_SIZE __CONST_RING_SIZE(usbif_urb, USBIF_RING_SIZE)
387 * RING for notifying connect/disconnect events to frontend
389 struct usbif_conn_request
{
392 typedef struct usbif_conn_request usbif_conn_request_t
;
394 struct usbif_conn_response
{
395 uint16_t id
; /* request id */
396 uint8_t portnum
; /* port number */
397 uint8_t speed
; /* usb_device_speed */
398 #define USBIF_SPEED_NONE 0
399 #define USBIF_SPEED_LOW 1
400 #define USBIF_SPEED_FULL 2
401 #define USBIF_SPEED_HIGH 3
403 typedef struct usbif_conn_response usbif_conn_response_t
;
405 DEFINE_RING_TYPES(usbif_conn
, struct usbif_conn_request
, struct usbif_conn_response
);
406 #define USB_CONN_RING_SIZE __CONST_RING_SIZE(usbif_conn, USBIF_RING_SIZE)
408 #endif /* __XEN_PUBLIC_IO_USBIF_H__ */