xref: /freebsd/sys/contrib/xen/io/usbif.h (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 /*
2  * usbif.h
3  *
4  * USB I/O interface for Xen guest OSes.
5  *
6  * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
7  * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to
11  * deal in the Software without restriction, including without limitation the
12  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13  * sell copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
28 #ifndef __XEN_PUBLIC_IO_USBIF_H__
29 #define __XEN_PUBLIC_IO_USBIF_H__
30 
31 #include "ring.h"
32 #include "../grant_table.h"
33 
34 /*
35  * Detailed Interface Description
36  * ==============================
37  * The pvUSB interface is using a split driver design: a frontend driver in
38  * the guest and a backend driver in a driver domain (normally dom0) having
39  * access to the physical USB device(s) being passed to the guest.
40  *
41  * The frontend and backend drivers use XenStore to initiate the connection
42  * between them, the I/O activity is handled via two shared ring pages and an
43  * event channel. As the interface between frontend and backend is at the USB
44  * host connector level, multiple (up to 31) physical USB devices can be
45  * handled by a single connection.
46  *
47  * The Xen pvUSB device name is "qusb", so the frontend's XenStore entries are
48  * to be found under "device/qusb", while the backend's XenStore entries are
49  * under "backend/<guest-dom-id>/qusb".
50  *
51  * When a new pvUSB connection is established, the frontend needs to setup the
52  * two shared ring pages for communication and the event channel. The ring
53  * pages need to be made available to the backend via the grant table
54  * interface.
55  *
56  * One of the shared ring pages is used by the backend to inform the frontend
57  * about USB device plug events (device to be added or removed). This is the
58  * "conn-ring".
59  *
60  * The other ring page is used for USB I/O communication (requests and
61  * responses). This is the "urb-ring".
62  *
63  * Feature and Parameter Negotiation
64  * =================================
65  * The two halves of a Xen pvUSB driver utilize nodes within the XenStore to
66  * communicate capabilities and to negotiate operating parameters. This
67  * section enumerates these nodes which reside in the respective front and
68  * backend portions of the XenStore, following the XenBus convention.
69  *
70  * Any specified default value is in effect if the corresponding XenBus node
71  * is not present in the XenStore.
72  *
73  * XenStore nodes in sections marked "PRIVATE" are solely for use by the
74  * driver side whose XenBus tree contains them.
75  *
76  *****************************************************************************
77  *                            Backend XenBus Nodes
78  *****************************************************************************
79  *
80  *------------------ Backend Device Identification (PRIVATE) ------------------
81  *
82  * num-ports
83  *      Values:         unsigned [1...31]
84  *
85  *      Number of ports for this (virtual) USB host connector.
86  *
87  * usb-ver
88  *      Values:         unsigned [1...2]
89  *
90  *      USB version of this host connector: 1 = USB 1.1, 2 = USB 2.0.
91  *
92  * port/[1...31]
93  *      Values:         string
94  *
95  *      Physical USB device connected to the given port, e.g. "3-1.5".
96  *
97  *****************************************************************************
98  *                            Frontend XenBus Nodes
99  *****************************************************************************
100  *
101  *----------------------- Request Transport Parameters -----------------------
102  *
103  * event-channel
104  *      Values:         unsigned
105  *
106  *      The identifier of the Xen event channel used to signal activity
107  *      in the ring buffer.
108  *
109  * urb-ring-ref
110  *      Values:         unsigned
111  *
112  *      The Xen grant reference granting permission for the backend to map
113  *      the sole page in a single page sized ring buffer. This is the ring
114  *      buffer for urb requests.
115  *
116  * conn-ring-ref
117  *      Values:         unsigned
118  *
119  *      The Xen grant reference granting permission for the backend to map
120  *      the sole page in a single page sized ring buffer. This is the ring
121  *      buffer for connection/disconnection requests.
122  *
123  * protocol
124  *      Values:         string (XEN_IO_PROTO_ABI_*)
125  *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
126  *
127  *      The machine ABI rules governing the format of all ring request and
128  *      response structures.
129  *
130  * Protocol Description
131  * ====================
132  *
133  *-------------------------- USB device plug events --------------------------
134  *
135  * USB device plug events are send via the "conn-ring" shared page. As only
136  * events are being sent, the respective requests from the frontend to the
137  * backend are just dummy ones.
138  * The events sent to the frontend have the following layout:
139  *         0                1                 2               3        octet
140  * +----------------+----------------+----------------+----------------+
141  * |               id                |    portnum     |     speed      | 4
142  * +----------------+----------------+----------------+----------------+
143  *   id - uint16_t, event id (taken from the actual frontend dummy request)
144  *   portnum - uint8_t, port number (1 ... 31)
145  *   speed - uint8_t, device USBIF_SPEED_*, USBIF_SPEED_NONE == unplug
146  *
147  * The dummy request:
148  *         0                1        octet
149  * +----------------+----------------+
150  * |               id                | 2
151  * +----------------+----------------+
152  *   id - uint16_t, guest supplied value (no need for being unique)
153  *
154  *-------------------------- USB I/O request ---------------------------------
155  *
156  * A single USB I/O request on the "urb-ring" has the following layout:
157  *         0                1                 2               3        octet
158  * +----------------+----------------+----------------+----------------+
159  * |               id                |         nr_buffer_segs          | 4
160  * +----------------+----------------+----------------+----------------+
161  * |                               pipe                                | 8
162  * +----------------+----------------+----------------+----------------+
163  * |         transfer_flags          |          buffer_length          | 12
164  * +----------------+----------------+----------------+----------------+
165  * |                       request type specific                       | 16
166  * |                               data                                | 20
167  * +----------------+----------------+----------------+----------------+
168  * |                              seg[0]                               | 24
169  * |                               data                                | 28
170  * +----------------+----------------+----------------+----------------+
171  * |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|
172  * +----------------+----------------+----------------+----------------+
173  * |             seg[USBIF_MAX_SEGMENTS_PER_REQUEST - 1]               | 144
174  * |                               data                                | 148
175  * +----------------+----------------+----------------+----------------+
176  * Bit field bit number 0 is always least significant bit, undefined bits must
177  * be zero.
178  *   id - uint16_t, guest supplied value
179  *   nr_buffer_segs - uint16_t, number of segment entries in seg[] array
180  *   pipe - uint32_t, bit field with multiple information:
181  *     bits 0-4: port request to send to
182  *     bit 5: unlink request with specified id (cancel I/O) if set (see below)
183  *     bit 7: direction (1 = read from device)
184  *     bits 8-14: device number on port
185  *     bits 15-18: endpoint of device
186  *     bits 30-31: request type: 00 = isochronous, 01 = interrupt,
187  *                               10 = control, 11 = bulk
188  *   transfer_flags - uint16_t, bit field with processing flags:
189  *     bit 0: less data than specified allowed
190  *   buffer_length - uint16_t, total length of data
191  *   request type specific data - 8 bytes, see below
192  *   seg[] - array with 8 byte elements, see below
193  *
194  * Request type specific data for isochronous request:
195  *         0                1                 2               3        octet
196  * +----------------+----------------+----------------+----------------+
197  * |            interval             |           start_frame           | 4
198  * +----------------+----------------+----------------+----------------+
199  * |       number_of_packets         |       nr_frame_desc_segs        | 8
200  * +----------------+----------------+----------------+----------------+
201  *   interval - uint16_t, time interval in msecs between frames
202  *   start_frame - uint16_t, start frame number
203  *   number_of_packets - uint16_t, number of packets to transfer
204  *   nr_frame_desc_segs - uint16_t number of seg[] frame descriptors elements
205  *
206  * Request type specific data for interrupt request:
207  *         0                1                 2               3        octet
208  * +----------------+----------------+----------------+----------------+
209  * |            interval             |                0                | 4
210  * +----------------+----------------+----------------+----------------+
211  * |                                 0                                 | 8
212  * +----------------+----------------+----------------+----------------+
213  *   interval - uint16_t, time in msecs until interruption
214  *
215  * Request type specific data for control request:
216  *         0                1                 2               3        octet
217  * +----------------+----------------+----------------+----------------+
218  * |                      data of setup packet                         | 4
219  * |                                                                   | 8
220  * +----------------+----------------+----------------+----------------+
221  *
222  * Request type specific data for bulk request:
223  *         0                1                 2               3        octet
224  * +----------------+----------------+----------------+----------------+
225  * |                                 0                                 | 4
226  * |                                 0                                 | 8
227  * +----------------+----------------+----------------+----------------+
228  *
229  * Request type specific data for unlink request:
230  *         0                1                 2               3        octet
231  * +----------------+----------------+----------------+----------------+
232  * |           unlink_id             |                0                | 4
233  * +----------------+----------------+----------------+----------------+
234  * |                                 0                                 | 8
235  * +----------------+----------------+----------------+----------------+
236  *   unlink_id - uint16_t, request id of request to terminate
237  *
238  * seg[] array element layout:
239  *         0                1                 2               3        octet
240  * +----------------+----------------+----------------+----------------+
241  * |                               gref                                | 4
242  * +----------------+----------------+----------------+----------------+
243  * |             offset              |             length              | 8
244  * +----------------+----------------+----------------+----------------+
245  *   gref - uint32_t, grant reference of buffer page
246  *   offset - uint16_t, offset of buffer start in page
247  *   length - uint16_t, length of buffer in page
248  *
249  *-------------------------- USB I/O response --------------------------------
250  *
251  *         0                1                 2               3        octet
252  * +----------------+----------------+----------------+----------------+
253  * |               id                |          start_frame            | 4
254  * +----------------+----------------+----------------+----------------+
255  * |                              status                               | 8
256  * +----------------+----------------+----------------+----------------+
257  * |                          actual_length                            | 12
258  * +----------------+----------------+----------------+----------------+
259  * |                           error_count                             | 16
260  * +----------------+----------------+----------------+----------------+
261  *   id - uint16_t, id of the request this response belongs to
262  *   start_frame - uint16_t, start_frame this response (iso requests only)
263  *   status - int32_t, USBIF_STATUS_* (non-iso requests)
264  *   actual_length - uint32_t, actual size of data transferred
265  *   error_count - uint32_t, number of errors (iso requests)
266  */
267 
268 enum usb_spec_version {
269     USB_VER_UNKNOWN = 0,
270     USB_VER_USB11,
271     USB_VER_USB20,
272     USB_VER_USB30,    /* not supported yet */
273 };
274 
275 /*
276  *  USB pipe in usbif_request
277  *
278  *  - port number:      bits 0-4
279  *                              (USB_MAXCHILDREN is 31)
280  *
281  *  - operation flag:   bit 5
282  *                              (0 = submit urb,
283  *                               1 = unlink urb)
284  *
285  *  - direction:        bit 7
286  *                              (0 = Host-to-Device [Out]
287  *                               1 = Device-to-Host [In])
288  *
289  *  - device address:   bits 8-14
290  *
291  *  - endpoint:         bits 15-18
292  *
293  *  - pipe type:        bits 30-31
294  *                              (00 = isochronous, 01 = interrupt,
295  *                               10 = control, 11 = bulk)
296  */
297 
298 #define USBIF_PIPE_PORT_MASK    0x0000001f
299 #define USBIF_PIPE_UNLINK       0x00000020
300 #define USBIF_PIPE_DIR          0x00000080
301 #define USBIF_PIPE_DEV_MASK     0x0000007f
302 #define USBIF_PIPE_DEV_SHIFT    8
303 #define USBIF_PIPE_EP_MASK      0x0000000f
304 #define USBIF_PIPE_EP_SHIFT     15
305 #define USBIF_PIPE_TYPE_MASK    0x00000003
306 #define USBIF_PIPE_TYPE_SHIFT   30
307 #define USBIF_PIPE_TYPE_ISOC    0
308 #define USBIF_PIPE_TYPE_INT     1
309 #define USBIF_PIPE_TYPE_CTRL    2
310 #define USBIF_PIPE_TYPE_BULK    3
311 
312 #define usbif_pipeportnum(pipe)                 ((pipe) & USBIF_PIPE_PORT_MASK)
313 #define usbif_setportnum_pipe(pipe, portnum)    ((pipe) | (portnum))
314 
315 #define usbif_pipeunlink(pipe)                  ((pipe) & USBIF_PIPE_UNLINK)
316 #define usbif_pipesubmit(pipe)                  (!usbif_pipeunlink(pipe))
317 #define usbif_setunlink_pipe(pipe)              ((pipe) | USBIF_PIPE_UNLINK)
318 
319 #define usbif_pipein(pipe)                      ((pipe) & USBIF_PIPE_DIR)
320 #define usbif_pipeout(pipe)                     (!usbif_pipein(pipe))
321 
322 #define usbif_pipedevice(pipe)                  \
323         (((pipe) >> USBIF_PIPE_DEV_SHIFT) & USBIF_PIPE_DEV_MASK)
324 
325 #define usbif_pipeendpoint(pipe)                \
326         (((pipe) >> USBIF_PIPE_EP_SHIFT) & USBIF_PIPE_EP_MASK)
327 
328 #define usbif_pipetype(pipe)                    \
329         (((pipe) >> USBIF_PIPE_TYPE_SHIFT) & USBIF_PIPE_TYPE_MASK)
330 #define usbif_pipeisoc(pipe)    (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_ISOC)
331 #define usbif_pipeint(pipe)     (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_INT)
332 #define usbif_pipectrl(pipe)    (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_CTRL)
333 #define usbif_pipebulk(pipe)    (usbif_pipetype(pipe) == USBIF_PIPE_TYPE_BULK)
334 
335 #define USBIF_MAX_SEGMENTS_PER_REQUEST (16)
336 #define USBIF_MAX_PORTNR        31
337 #define USBIF_RING_SIZE         4096
338 
339 /*
340  * RING for transferring urbs.
341  */
342 struct usbif_request_segment {
343     grant_ref_t gref;
344     uint16_t offset;
345     uint16_t length;
346 };
347 
348 struct usbif_urb_request {
349     uint16_t id;                  /* request id */
350     uint16_t nr_buffer_segs;      /* number of urb->transfer_buffer segments */
351 
352     /* basic urb parameter */
353     uint32_t pipe;
354     uint16_t transfer_flags;
355 #define USBIF_SHORT_NOT_OK      0x0001
356     uint16_t buffer_length;
357     union {
358         uint8_t ctrl[8];                 /* setup_packet (Ctrl) */
359 
360         struct {
361             uint16_t interval;           /* maximum (1024*8) in usb core */
362             uint16_t start_frame;        /* start frame */
363             uint16_t number_of_packets;  /* number of ISO packet */
364             uint16_t nr_frame_desc_segs; /* number of iso_frame_desc segments */
365         } isoc;
366 
367         struct {
368             uint16_t interval;           /* maximum (1024*8) in usb core */
369             uint16_t pad[3];
370         } intr;
371 
372         struct {
373             uint16_t unlink_id;          /* unlink request id */
374             uint16_t pad[3];
375         } unlink;
376 
377     } u;
378 
379     /* urb data segments */
380     struct usbif_request_segment seg[USBIF_MAX_SEGMENTS_PER_REQUEST];
381 };
382 typedef struct usbif_urb_request usbif_urb_request_t;
383 
384 struct usbif_urb_response {
385     uint16_t id;           /* request id */
386     uint16_t start_frame;  /* start frame (ISO) */
387     int32_t status;        /* status (non-ISO) */
388 #define USBIF_STATUS_OK         0
389 #define USBIF_STATUS_NODEV      (-19)
390 #define USBIF_STATUS_INVAL      (-22)
391 #define USBIF_STATUS_STALL      (-32)
392 #define USBIF_STATUS_IOERROR    (-71)
393 #define USBIF_STATUS_BABBLE     (-75)
394 #define USBIF_STATUS_SHUTDOWN   (-108)
395     int32_t actual_length; /* actual transfer length */
396     int32_t error_count;   /* number of ISO errors */
397 };
398 typedef struct usbif_urb_response usbif_urb_response_t;
399 
400 DEFINE_RING_TYPES(usbif_urb, struct usbif_urb_request, struct usbif_urb_response);
401 #define USB_URB_RING_SIZE __CONST_RING_SIZE(usbif_urb, USBIF_RING_SIZE)
402 
403 /*
404  * RING for notifying connect/disconnect events to frontend
405  */
406 struct usbif_conn_request {
407     uint16_t id;
408 };
409 typedef struct usbif_conn_request usbif_conn_request_t;
410 
411 struct usbif_conn_response {
412     uint16_t id;           /* request id */
413     uint8_t portnum;       /* port number */
414     uint8_t speed;         /* usb_device_speed */
415 #define USBIF_SPEED_NONE        0
416 #define USBIF_SPEED_LOW         1
417 #define USBIF_SPEED_FULL        2
418 #define USBIF_SPEED_HIGH        3
419 };
420 typedef struct usbif_conn_response usbif_conn_response_t;
421 
422 DEFINE_RING_TYPES(usbif_conn, struct usbif_conn_request, struct usbif_conn_response);
423 #define USB_CONN_RING_SIZE __CONST_RING_SIZE(usbif_conn, USBIF_RING_SIZE)
424 
425 #endif /* __XEN_PUBLIC_IO_USBIF_H__ */
426