/
loopback.c
182 lines (157 loc) · 4.49 KB
/
loopback.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
178
179
180
181
182
/*
* USB loopback function
*
* Copyright (c) 2018 Phytec Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <init.h>
#include <misc/byteorder.h>
#include <usb/usb_device.h>
#include <usb/usb_common.h>
#include <usb_descriptor.h>
#define LOG_LEVEL CONFIG_USB_DEVICE_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(usb_loopback);
#define LOOPBACK_OUT_EP_ADDR 0x01
#define LOOPBACK_IN_EP_ADDR 0x81
#define LOOPBACK_OUT_EP_IDX 0
#define LOOPBACK_IN_EP_IDX 1
static u8_t loopback_buf[1024];
/* usb.rst config structure start */
struct usb_loopback_config {
struct usb_if_descriptor if0;
struct usb_ep_descriptor if0_out_ep;
struct usb_ep_descriptor if0_in_ep;
} __packed;
USBD_CLASS_DESCR_DEFINE(primary, 0) struct usb_loopback_config loopback_cfg = {
/* Interface descriptor 0 */
.if0 = {
.bLength = sizeof(struct usb_if_descriptor),
.bDescriptorType = USB_INTERFACE_DESC,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = CUSTOM_CLASS,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 0,
},
/* Data Endpoint OUT */
.if0_out_ep = {
.bLength = sizeof(struct usb_ep_descriptor),
.bDescriptorType = USB_ENDPOINT_DESC,
.bEndpointAddress = LOOPBACK_OUT_EP_ADDR,
.bmAttributes = USB_DC_EP_BULK,
.wMaxPacketSize = sys_cpu_to_le16(CONFIG_LOOPBACK_BULK_EP_MPS),
.bInterval = 0x00,
},
/* Data Endpoint IN */
.if0_in_ep = {
.bLength = sizeof(struct usb_ep_descriptor),
.bDescriptorType = USB_ENDPOINT_DESC,
.bEndpointAddress = LOOPBACK_IN_EP_ADDR,
.bmAttributes = USB_DC_EP_BULK,
.wMaxPacketSize = sys_cpu_to_le16(CONFIG_LOOPBACK_BULK_EP_MPS),
.bInterval = 0x00,
},
};
/* usb.rst config structure end */
static void loopback_out_cb(u8_t ep, enum usb_dc_ep_cb_status_code ep_status)
{
u32_t bytes_to_read;
usb_read(ep, NULL, 0, &bytes_to_read);
LOG_DBG("ep 0x%x, bytes to read %d ", ep, bytes_to_read);
usb_read(ep, loopback_buf, bytes_to_read, NULL);
}
static void loopback_in_cb(u8_t ep,
enum usb_dc_ep_cb_status_code ep_status)
{
if (usb_write(ep, loopback_buf, CONFIG_LOOPBACK_BULK_EP_MPS, NULL)) {
LOG_DBG("ep 0x%x", ep);
}
}
/* usb.rst endpoint configuration start */
static struct usb_ep_cfg_data ep_cfg[] = {
{
.ep_cb = loopback_out_cb,
.ep_addr = LOOPBACK_OUT_EP_ADDR,
},
{
.ep_cb = loopback_in_cb,
.ep_addr = LOOPBACK_IN_EP_ADDR,
},
};
/* usb.rst endpoint configuration end */
static void loopback_status_cb(struct usb_cfg_data *cfg,
enum usb_dc_status_code status,
const u8_t *param)
{
ARG_UNUSED(cfg);
switch (status) {
case USB_DC_CONFIGURED:
loopback_in_cb(ep_cfg[LOOPBACK_IN_EP_IDX].ep_addr, 0);
LOG_DBG("USB device configured");
break;
case USB_DC_SET_HALT:
LOG_DBG("Set Feature ENDPOINT_HALT");
break;
case USB_DC_CLEAR_HALT:
LOG_DBG("Clear Feature ENDPOINT_HALT");
if (*param == ep_cfg[LOOPBACK_IN_EP_IDX].ep_addr) {
loopback_in_cb(ep_cfg[LOOPBACK_IN_EP_IDX].ep_addr, 0);
}
break;
default:
break;
}
}
/* usb.rst vendor handler start */
static int loopback_vendor_handler(struct usb_setup_packet *setup,
s32_t *len, u8_t **data)
{
LOG_DBG("Class request: bRequest 0x%x bmRequestType 0x%x len %d",
setup->bRequest, setup->bmRequestType, *len);
if (REQTYPE_GET_RECIP(setup->bmRequestType) != REQTYPE_RECIP_DEVICE) {
return -ENOTSUP;
}
if (REQTYPE_GET_DIR(setup->bmRequestType) == REQTYPE_DIR_TO_DEVICE &&
setup->bRequest == 0x5b) {
LOG_DBG("Host-to-Device, data %p", *data);
return 0;
}
if ((REQTYPE_GET_DIR(setup->bmRequestType) == REQTYPE_DIR_TO_HOST) &&
(setup->bRequest == 0x5c)) {
if (setup->wLength > sizeof(loopback_buf)) {
return -ENOTSUP;
}
*data = loopback_buf;
*len = setup->wLength;
LOG_DBG("Device-to-Host, wLength %d, data %p",
setup->wLength, *data);
return 0;
}
return -ENOTSUP;
}
/* usb.rst vendor handler end */
static void loopback_interface_config(struct usb_desc_header *head,
u8_t bInterfaceNumber)
{
ARG_UNUSED(head);
loopback_cfg.if0.bInterfaceNumber = bInterfaceNumber;
}
/* usb.rst device config data start */
USBD_CFG_DATA_DEFINE(primary, loopback) struct usb_cfg_data loopback_config = {
.usb_device_description = NULL,
.interface_config = loopback_interface_config,
.interface_descriptor = &loopback_cfg.if0,
.cb_usb_status = loopback_status_cb,
.interface = {
.class_handler = NULL,
.custom_handler = NULL,
.vendor_handler = loopback_vendor_handler,
},
.num_endpoints = ARRAY_SIZE(ep_cfg),
.endpoint = ep_cfg,
};
/* usb.rst device config data end */