1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 struct op_common {
8 	uint16_t version;
9 	uint16_t code;
10 	uint32_t status;
11 } __packed;
12 
13 struct devlist_device {
14 	char path[256];
15 	char busid[32];
16 
17 	uint32_t busnum;
18 	uint32_t devnum;
19 	uint32_t speed;
20 
21 	uint16_t idVendor;
22 	uint16_t idProduct;
23 	uint16_t bcdDevice;
24 
25 	uint8_t bDeviceClass;
26 	uint8_t bDeviceSubClass;
27 	uint8_t bDeviceProtocol;
28 	uint8_t bConfigurationValue;
29 	uint8_t bNumConfigurations;
30 	uint8_t bNumInterfaces;
31 } __packed;
32 
33 #define OP_REQUEST		(0x80 << 8)
34 #define OP_REPLY		(0x00 << 8)
35 
36 /* Devlist */
37 #define OP_DEVLIST		0x05
38 #define OP_REQ_DEVLIST		(OP_REQUEST | OP_DEVLIST)
39 #define OP_REP_DEVLIST		(OP_REPLY | OP_DEVLIST)
40 
41 /* Import USB device */
42 #define OP_IMPORT		0x03
43 #define OP_REQ_IMPORT		(OP_REQUEST | OP_IMPORT)
44 #define OP_REP_IMPORT		(OP_REPLY | OP_IMPORT)
45 
46 /* USBIP requests */
47 #define USBIP_CMD_SUBMIT	0x0001
48 #define USBIP_CMD_UNLINK	0x0002
49 #define USBIP_RET_SUBMIT	0x0003
50 #define USBIP_RET_UNLINK	0x0004
51 
52 /* USBIP direction */
53 #define USBIP_DIR_OUT		0x00
54 #define USBIP_DIR_IN		0x01
55 
56 struct usbip_header_common {
57 	uint32_t command;
58 	uint32_t seqnum;
59 	uint32_t devid;
60 	uint32_t direction;
61 	uint32_t ep;
62 } __packed;
63 
64 struct usbip_submit {
65 	uint32_t transfer_flags;
66 	int32_t transfer_buffer_length;
67 	int32_t start_frame;
68 	int32_t number_of_packets;
69 	int32_t interval;
70 	uint8_t bmRequestType;
71 	uint8_t bRequest;
72 	uint16_t wValue;
73 	uint16_t wIndex;
74 	uint16_t wLength;
75 } __packed;
76 
77 struct usbip_unlink {
78 	uint32_t seqnum;
79 } __packed;
80 
81 struct usbip_submit_rsp {
82 	struct usbip_header_common common;
83 
84 	int32_t status;
85 	int32_t actual_length;
86 	int32_t start_frame;
87 	int32_t number_of_packets;
88 	int32_t error_count;
89 
90 	uint64_t setup;
91 } __packed;
92 
93 struct usbip_header {
94 	struct usbip_header_common common;
95 
96 	union {
97 		struct usbip_submit submit;
98 		struct usbip_unlink unlink;
99 	} u;
100 } __packed;
101 
102 /* Function definitions */
103 
104 int usbip_recv(uint8_t *buf, size_t len);
105 bool usbip_send_common(uint8_t ep, uint32_t data_len);
106 int usbip_send(uint8_t ep, const uint8_t *data, size_t len);
107 
108 void usbip_start(void);
109 
110 int handle_usb_control(struct usbip_header *hdr);
111 int handle_usb_data(struct usbip_header *hdr);
112