xref: /openbmc/qemu/include/hw/xen/interface/io/usbif.h (revision ec6f3fc3)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * usbif.h
4  *
5  * USB I/O interface for Xen guest OSes.
6  *
7  * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
8  * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
9  */
10 
11 #ifndef __XEN_PUBLIC_IO_USBIF_H__
12 #define __XEN_PUBLIC_IO_USBIF_H__
13 
14 #include "ring.h"
15 #include "../grant_table.h"
16 
17 /*
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.
23  *
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.
29  *
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".
33  *
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
37  * interface.
38  *
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
41  * "conn-ring".
42  *
43  * The other ring page is used for USB I/O communication (requests and
44  * responses). This is the "urb-ring".
45  *
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.
52  *
53  * Any specified default value is in effect if the corresponding XenBus node
54  * is not present in the XenStore.
55  *
56  * XenStore nodes in sections marked "PRIVATE" are solely for use by the
57  * driver side whose XenBus tree contains them.
58  *
59  *****************************************************************************
60  *                            Backend XenBus Nodes
61  *****************************************************************************
62  *
63  *------------------ Backend Device Identification (PRIVATE) ------------------
64  *
65  * num-ports
66  *      Values:         unsigned [1...31]
67  *
68  *      Number of ports for this (virtual) USB host connector.
69  *
70  * usb-ver
71  *      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:         string
77  *
78  *      Physical USB device connected to the given port, e.g. "3-1.5".
79  *
80  *****************************************************************************
81  *                            Frontend XenBus Nodes
82  *****************************************************************************
83  *
84  *----------------------- Request Transport Parameters -----------------------
85  *
86  * event-channel
87  *      Values:         unsigned
88  *
89  *      The identifier of the Xen event channel used to signal activity
90  *      in the ring buffer.
91  *
92  * urb-ring-ref
93  *      Values:         unsigned
94  *
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.
98  *
99  * conn-ring-ref
100  *      Values:         unsigned
101  *
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.
105  *
106  * protocol
107  *      Values:         string (XEN_IO_PROTO_ABI_*)
108  *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
109  *
110  *      The machine ABI rules governing the format of all ring request and
111  *      response structures.
112  *
113  * Protocol Description
114  * ====================
115  *
116  *-------------------------- USB device plug events --------------------------
117  *
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:
122  *         0                1                 2               3        octet
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
129  *
130  * The dummy request:
131  *         0                1        octet
132  * +----------------+----------------+
133  * |               id                | 2
134  * +----------------+----------------+
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        octet
141  * +----------------+----------------+----------------+----------------+
142  * |               id                |         nr_buffer_segs          | 4
143  * +----------------+----------------+----------------+----------------+
144  * |                               pipe                                | 8
145  * +----------------+----------------+----------------+----------------+
146  * |         transfer_flags          |          buffer_length          | 12
147  * +----------------+----------------+----------------+----------------+
148  * |                       request type specific                       | 16
149  * |                               data                                | 20
150  * +----------------+----------------+----------------+----------------+
151  * |                              seg[0]                               | 24
152  * |                               data                                | 28
153  * +----------------+----------------+----------------+----------------+
154  * |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|
155  * +----------------+----------------+----------------+----------------+
156  * |             seg[USBIF_MAX_SEGMENTS_PER_REQUEST - 1]               | 144
157  * |                               data                                | 148
158  * +----------------+----------------+----------------+----------------+
159  * Bit field bit number 0 is always least significant bit, undefined bits must
160  * be zero.
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
176  *
177  * Request type specific data for isochronous request:
178  *         0                1                 2               3        octet
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
188  *
189  * Request type specific data for interrupt request:
190  *         0                1                 2               3        octet
191  * +----------------+----------------+----------------+----------------+
192  * |            interval             |                0                | 4
193  * +----------------+----------------+----------------+----------------+
194  * |                                 0                                 | 8
195  * +----------------+----------------+----------------+----------------+
196  *   interval - uint16_t, time in msecs until interruption
197  *
198  * Request type specific data for control request:
199  *         0                1                 2               3        octet
200  * +----------------+----------------+----------------+----------------+
201  * |                      data of setup packet                         | 4
202  * |                                                                   | 8
203  * +----------------+----------------+----------------+----------------+
204  *
205  * Request type specific data for bulk request:
206  *         0                1                 2               3        octet
207  * +----------------+----------------+----------------+----------------+
208  * |                                 0                                 | 4
209  * |                                 0                                 | 8
210  * +----------------+----------------+----------------+----------------+
211  *
212  * Request type specific data for unlink request:
213  *         0                1                 2               3        octet
214  * +----------------+----------------+----------------+----------------+
215  * |           unlink_id             |                0                | 4
216  * +----------------+----------------+----------------+----------------+
217  * |                                 0                                 | 8
218  * +----------------+----------------+----------------+----------------+
219  *   unlink_id - uint16_t, request id of request to terminate
220  *
221  * seg[] array element layout:
222  *         0                1                 2               3        octet
223  * +----------------+----------------+----------------+----------------+
224  * |                               gref                                | 4
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
231  *
232  *-------------------------- USB I/O response --------------------------------
233  *
234  *         0                1                 2               3        octet
235  * +----------------+----------------+----------------+----------------+
236  * |               id                |          start_frame            | 4
237  * +----------------+----------------+----------------+----------------+
238  * |                              status                               | 8
239  * +----------------+----------------+----------------+----------------+
240  * |                          actual_length                            | 12
241  * +----------------+----------------+----------------+----------------+
242  * |                           error_count                             | 16
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)
249  */
250 
251 enum usb_spec_version {
252     USB_VER_UNKNOWN = 0,
253     USB_VER_USB11,
254     USB_VER_USB20,
255     USB_VER_USB30,    /* not supported yet */
256 };
257 
258 /*
259  *  USB pipe in usbif_request
260  *
261  *  - port number:      bits 0-4
262  *                              (USB_MAXCHILDREN is 31)
263  *
264  *  - operation flag:   bit 5
265  *                              (0 = submit urb,
266  *                               1 = unlink urb)
267  *
268  *  - direction:        bit 7
269  *                              (0 = Host-to-Device [Out]
270  *                               1 = Device-to-Host [In])
271  *
272  *  - device address:   bits 8-14
273  *
274  *  - endpoint:         bits 15-18
275  *
276  *  - pipe type:        bits 30-31
277  *                              (00 = isochronous, 01 = interrupt,
278  *                               10 = control, 11 = bulk)
279  */
280 
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
294 
295 #define usbif_pipeportnum(pipe)                 ((pipe) & USBIF_PIPE_PORT_MASK)
296 #define usbif_setportnum_pipe(pipe, portnum)    ((pipe) | (portnum))
297 
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)
301 
302 #define usbif_pipein(pipe)                      ((pipe) & USBIF_PIPE_DIR)
303 #define usbif_pipeout(pipe)                     (!usbif_pipein(pipe))
304 
305 #define usbif_pipedevice(pipe)                  \
306         (((pipe) >> USBIF_PIPE_DEV_SHIFT) & USBIF_PIPE_DEV_MASK)
307 
308 #define usbif_pipeendpoint(pipe)                \
309         (((pipe) >> USBIF_PIPE_EP_SHIFT) & USBIF_PIPE_EP_MASK)
310 
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)
317 
318 #define USBIF_MAX_SEGMENTS_PER_REQUEST (16)
319 #define USBIF_MAX_PORTNR        31
320 #define USBIF_RING_SIZE         4096
321 
322 /*
323  * RING for transferring urbs.
324  */
325 struct usbif_request_segment {
326     grant_ref_t gref;
327     uint16_t offset;
328     uint16_t length;
329 };
330 
331 struct usbif_urb_request {
332     uint16_t id;                  /* request id */
333     uint16_t nr_buffer_segs;      /* number of urb->transfer_buffer segments */
334 
335     /* basic urb parameter */
336     uint32_t pipe;
337     uint16_t transfer_flags;
338 #define USBIF_SHORT_NOT_OK      0x0001
339     uint16_t buffer_length;
340     union {
341         uint8_t ctrl[8];                 /* setup_packet (Ctrl) */
342 
343         struct {
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 */
348         } isoc;
349 
350         struct {
351             uint16_t interval;           /* maximum (1024*8) in usb core */
352             uint16_t pad[3];
353         } intr;
354 
355         struct {
356             uint16_t unlink_id;          /* unlink request id */
357             uint16_t pad[3];
358         } unlink;
359 
360     } u;
361 
362     /* urb data segments */
363     struct usbif_request_segment seg[USBIF_MAX_SEGMENTS_PER_REQUEST];
364 };
365 typedef struct usbif_urb_request usbif_urb_request_t;
366 
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 */
380 };
381 typedef struct usbif_urb_response usbif_urb_response_t;
382 
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)
385 
386 /*
387  * RING for notifying connect/disconnect events to frontend
388  */
389 struct usbif_conn_request {
390     uint16_t id;
391 };
392 typedef struct usbif_conn_request usbif_conn_request_t;
393 
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
402 };
403 typedef struct usbif_conn_response usbif_conn_response_t;
404 
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)
407 
408 #endif /* __XEN_PUBLIC_IO_USBIF_H__ */
409