1 /* SPDX-License-Identifier: MIT */
6 * USB I/O interface for Xen guest OSes.
8 * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
9 * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
12 #ifndef __XEN_PUBLIC_IO_USBIF_H__
13 #define __XEN_PUBLIC_IO_USBIF_H__
16 #include "../grant_table.h"
19 * Detailed Interface Description
20 * ==============================
21 * The pvUSB interface is using a split driver design: a frontend driver in
22 * the guest and a backend driver in a driver domain (normally dom0) having
23 * access to the physical USB device(s) being passed to the guest.
25 * The frontend and backend drivers use XenStore to initiate the connection
26 * between them, the I/O activity is handled via two shared ring pages and an
27 * event channel. As the interface between frontend and backend is at the USB
28 * host connector level, multiple (up to 31) physical USB devices can be
29 * handled by a single connection.
31 * The Xen pvUSB device name is "qusb", so the frontend's XenStore entries are
32 * to be found under "device/qusb", while the backend's XenStore entries are
33 * under "backend/<guest-dom-id>/qusb".
35 * When a new pvUSB connection is established, the frontend needs to setup the
36 * two shared ring pages for communication and the event channel. The ring
37 * pages need to be made available to the backend via the grant table
40 * One of the shared ring pages is used by the backend to inform the frontend
41 * about USB device plug events (device to be added or removed). This is the
44 * The other ring page is used for USB I/O communication (requests and
45 * responses). This is the "urb-ring".
47 * Feature and Parameter Negotiation
48 * =================================
49 * The two halves of a Xen pvUSB driver utilize nodes within the XenStore to
50 * communicate capabilities and to negotiate operating parameters. This
51 * section enumerates these nodes which reside in the respective front and
52 * backend portions of the XenStore, following the XenBus convention.
54 * Any specified default value is in effect if the corresponding XenBus node
55 * is not present in the XenStore.
57 * XenStore nodes in sections marked "PRIVATE" are solely for use by the
58 * driver side whose XenBus tree contains them.
60 *****************************************************************************
61 * Backend XenBus Nodes
62 *****************************************************************************
64 *------------------ Backend Device Identification (PRIVATE) ------------------
67 * Values: unsigned [1...31]
69 * Number of ports for this (virtual) USB host connector.
72 * Values: unsigned [1...2]
74 * USB version of this host connector: 1 = USB 1.1, 2 = USB 2.0.
79 * Physical USB device connected to the given port, e.g. "3-1.5".
81 *****************************************************************************
82 * Frontend XenBus Nodes
83 *****************************************************************************
85 *----------------------- Request Transport Parameters -----------------------
90 * The identifier of the Xen event channel used to signal activity
96 * The Xen grant reference granting permission for the backend to map
97 * the sole page in a single page sized ring buffer. This is the ring
98 * buffer for urb requests.
103 * The Xen grant reference granting permission for the backend to map
104 * the sole page in a single page sized ring buffer. This is the ring
105 * buffer for connection/disconnection requests.
108 * Values: string (XEN_IO_PROTO_ABI_*)
109 * Default Value: XEN_IO_PROTO_ABI_NATIVE
111 * The machine ABI rules governing the format of all ring request and
112 * response structures.
114 * Protocol Description
115 * ====================
117 *-------------------------- USB device plug events --------------------------
119 * USB device plug events are send via the "conn-ring" shared page. As only
120 * events are being sent, the respective requests from the frontend to the
121 * backend are just dummy ones.
122 * The events sent to the frontend have the following layout:
124 * +----------------+----------------+----------------+----------------+
125 * | id | portnum | speed | 4
126 * +----------------+----------------+----------------+----------------+
127 * id - uint16_t, event id (taken from the actual frontend dummy request)
128 * portnum - uint8_t, port number (1 ... 31)
129 * speed - uint8_t, device XENUSB_SPEED_*, XENUSB_SPEED_NONE == unplug
133 * +----------------+----------------+
135 * +----------------+----------------+
136 * id - uint16_t, guest supplied value (no need for being unique)
138 *-------------------------- USB I/O request ---------------------------------
140 * A single USB I/O request on the "urb-ring" has the following layout:
142 * +----------------+----------------+----------------+----------------+
143 * | id | nr_buffer_segs | 4
144 * +----------------+----------------+----------------+----------------+
146 * +----------------+----------------+----------------+----------------+
147 * | transfer_flags | buffer_length | 12
148 * +----------------+----------------+----------------+----------------+
149 * | request type specific | 16
151 * +----------------+----------------+----------------+----------------+
154 * +----------------+----------------+----------------+----------------+
155 * |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|
156 * +----------------+----------------+----------------+----------------+
157 * | seg[XENUSB_MAX_SEGMENTS_PER_REQUEST - 1] | 144
159 * +----------------+----------------+----------------+----------------+
160 * Bit field bit number 0 is always least significant bit, undefined bits must
162 * id - uint16_t, guest supplied value
163 * nr_buffer_segs - uint16_t, number of segment entries in seg[] array
164 * pipe - uint32_t, bit field with multiple information:
165 * bits 0-4: port request to send to
166 * bit 5: unlink request with specified id (cancel I/O) if set (see below)
167 * bit 7: direction (1 = read from device)
168 * bits 8-14: device number on port
169 * bits 15-18: endpoint of device
170 * bits 30-31: request type: 00 = isochronous, 01 = interrupt,
171 * 10 = control, 11 = bulk
172 * transfer_flags - uint16_t, bit field with processing flags:
173 * bit 0: less data than specified allowed
174 * buffer_length - uint16_t, total length of data
175 * request type specific data - 8 bytes, see below
176 * seg[] - array with 8 byte elements, see below
178 * Request type specific data for isochronous request:
180 * +----------------+----------------+----------------+----------------+
181 * | interval | start_frame | 4
182 * +----------------+----------------+----------------+----------------+
183 * | number_of_packets | nr_frame_desc_segs | 8
184 * +----------------+----------------+----------------+----------------+
185 * interval - uint16_t, time interval in msecs between frames
186 * start_frame - uint16_t, start frame number
187 * number_of_packets - uint16_t, number of packets to transfer
188 * nr_frame_desc_segs - uint16_t number of seg[] frame descriptors elements
190 * Request type specific data for interrupt request:
192 * +----------------+----------------+----------------+----------------+
194 * +----------------+----------------+----------------+----------------+
196 * +----------------+----------------+----------------+----------------+
197 * interval - uint16_t, time in msecs until interruption
199 * Request type specific data for control request:
201 * +----------------+----------------+----------------+----------------+
202 * | data of setup packet | 4
204 * +----------------+----------------+----------------+----------------+
206 * Request type specific data for bulk request:
208 * +----------------+----------------+----------------+----------------+
211 * +----------------+----------------+----------------+----------------+
213 * Request type specific data for unlink request:
215 * +----------------+----------------+----------------+----------------+
216 * | unlink_id | 0 | 4
217 * +----------------+----------------+----------------+----------------+
219 * +----------------+----------------+----------------+----------------+
220 * unlink_id - uint16_t, request id of request to terminate
222 * seg[] array element layout:
224 * +----------------+----------------+----------------+----------------+
226 * +----------------+----------------+----------------+----------------+
227 * | offset | length | 8
228 * +----------------+----------------+----------------+----------------+
229 * gref - uint32_t, grant reference of buffer page
230 * offset - uint16_t, offset of buffer start in page
231 * length - uint16_t, length of buffer in page
233 *-------------------------- USB I/O response --------------------------------
236 * +----------------+----------------+----------------+----------------+
237 * | id | start_frame | 4
238 * +----------------+----------------+----------------+----------------+
240 * +----------------+----------------+----------------+----------------+
241 * | actual_length | 12
242 * +----------------+----------------+----------------+----------------+
244 * +----------------+----------------+----------------+----------------+
245 * id - uint16_t, id of the request this response belongs to
246 * start_frame - uint16_t, start_frame this response (iso requests only)
247 * status - int32_t, XENUSB_STATUS_* (non-iso requests)
248 * actual_length - uint32_t, actual size of data transferred
249 * error_count - uint32_t, number of errors (iso requests)
252 enum xenusb_spec_version {
253 XENUSB_VER_UNKNOWN = 0,
256 XENUSB_VER_USB30, /* not supported yet */
260 * USB pipe in xenusb_request
262 * - port number: bits 0-4
263 * (USB_MAXCHILDREN is 31)
265 * - operation flag: bit 5
270 * (0 = Host-to-Device [Out]
271 * 1 = Device-to-Host [In])
273 * - device address: bits 8-14
275 * - endpoint: bits 15-18
277 * - pipe type: bits 30-31
278 * (00 = isochronous, 01 = interrupt,
279 * 10 = control, 11 = bulk)
282 #define XENUSB_PIPE_PORT_MASK 0x0000001f
283 #define XENUSB_PIPE_UNLINK 0x00000020
284 #define XENUSB_PIPE_DIR 0x00000080
285 #define XENUSB_PIPE_DEV_MASK 0x0000007f
286 #define XENUSB_PIPE_DEV_SHIFT 8
287 #define XENUSB_PIPE_EP_MASK 0x0000000f
288 #define XENUSB_PIPE_EP_SHIFT 15
289 #define XENUSB_PIPE_TYPE_MASK 0x00000003
290 #define XENUSB_PIPE_TYPE_SHIFT 30
291 #define XENUSB_PIPE_TYPE_ISOC 0
292 #define XENUSB_PIPE_TYPE_INT 1
293 #define XENUSB_PIPE_TYPE_CTRL 2
294 #define XENUSB_PIPE_TYPE_BULK 3
296 #define xenusb_pipeportnum(pipe) ((pipe) & XENUSB_PIPE_PORT_MASK)
297 #define xenusb_setportnum_pipe(pipe, portnum) ((pipe) | (portnum))
299 #define xenusb_pipeunlink(pipe) ((pipe) & XENUSB_PIPE_UNLINK)
300 #define xenusb_pipesubmit(pipe) (!xenusb_pipeunlink(pipe))
301 #define xenusb_setunlink_pipe(pipe) ((pipe) | XENUSB_PIPE_UNLINK)
303 #define xenusb_pipein(pipe) ((pipe) & XENUSB_PIPE_DIR)
304 #define xenusb_pipeout(pipe) (!xenusb_pipein(pipe))
306 #define xenusb_pipedevice(pipe) \
307 (((pipe) >> XENUSB_PIPE_DEV_SHIFT) & XENUSB_PIPE_DEV_MASK)
309 #define xenusb_pipeendpoint(pipe) \
310 (((pipe) >> XENUSB_PIPE_EP_SHIFT) & XENUSB_PIPE_EP_MASK)
312 #define xenusb_pipetype(pipe) \
313 (((pipe) >> XENUSB_PIPE_TYPE_SHIFT) & XENUSB_PIPE_TYPE_MASK)
314 #define xenusb_pipeisoc(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_ISOC)
315 #define xenusb_pipeint(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_INT)
316 #define xenusb_pipectrl(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_CTRL)
317 #define xenusb_pipebulk(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_BULK)
319 #define XENUSB_MAX_SEGMENTS_PER_REQUEST (16)
320 #define XENUSB_MAX_PORTNR 31
321 #define XENUSB_RING_SIZE 4096
324 * RING for transferring urbs.
326 struct xenusb_request_segment {
332 struct xenusb_urb_request {
333 uint16_t id; /* request id */
334 uint16_t nr_buffer_segs; /* number of urb->transfer_buffer segments */
336 /* basic urb parameter */
338 uint16_t transfer_flags;
339 #define XENUSB_SHORT_NOT_OK 0x0001
340 uint16_t buffer_length;
342 uint8_t ctrl[8]; /* setup_packet (Ctrl) */
345 uint16_t interval; /* maximum (1024*8) in usb core */
346 uint16_t start_frame; /* start frame */
347 uint16_t number_of_packets; /* number of ISO packet */
348 uint16_t nr_frame_desc_segs; /* number of iso_frame_desc segments */
352 uint16_t interval; /* maximum (1024*8) in usb core */
357 uint16_t unlink_id; /* unlink request id */
363 /* urb data segments */
364 struct xenusb_request_segment seg[XENUSB_MAX_SEGMENTS_PER_REQUEST];
367 struct xenusb_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 XENUSB_STATUS_OK 0
372 #define XENUSB_STATUS_NODEV (-19)
373 #define XENUSB_STATUS_INVAL (-22)
374 #define XENUSB_STATUS_STALL (-32)
375 #define XENUSB_STATUS_IOERROR (-71)
376 #define XENUSB_STATUS_BABBLE (-75)
377 #define XENUSB_STATUS_SHUTDOWN (-108)
378 int32_t actual_length; /* actual transfer length */
379 int32_t error_count; /* number of ISO errors */
382 DEFINE_RING_TYPES(xenusb_urb, struct xenusb_urb_request, struct xenusb_urb_response);
383 #define XENUSB_URB_RING_SIZE __CONST_RING_SIZE(xenusb_urb, XENUSB_RING_SIZE)
386 * RING for notifying connect/disconnect events to frontend
388 struct xenusb_conn_request {
392 struct xenusb_conn_response {
393 uint16_t id; /* request id */
394 uint8_t portnum; /* port number */
395 uint8_t speed; /* usb_device_speed */
396 #define XENUSB_SPEED_NONE 0
397 #define XENUSB_SPEED_LOW 1
398 #define XENUSB_SPEED_FULL 2
399 #define XENUSB_SPEED_HIGH 3
402 DEFINE_RING_TYPES(xenusb_conn, struct xenusb_conn_request, struct xenusb_conn_response);
403 #define XENUSB_CONN_RING_SIZE __CONST_RING_SIZE(xenusb_conn, XENUSB_RING_SIZE)
405 #endif /* __XEN_PUBLIC_IO_USBIF_H__ */