Skip to content

Commit 81bf9d1

Browse files
jfischer-nofabiobaltieri
authored andcommitted
usb: device_next: disable high-speed descriptors when not in use
Disable high-speed descriptors when they are not in use saves 64 bytes in flash/RAM. It is unlikely that it will scale well enough to be used in all class implementations or to support a different speed. Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
1 parent 1c2d692 commit 81bf9d1

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

subsys/usb/device_next/class/usbd_cdc_acm.c

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ struct usbd_cdc_acm_desc {
6565
struct cdc_acm_descriptor if0_acm;
6666
struct cdc_union_descriptor if0_union;
6767
struct usb_ep_descriptor if0_int_ep;
68-
struct usb_ep_descriptor if0_hs_int_ep;
6968

7069
struct usb_if_descriptor if1;
7170
struct usb_ep_descriptor if1_in_ep;
7271
struct usb_ep_descriptor if1_out_ep;
72+
73+
#if USBD_SUPPORTS_HIGH_SPEED
74+
struct usb_ep_descriptor if0_hs_int_ep;
7375
struct usb_ep_descriptor if1_hs_in_ep;
7476
struct usb_ep_descriptor if1_hs_out_ep;
77+
#endif
7578

7679
struct usb_desc_header nil_desc;
7780
};
@@ -197,45 +200,45 @@ static ALWAYS_INLINE int cdc_acm_work_schedule(struct k_work_delayable *work,
197200

198201
static uint8_t cdc_acm_get_int_in(struct usbd_class_data *const c_data)
199202
{
200-
struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
201203
const struct device *dev = usbd_class_get_private(c_data);
202204
const struct cdc_acm_uart_config *cfg = dev->config;
203205
struct usbd_cdc_acm_desc *desc = cfg->desc;
204206

205-
if (USBD_SUPPORTS_HIGH_SPEED &&
206-
usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
207+
#if USBD_SUPPORTS_HIGH_SPEED
208+
if (usbd_bus_speed(usbd_class_get_ctx(c_data)) == USBD_SPEED_HS) {
207209
return desc->if0_hs_int_ep.bEndpointAddress;
208210
}
211+
#endif
209212

210213
return desc->if0_int_ep.bEndpointAddress;
211214
}
212215

213216
static uint8_t cdc_acm_get_bulk_in(struct usbd_class_data *const c_data)
214217
{
215-
struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
216218
const struct device *dev = usbd_class_get_private(c_data);
217219
const struct cdc_acm_uart_config *cfg = dev->config;
218220
struct usbd_cdc_acm_desc *desc = cfg->desc;
219221

220-
if (USBD_SUPPORTS_HIGH_SPEED &&
221-
usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
222+
#if USBD_SUPPORTS_HIGH_SPEED
223+
if (usbd_bus_speed(usbd_class_get_ctx(c_data)) == USBD_SPEED_HS) {
222224
return desc->if1_hs_in_ep.bEndpointAddress;
223225
}
226+
#endif
224227

225228
return desc->if1_in_ep.bEndpointAddress;
226229
}
227230

228231
static uint8_t cdc_acm_get_bulk_out(struct usbd_class_data *const c_data)
229232
{
230-
struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
231233
const struct device *dev = usbd_class_get_private(c_data);
232234
const struct cdc_acm_uart_config *cfg = dev->config;
233235
struct usbd_cdc_acm_desc *desc = cfg->desc;
234236

235-
if (USBD_SUPPORTS_HIGH_SPEED &&
236-
usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
237+
#if USBD_SUPPORTS_HIGH_SPEED
238+
if (usbd_bus_speed(usbd_class_get_ctx(c_data)) == USBD_SPEED_HS) {
237239
return desc->if1_hs_out_ep.bEndpointAddress;
238240
}
241+
#endif
239242

240243
return desc->if1_out_ep.bEndpointAddress;
241244
}
@@ -1148,6 +1151,34 @@ struct usbd_class_api usbd_cdc_acm_api = {
11481151
.get_desc = usbd_cdc_acm_get_desc,
11491152
};
11501153

1154+
#define CDC_ACM_DEFINE_DESCRIPTOR_HS(n) \
1155+
.if0_hs_int_ep = { \
1156+
.bLength = sizeof(struct usb_ep_descriptor), \
1157+
.bDescriptorType = USB_DESC_ENDPOINT, \
1158+
.bEndpointAddress = 0x81, \
1159+
.bmAttributes = USB_EP_TYPE_INTERRUPT, \
1160+
.wMaxPacketSize = sys_cpu_to_le16(CDC_ACM_DEFAULT_INT_EP_MPS), \
1161+
.bInterval = CDC_ACM_HS_INT_EP_INTERVAL, \
1162+
}, \
1163+
\
1164+
.if1_hs_in_ep = { \
1165+
.bLength = sizeof(struct usb_ep_descriptor), \
1166+
.bDescriptorType = USB_DESC_ENDPOINT, \
1167+
.bEndpointAddress = 0x82, \
1168+
.bmAttributes = USB_EP_TYPE_BULK, \
1169+
.wMaxPacketSize = sys_cpu_to_le16(512U), \
1170+
.bInterval = 0, \
1171+
}, \
1172+
\
1173+
.if1_hs_out_ep = { \
1174+
.bLength = sizeof(struct usb_ep_descriptor), \
1175+
.bDescriptorType = USB_DESC_ENDPOINT, \
1176+
.bEndpointAddress = 0x01, \
1177+
.bmAttributes = USB_EP_TYPE_BULK, \
1178+
.wMaxPacketSize = sys_cpu_to_le16(512U), \
1179+
.bInterval = 0, \
1180+
},
1181+
11511182
#define CDC_ACM_DEFINE_DESCRIPTOR(n) \
11521183
static struct usbd_cdc_acm_desc cdc_acm_desc_##n = { \
11531184
.iad = { \
@@ -1213,15 +1244,6 @@ static struct usbd_cdc_acm_desc cdc_acm_desc_##n = { \
12131244
.bInterval = CDC_ACM_FS_INT_EP_INTERVAL, \
12141245
}, \
12151246
\
1216-
.if0_hs_int_ep = { \
1217-
.bLength = sizeof(struct usb_ep_descriptor), \
1218-
.bDescriptorType = USB_DESC_ENDPOINT, \
1219-
.bEndpointAddress = 0x81, \
1220-
.bmAttributes = USB_EP_TYPE_INTERRUPT, \
1221-
.wMaxPacketSize = sys_cpu_to_le16(CDC_ACM_DEFAULT_INT_EP_MPS), \
1222-
.bInterval = CDC_ACM_HS_INT_EP_INTERVAL, \
1223-
}, \
1224-
\
12251247
.if1 = { \
12261248
.bLength = sizeof(struct usb_if_descriptor), \
12271249
.bDescriptorType = USB_DESC_INTERFACE, \
@@ -1252,23 +1274,9 @@ static struct usbd_cdc_acm_desc cdc_acm_desc_##n = { \
12521274
.bInterval = 0, \
12531275
}, \
12541276
\
1255-
.if1_hs_in_ep = { \
1256-
.bLength = sizeof(struct usb_ep_descriptor), \
1257-
.bDescriptorType = USB_DESC_ENDPOINT, \
1258-
.bEndpointAddress = 0x82, \
1259-
.bmAttributes = USB_EP_TYPE_BULK, \
1260-
.wMaxPacketSize = sys_cpu_to_le16(512U), \
1261-
.bInterval = 0, \
1262-
}, \
1263-
\
1264-
.if1_hs_out_ep = { \
1265-
.bLength = sizeof(struct usb_ep_descriptor), \
1266-
.bDescriptorType = USB_DESC_ENDPOINT, \
1267-
.bEndpointAddress = 0x01, \
1268-
.bmAttributes = USB_EP_TYPE_BULK, \
1269-
.wMaxPacketSize = sys_cpu_to_le16(512U), \
1270-
.bInterval = 0, \
1271-
}, \
1277+
COND_CODE_1(USBD_SUPPORTS_HIGH_SPEED, \
1278+
(CDC_ACM_DEFINE_DESCRIPTOR_HS(n)), \
1279+
()) \
12721280
\
12731281
.nil_desc = { \
12741282
.bLength = 0, \
@@ -1288,8 +1296,9 @@ const static struct usb_desc_header *cdc_acm_fs_desc_##n[] = { \
12881296
(struct usb_desc_header *) &cdc_acm_desc_##n.if1_in_ep, \
12891297
(struct usb_desc_header *) &cdc_acm_desc_##n.if1_out_ep, \
12901298
(struct usb_desc_header *) &cdc_acm_desc_##n.nil_desc, \
1291-
}; \
1292-
\
1299+
}
1300+
1301+
#define CDC_ACM_DEFINE_HS_DESC_HEADER(n) \
12931302
const static struct usb_desc_header *cdc_acm_hs_desc_##n[] = { \
12941303
(struct usb_desc_header *) &cdc_acm_desc_##n.iad, \
12951304
(struct usb_desc_header *) &cdc_acm_desc_##n.if0, \
@@ -1302,14 +1311,17 @@ const static struct usb_desc_header *cdc_acm_hs_desc_##n[] = { \
13021311
(struct usb_desc_header *) &cdc_acm_desc_##n.if1_hs_in_ep, \
13031312
(struct usb_desc_header *) &cdc_acm_desc_##n.if1_hs_out_ep, \
13041313
(struct usb_desc_header *) &cdc_acm_desc_##n.nil_desc, \
1305-
}
1314+
};
13061315

13071316
#define USBD_CDC_ACM_DT_DEVICE_DEFINE(n) \
13081317
BUILD_ASSERT(DT_INST_ON_BUS(n, usb), \
13091318
"node " DT_NODE_PATH(DT_DRV_INST(n)) \
13101319
" is not assigned to a USB device controller"); \
13111320
\
13121321
CDC_ACM_DEFINE_DESCRIPTOR(n); \
1322+
COND_CODE_1(USBD_SUPPORTS_HIGH_SPEED, \
1323+
(CDC_ACM_DEFINE_HS_DESC_HEADER(n)), \
1324+
()) \
13131325
\
13141326
USBD_DEFINE_CLASS(cdc_acm_##n, \
13151327
&usbd_cdc_acm_api, \
@@ -1331,7 +1343,8 @@ const static struct usb_desc_header *cdc_acm_hs_desc_##n[] = { \
13311343
)) \
13321344
.desc = &cdc_acm_desc_##n, \
13331345
.fs_desc = cdc_acm_fs_desc_##n, \
1334-
.hs_desc = cdc_acm_hs_desc_##n, \
1346+
.hs_desc = COND_CODE_1(USBD_SUPPORTS_HIGH_SPEED, \
1347+
(cdc_acm_hs_desc_##n,), (NULL,)) \
13351348
}; \
13361349
\
13371350
static struct cdc_acm_uart_data uart_data_##n = { \

0 commit comments

Comments
 (0)