xref: /freebsd/usr.sbin/bhyve/usb_emul.h (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014 Leon Dang <ldang@nahannisys.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _USB_EMUL_H_
32 #define _USB_EMUL_H_
33 
34 #include <stdlib.h>
35 #include <sys/linker_set.h>
36 #include <pthread.h>
37 
38 #define	USB_MAX_XFER_BLOCKS	8
39 
40 #define	USB_XFER_OUT		0
41 #define	USB_XFER_IN		1
42 
43 
44 struct usb_hci;
45 struct usb_device_request;
46 struct usb_data_xfer;
47 struct vm_snapshot_meta;
48 
49 /* Device emulation handlers */
50 struct usb_devemu {
51 	char	*ue_emu;	/* name of device emulation */
52 	int	ue_usbver;	/* usb version: 2 or 3 */
53 	int	ue_usbspeed;	/* usb device speed */
54 
55 	/* instance creation */
56 	void	*(*ue_init)(struct usb_hci *hci, char *opt);
57 
58 	/* handlers */
59 	int	(*ue_request)(void *sc, struct usb_data_xfer *xfer);
60 	int	(*ue_data)(void *sc, struct usb_data_xfer *xfer, int dir,
61 	                   int epctx);
62 	int	(*ue_reset)(void *sc);
63 	int	(*ue_remove)(void *sc);
64 	int	(*ue_stop)(void *sc);
65 	int	(*ue_snapshot)(void *scarg, struct vm_snapshot_meta *meta);
66 };
67 #define	USB_EMUL_SET(x)		DATA_SET(usb_emu_set, x);
68 
69 /*
70  * USB device events to notify HCI when state changes
71  */
72 enum hci_usbev {
73 	USBDEV_ATTACH,
74 	USBDEV_RESET,
75 	USBDEV_STOP,
76 	USBDEV_REMOVE,
77 };
78 
79 /* usb controller, ie xhci, ehci */
80 struct usb_hci {
81 	int	(*hci_intr)(struct usb_hci *hci, int epctx);
82 	int	(*hci_event)(struct usb_hci *hci, enum hci_usbev evid,
83 		             void *param);
84 	void	*hci_sc;			/* private softc for hci */
85 
86 	/* controller managed fields */
87 	int	hci_address;
88 	int	hci_port;
89 };
90 
91 /*
92  * Each xfer block is mapped to the hci transfer block.
93  * On input into the device handler, blen is set to the lenght of buf.
94  * The device handler is to update blen to reflect on the residual size
95  * of the buffer, i.e. len(buf) - len(consumed).
96  */
97 struct usb_data_xfer_block {
98 	void	*buf;			/* IN or OUT pointer */
99 	int	blen;			/* in:len(buf), out:len(remaining) */
100 	int	bdone;			/* bytes transferred */
101 	uint32_t processed;		/* device processed this + errcode */
102 	void	*hci_data;		/* HCI private reference */
103 	int	ccs;
104 	uint32_t streamid;
105 	uint64_t trbnext;		/* next TRB guest address */
106 };
107 
108 struct usb_data_xfer {
109 	struct usb_data_xfer_block data[USB_MAX_XFER_BLOCKS];
110 	struct usb_device_request *ureq; 	/* setup ctl request */
111 	int	ndata;				/* # of data items */
112 	int	head;
113 	int	tail;
114 	pthread_mutex_t mtx;
115 };
116 
117 enum USB_ERRCODE {
118 	USB_ACK,
119 	USB_NAK,
120 	USB_STALL,
121 	USB_NYET,
122 	USB_ERR,
123 	USB_SHORT
124 };
125 
126 #define	USB_DATA_GET_ERRCODE(x)		(x)->processed >> 8
127 #define	USB_DATA_SET_ERRCODE(x,e)	do {				\
128 			(x)->processed = ((x)->processed & 0xFF) | (e << 8); \
129 		} while (0)
130 
131 #define	USB_DATA_OK(x,i)	((x)->data[(i)].buf != NULL)
132 
133 #define	USB_DATA_XFER_INIT(x)	do {					\
134 			memset((x), 0, sizeof(*(x)));			\
135 			pthread_mutex_init(&((x)->mtx), NULL);		\
136 		} while (0)
137 
138 #define	USB_DATA_XFER_RESET(x)	do {					\
139 			memset((x)->data, 0, sizeof((x)->data));	\
140 			(x)->ndata = 0;					\
141 			(x)->head = (x)->tail = 0;			\
142 		} while (0)
143 
144 #define	USB_DATA_XFER_LOCK(x)	do {					\
145 			pthread_mutex_lock(&((x)->mtx));		\
146 		} while (0)
147 
148 #define	USB_DATA_XFER_UNLOCK(x)	do {					\
149 			pthread_mutex_unlock(&((x)->mtx));		\
150 		} while (0)
151 
152 struct usb_devemu *usb_emu_finddev(char *name);
153 
154 struct usb_data_xfer_block *usb_data_xfer_append(struct usb_data_xfer *xfer,
155                           void *buf, int blen, void *hci_data, int ccs);
156 
157 
158 #endif /* _USB_EMUL_H_ */
159