1 /*-
2 * Copyright (c) 2014 Leon Dang <ldang@nahannisys.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29 #ifndef _USB_CORE_H_
30 #define _USB_CORE_H_
31
32 #include <stdlib.h>
33 #include <pthread.h>
34 #include <stdbool.h>
35 #include "types.h"
36 #include "log.h"
37
38 /* FIXME:
39 * There are some huge data requests which need more than 256 TRBs in a single
40 * transfer, so it is neccessary to expand it.
41 * But this is not final solution, this size should be dynamically changed
42 * according to the native xhci driver's adjust of trb segements.
43 * By default, the native xhci driver use two segments which contain 2 * 256
44 * trbs, so 1024 is enough currently.
45 */
46 #define USB_XFER_OUT 0
47 #define USB_XFER_IN 1
48
49 #define USB_DIR_OUT 0
50 #define USB_DIR_IN 0x80
51
52 #define LIBUSB_TIMEOUT 10000
53
54 #define USB_CFG_ATT_ONE (1 << 7) /* should always be set */
55 #define USB_CFG_ATT_SELFPOWER (1 << 6)
56 #define USB_CFG_ATT_WAKEUP (1 << 5)
57 #define USB_CFG_ATT_BATTERY (1 << 4)
58
59 enum endpoint_type {
60 USB_ENDPOINT_CONTROL = 0,
61 USB_ENDPOINT_ISOC,
62 USB_ENDPOINT_BULK,
63 USB_ENDPOINT_INT,
64 USB_ENDPOINT_INVALID = 255
65 };
66
67 #define USB_INTERFACE_INVALID 255
68
69 enum token_type {
70 TOKEN_OUT = 0,
71 TOKEN_IN,
72 TOKEN_SETUP
73 };
74
75 enum usb_dev_type {
76 USB_DEV_STATIC = 0,
77 USB_DEV_PORT_MAPPER
78 };
79
80 enum usb_block_stat {
81 USB_BLOCK_FREE = 0,
82 USB_BLOCK_HANDLING,
83 USB_BLOCK_HANDLED
84 };
85
86 enum usb_native_devtype {
87 USB_TYPE_ROOTHUB,
88 USB_TYPE_EXTHUB,
89 USB_TYPE_ROOTHUB_SUBDEV,
90 USB_TYPE_EXTHUB_SUBDEV,
91 USB_TYPE_NONE
92 };
93
94 #define USB_MAX_TIERS 7
95
96 struct usb_hci;
97 struct usb_device_request;
98 struct usb_xfer;
99
100 /* Device emulation handlers */
101 struct usb_devemu {
102 char *ue_emu; /* name of device emulation */
103 int ue_usbver; /* usb version: 2 or 3 */
104 int ue_usbspeed; /* usb device speed */
105 int ue_devtype;
106
107 /* instance creation */
108 void *(*ue_init)(void *pdata, char *opt);
109
110 /* handlers */
111 int (*ue_request)(void *sc, struct usb_xfer *xfer);
112 int (*ue_data)(void *sc, struct usb_xfer *xfer, int dir,
113 int epctx);
114 int (*ue_info)(void *sc, int type, void *value, int size);
115 int (*ue_reset)(void *sc);
116 int (*ue_remove)(void *sc);
117 int (*ue_stop)(void *sc);
118 void (*ue_cancel_req)(void *pdata);
119 void (*ue_free_req)(void *pdata);
120 void (*ue_deinit)(void *pdata);
121 };
122 #define USB_EMUL_SET(x) DATA_SET(usb_emu_set, x)
123
124 /*
125 * USB device events to notify HCD when state changes
126 */
127 enum hci_usbev {
128 USBDEV_ATTACH,
129 USBDEV_RESET,
130 USBDEV_STOP,
131 USBDEV_REMOVE,
132 };
133
134 /* usb controller, ie xhci, ehci */
135 struct usb_hci {
136 int (*hci_intr)(struct usb_hci *hci, int epctx);
137 int (*hci_event)(struct usb_hci *hci, enum hci_usbev evid,
138 void *param);
139 void *dev; /* private device for hci */
140
141 /* controller managed fields */
142 int hci_address;
143 int hci_port;
144 };
145
146 enum usb_block_type {
147 USB_DATA_NONE,
148 USB_DATA_PART,
149 USB_DATA_FULL
150 };
151
152 /*
153 * Each xfer block is mapped to the hci transfer block.
154 * On input into the device handler, blen is set to the lenght of buf.
155 * The device handler is to update blen to reflect on the residual size
156 * of the buffer, i.e. len(buf) - len(consumed).
157 */
158 struct usb_block {
159 void *buf; /* IN or OUT pointer */
160 int blen; /* in:len(buf), out:len(remaining) */
161 int bdone; /* bytes transferred */
162 enum usb_block_stat stat; /* processed status */
163 enum usb_block_type type;
164 void *hcb; /* host controller block */
165 };
166
167 struct usb_xfer {
168 struct usb_block *data;
169 struct usb_dev_req **reqs;
170 struct usb_device_request *ureq; /* setup ctl request */
171 int ndata; /* # of data items */
172 int head;
173 int tail;
174 void *dev; /* struct pci_xhci_dev_emu *dev */
175 int epid; /* related endpoint id */
176 int pid; /* token id */
177 int max_blk_cnt;
178 int status;
179 };
180
181 struct usb_devpath {
182 uint8_t bus;
183 uint8_t depth;
184 uint8_t path[USB_MAX_TIERS];
185 };
186 #define ROOTHUB_PORT(x) ((x).path[0])
187
188 struct usb_native_devinfo {
189 int speed;
190 int maxchild;
191 uint16_t bcd;
192 uint16_t pid;
193 uint16_t vid;
194 enum usb_native_devtype type;
195 struct usb_devpath path;
196 void *priv_data;
197 };
198
199 enum USB_ERRCODE {
200 USB_ACK,
201 USB_NAK,
202 USB_STALL,
203 USB_NYET,
204 USB_ERR,
205 USB_SHORT
206 };
207
208 #define USB_DATA_GET_ERRCODE(x) ((x)->stat >> 8)
209 #define USB_DATA_SET_ERRCODE(x, e) \
210 ((x)->stat = ((x)->stat & 0xFF) | (e << 8))
211
212 #define USB_DATA_OK(x, i) ((x)->data[(i)].buf != NULL)
213
214 #define LOG_TAG "USB: "
215 #define LFTL 0
216 #define LWRN 1
217 #define LINF 2
218 #define LDBG 3
219 #define LVRB 4
220 #define UPRINTF(lvl, fmt, args...) \
221 do { if (lvl <= usb_log_level) pr_dbg(LOG_TAG fmt, ##args); } while (0)
222
223 #define NATIVE_USBSYS_DEVDIR "/sys/bus/usb/devices"
224
225 inline bool
index_valid(int head,int tail,int maxcnt,int idx)226 index_valid(int head, int tail, int maxcnt, int idx) {
227 if (head <= tail)
228 return (idx >= head && idx < tail);
229 else
230 return (idx >= head && idx < maxcnt) ||
231 (idx >= 0 && idx < tail);
232 }
233
234 inline int
index_inc(int idx,int maxcnt)235 index_inc(int idx, int maxcnt)
236 {
237 return (idx + 1) % maxcnt;
238 }
239
240 extern int usb_log_level;
usb_get_log_level(void)241 static inline int usb_get_log_level(void) { return usb_log_level; }
usb_set_log_level(int level)242 static inline void usb_set_log_level(int level) { usb_log_level = level; }
243 void usb_parse_log_level(char level);
244 struct usb_devemu *usb_emu_finddev(char *name);
245 int usb_native_is_bus_existed(uint8_t bus_num);
246 int usb_native_is_port_existed(uint8_t bus_num, uint8_t port_num);
247 int usb_native_is_device_existed(struct usb_devpath *path);
248 struct usb_block *usb_block_append(struct usb_xfer *xfer, void *buf, int blen,
249 void *hcb, int hcb_len);
250 int usb_get_hub_port_num(struct usb_devpath *path);
251 char *usb_dev_path(struct usb_devpath *path);
252 bool usb_dev_path_cmp(struct usb_devpath *p1, struct usb_devpath *p2);
253 #endif /* _USB_CORE_H_ */
254