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