forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracing_backend_usb.c
177 lines (150 loc) · 4.09 KB
/
tracing_backend_usb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2019 Intel corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Disable syscall tracing for all calls from this compilation unit to avoid
* undefined symbols as the macros are not expanded recursively
*/
#define DISABLE_SYSCALL_TRACING
#include <zephyr/sys/util.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/usb/usb_device.h>
#include <tracing_core.h>
#include <tracing_buffer.h>
#include <tracing_backend.h>
#define USB_TRANSFER_ONGOING 1
#define USB_TRANSFER_FREE 0
#define TRACING_IF_IN_EP_ADDR 0x81
#define TRACING_IF_OUT_EP_ADDR 0x01
struct usb_device_desc {
struct usb_if_descriptor if0;
struct usb_ep_descriptor if0_in_ep;
struct usb_ep_descriptor if0_out_ep;
} __packed;
static volatile int transfer_state;
static enum usb_dc_status_code usb_device_status = USB_DC_UNKNOWN;
USBD_CLASS_DESCR_DEFINE(primary, 0) struct usb_device_desc dev_desc = {
/*
* Interface descriptor 0
*/
.if0 = {
.bLength = sizeof(struct usb_if_descriptor),
.bDescriptorType = USB_DESC_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_BCC_VENDOR,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 0,
},
/*
* Data Endpoint IN
*/
.if0_in_ep = {
.bLength = sizeof(struct usb_ep_descriptor),
.bDescriptorType = USB_DESC_ENDPOINT,
.bEndpointAddress = TRACING_IF_IN_EP_ADDR,
.bmAttributes = USB_DC_EP_BULK,
.wMaxPacketSize = sys_cpu_to_le16(CONFIG_TRACING_USB_MPS),
.bInterval = 0x00,
},
/*
* Data Endpoint OUT
*/
.if0_out_ep = {
.bLength = sizeof(struct usb_ep_descriptor),
.bDescriptorType = USB_DESC_ENDPOINT,
.bEndpointAddress = TRACING_IF_OUT_EP_ADDR,
.bmAttributes = USB_DC_EP_BULK,
.wMaxPacketSize = sys_cpu_to_le16(CONFIG_TRACING_USB_MPS),
.bInterval = 0x00,
},
};
static void dev_status_cb(struct usb_cfg_data *cfg,
enum usb_dc_status_code status,
const uint8_t *param)
{
ARG_UNUSED(cfg);
ARG_UNUSED(param);
usb_device_status = status;
}
static void tracing_ep_out_cb(uint8_t ep, enum usb_dc_ep_cb_status_code ep_status)
{
uint8_t *cmd = NULL;
uint32_t bytes_to_read, length;
usb_read(ep, NULL, 0, &bytes_to_read);
while (bytes_to_read) {
length = tracing_cmd_buffer_alloc(&cmd);
if (cmd) {
length = MIN(length, bytes_to_read);
usb_read(ep, cmd, length, NULL);
tracing_cmd_handle(cmd, length);
bytes_to_read -= length;
}
}
/*
* send ZLP to sync with host receive thread
*/
usb_write(TRACING_IF_IN_EP_ADDR, NULL, 0, NULL);
}
static void tracing_ep_in_cb(uint8_t ep, enum usb_dc_ep_cb_status_code ep_status)
{
ARG_UNUSED(ep);
ARG_UNUSED(ep_status);
transfer_state = USB_TRANSFER_FREE;
}
static struct usb_ep_cfg_data ep_cfg[] = {
{
.ep_cb = tracing_ep_out_cb,
.ep_addr = TRACING_IF_OUT_EP_ADDR,
},
{
.ep_cb = tracing_ep_in_cb,
.ep_addr = TRACING_IF_IN_EP_ADDR,
},
};
USBD_DEFINE_CFG_DATA(tracing_backend_usb_config) = {
.usb_device_description = NULL,
.interface_descriptor = &dev_desc.if0,
.cb_usb_status = dev_status_cb,
.interface = {
.class_handler = NULL,
.custom_handler = NULL,
.vendor_handler = NULL,
},
.num_endpoints = ARRAY_SIZE(ep_cfg),
.endpoint = ep_cfg,
};
static void tracing_backend_usb_output(const struct tracing_backend *backend,
uint8_t *data, uint32_t length)
{
int ret = 0;
uint32_t bytes;
while (length > 0) {
transfer_state = USB_TRANSFER_ONGOING;
/*
* make sure every USB transfer no need ZLP at all
* because we are in lowest priority thread content
* there are no deterministic time between real USB
* packet and ZLP
*/
ret = usb_write(TRACING_IF_IN_EP_ADDR, data,
length >= CONFIG_TRACING_USB_MPS ?
CONFIG_TRACING_USB_MPS - 1 : length, &bytes);
if (ret) {
continue;
}
data += bytes;
length -= bytes;
while (transfer_state == USB_TRANSFER_ONGOING) {
}
}
}
const struct tracing_backend_api tracing_backend_usb_api = {
.output = tracing_backend_usb_output
};
TRACING_BACKEND_DEFINE(tracing_backend_usb, tracing_backend_usb_api);