1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <errno.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/usb/usbd.h>
10 #include <zephyr/drivers/usb/udc.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/slist.h>
13 #include <zephyr/drivers/hwinfo.h>
14
15 #include "usbd_device.h"
16 #include "usbd_desc.h"
17 #include "usbd_ch9.h"
18 #include "usbd_config.h"
19 #include "usbd_class.h"
20 #include "usbd_class_api.h"
21 #include "usbd_interface.h"
22 #include "usbd_msg.h"
23
24 #include <zephyr/logging/log.h>
25 LOG_MODULE_REGISTER(usbd_ch9, CONFIG_USBD_LOG_LEVEL);
26
27 #define CTRL_AWAIT_SETUP_DATA 0
28 #define CTRL_AWAIT_STATUS_STAGE 1
29
30 #define SF_TEST_MODE_SELECTOR(wIndex) ((uint8_t)((wIndex) >> 8))
31 #define SF_TEST_LOWER_BYTE(wIndex) ((uint8_t)(wIndex))
32
33 static int nonstd_request(struct usbd_context *const uds_ctx,
34 struct net_buf *const dbuf);
35
reqtype_is_to_host(const struct usb_setup_packet * const setup)36 static bool reqtype_is_to_host(const struct usb_setup_packet *const setup)
37 {
38 return setup->wLength && USB_REQTYPE_GET_DIR(setup->bmRequestType);
39 }
40
reqtype_is_to_device(const struct usb_setup_packet * const setup)41 static bool reqtype_is_to_device(const struct usb_setup_packet *const setup)
42 {
43 return !reqtype_is_to_host(setup);
44 }
45
ch9_set_ctrl_type(struct usbd_context * const uds_ctx,const int type)46 static void ch9_set_ctrl_type(struct usbd_context *const uds_ctx,
47 const int type)
48 {
49 uds_ctx->ch9_data.ctrl_type = type;
50 }
51
ch9_get_ctrl_type(struct usbd_context * const uds_ctx)52 static int ch9_get_ctrl_type(struct usbd_context *const uds_ctx)
53 {
54 return uds_ctx->ch9_data.ctrl_type;
55 }
56
post_status_stage(struct usbd_context * const uds_ctx)57 static int post_status_stage(struct usbd_context *const uds_ctx)
58 {
59 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
60 int ret = 0;
61
62 if (setup->bRequest == USB_SREQ_SET_ADDRESS) {
63 ret = udc_set_address(uds_ctx->dev, setup->wValue);
64 if (ret) {
65 LOG_ERR("Failed to set device address 0x%x", setup->wValue);
66 }
67 }
68
69 if (setup->bRequest == USB_SREQ_SET_FEATURE &&
70 setup->wValue == USB_SFS_TEST_MODE) {
71 uint8_t mode = SF_TEST_MODE_SELECTOR(setup->wIndex);
72
73 ret = udc_test_mode(uds_ctx->dev, mode, false);
74 if (ret) {
75 LOG_ERR("Failed to enable TEST_MODE %u", mode);
76 }
77 }
78
79 uds_ctx->ch9_data.post_status = false;
80
81 return ret;
82 }
83
sreq_set_address(struct usbd_context * const uds_ctx)84 static int sreq_set_address(struct usbd_context *const uds_ctx)
85 {
86 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
87 struct udc_device_caps caps = udc_caps(uds_ctx->dev);
88
89 /* Not specified if wIndex or wLength is non-zero, treat as error */
90 if (setup->wValue > 127 || setup->wIndex || setup->wLength) {
91 errno = -ENOTSUP;
92 return 0;
93 }
94
95 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE) {
96 errno = -ENOTSUP;
97 return 0;
98 }
99
100 if (usbd_state_is_configured(uds_ctx)) {
101 errno = -EPERM;
102 return 0;
103 }
104
105 if (caps.addr_before_status) {
106 int ret;
107
108 ret = udc_set_address(uds_ctx->dev, setup->wValue);
109 if (ret) {
110 LOG_ERR("Failed to set device address 0x%x", setup->wValue);
111 return ret;
112 }
113 } else {
114 uds_ctx->ch9_data.post_status = true;
115 }
116
117 if (usbd_state_is_address(uds_ctx) && setup->wValue == 0) {
118 uds_ctx->ch9_data.state = USBD_STATE_DEFAULT;
119 } else {
120 uds_ctx->ch9_data.state = USBD_STATE_ADDRESS;
121 }
122
123
124 return 0;
125 }
126
sreq_set_configuration(struct usbd_context * const uds_ctx)127 static int sreq_set_configuration(struct usbd_context *const uds_ctx)
128 {
129 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
130 const enum usbd_speed speed = usbd_bus_speed(uds_ctx);
131 int ret;
132
133 LOG_INF("Set Configuration Request value %u", setup->wValue);
134
135 /* Not specified if wLength is non-zero, treat as error */
136 if (setup->wValue > UINT8_MAX || setup->wLength) {
137 errno = -ENOTSUP;
138 return 0;
139 }
140
141 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE) {
142 errno = -ENOTSUP;
143 return 0;
144 }
145
146 if (usbd_state_is_default(uds_ctx)) {
147 errno = -EPERM;
148 return 0;
149 }
150
151 if (setup->wValue && !usbd_config_exist(uds_ctx, speed, setup->wValue)) {
152 errno = -EPERM;
153 return 0;
154 }
155
156 if (setup->wValue == usbd_get_config_value(uds_ctx)) {
157 LOG_DBG("Already in the configuration %u", setup->wValue);
158 return 0;
159 }
160
161 ret = usbd_config_set(uds_ctx, setup->wValue);
162 if (ret) {
163 LOG_ERR("Failed to set configuration %u, %d",
164 setup->wValue, ret);
165 return ret;
166 }
167
168 if (setup->wValue == 0) {
169 /* Enter address state */
170 uds_ctx->ch9_data.state = USBD_STATE_ADDRESS;
171 } else {
172 uds_ctx->ch9_data.state = USBD_STATE_CONFIGURED;
173 }
174
175 if (ret == 0) {
176 usbd_msg_pub_simple(uds_ctx, USBD_MSG_CONFIGURATION, setup->wValue);
177 }
178
179 return ret;
180 }
181
sreq_set_interface(struct usbd_context * const uds_ctx)182 static int sreq_set_interface(struct usbd_context *const uds_ctx)
183 {
184 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
185 int ret;
186
187 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_INTERFACE) {
188 errno = -ENOTSUP;
189 return 0;
190 }
191
192 /* Not specified if wLength is non-zero, treat as error */
193 if (setup->wLength) {
194 errno = -ENOTSUP;
195 return 0;
196 }
197
198 if (setup->wValue > UINT8_MAX || setup->wIndex > UINT8_MAX) {
199 errno = -ENOTSUP;
200 return 0;
201 }
202
203 if (!usbd_state_is_configured(uds_ctx)) {
204 errno = -EPERM;
205 return 0;
206 }
207
208 ret = usbd_interface_set(uds_ctx, setup->wIndex, setup->wValue);
209 if (ret == -ENOENT) {
210 LOG_INF("Interface or alternate does not exist");
211 errno = ret;
212 ret = 0;
213 }
214
215 return ret;
216 }
217
sreq_feature_halt_notify(struct usbd_context * const uds_ctx,const uint8_t ep,const bool halted)218 static void sreq_feature_halt_notify(struct usbd_context *const uds_ctx,
219 const uint8_t ep, const bool halted)
220 {
221 struct usbd_class_node *c_nd = usbd_class_get_by_ep(uds_ctx, ep);
222
223 if (c_nd != NULL) {
224 usbd_class_feature_halt(c_nd->c_data, ep, halted);
225 }
226 }
227
sreq_clear_feature(struct usbd_context * const uds_ctx)228 static int sreq_clear_feature(struct usbd_context *const uds_ctx)
229 {
230 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
231 uint8_t ep = setup->wIndex;
232 int ret = 0;
233
234 /* Not specified if wLength is non-zero, treat as error */
235 if (setup->wLength) {
236 errno = -ENOTSUP;
237 return 0;
238 }
239
240 /* Not specified in default state, treat as error */
241 if (usbd_state_is_default(uds_ctx)) {
242 errno = -EPERM;
243 return 0;
244 }
245
246 if (usbd_state_is_address(uds_ctx) && setup->wIndex) {
247 errno = -EPERM;
248 return 0;
249 }
250
251 switch (setup->RequestType.recipient) {
252 case USB_REQTYPE_RECIPIENT_DEVICE:
253 if (setup->wIndex != 0) {
254 errno = -EPERM;
255 return 0;
256 }
257
258 if (setup->wValue == USB_SFS_REMOTE_WAKEUP) {
259 LOG_DBG("Clear feature remote wakeup");
260 uds_ctx->status.rwup = false;
261 }
262 break;
263 case USB_REQTYPE_RECIPIENT_ENDPOINT:
264 if (setup->wValue == USB_SFS_ENDPOINT_HALT) {
265 /* UDC checks if endpoint is enabled */
266 errno = usbd_ep_clear_halt(uds_ctx, ep);
267 ret = (errno == -EPERM) ? errno : 0;
268 if (ret == 0) {
269 /* Notify class instance */
270 sreq_feature_halt_notify(uds_ctx, ep, false);
271 }
272 break;
273 }
274 break;
275 case USB_REQTYPE_RECIPIENT_INTERFACE:
276 default:
277 break;
278 }
279
280 return ret;
281 }
282
set_feature_test_mode(struct usbd_context * const uds_ctx)283 static int set_feature_test_mode(struct usbd_context *const uds_ctx)
284 {
285 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
286 uint8_t mode = SF_TEST_MODE_SELECTOR(setup->wIndex);
287
288 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE ||
289 SF_TEST_LOWER_BYTE(setup->wIndex) != 0) {
290 errno = -ENOTSUP;
291 return 0;
292 }
293
294 if (udc_test_mode(uds_ctx->dev, mode, true) != 0) {
295 errno = -ENOTSUP;
296 return 0;
297 }
298
299 uds_ctx->ch9_data.post_status = true;
300
301 return 0;
302 }
303
sreq_set_feature(struct usbd_context * const uds_ctx)304 static int sreq_set_feature(struct usbd_context *const uds_ctx)
305 {
306 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
307 uint8_t ep = setup->wIndex;
308 int ret = 0;
309
310 /* Not specified if wLength is non-zero, treat as error */
311 if (setup->wLength) {
312 errno = -ENOTSUP;
313 return 0;
314 }
315
316 if (unlikely(setup->wValue == USB_SFS_TEST_MODE)) {
317 return set_feature_test_mode(uds_ctx);
318 }
319
320 /*
321 * Other request behavior is not specified in the default state, treat
322 * as an error.
323 */
324 if (usbd_state_is_default(uds_ctx)) {
325 errno = -EPERM;
326 return 0;
327 }
328
329 if (usbd_state_is_address(uds_ctx) && setup->wIndex) {
330 errno = -EPERM;
331 return 0;
332 }
333
334 switch (setup->RequestType.recipient) {
335 case USB_REQTYPE_RECIPIENT_DEVICE:
336 if (setup->wIndex != 0) {
337 errno = -EPERM;
338 return 0;
339 }
340
341 if (setup->wValue == USB_SFS_REMOTE_WAKEUP) {
342 LOG_DBG("Set feature remote wakeup");
343 uds_ctx->status.rwup = true;
344 }
345 break;
346 case USB_REQTYPE_RECIPIENT_ENDPOINT:
347 if (setup->wValue == USB_SFS_ENDPOINT_HALT) {
348 /* UDC checks if endpoint is enabled */
349 errno = usbd_ep_set_halt(uds_ctx, ep);
350 ret = (errno == -EPERM) ? errno : 0;
351 if (ret == 0) {
352 /* Notify class instance */
353 sreq_feature_halt_notify(uds_ctx, ep, true);
354 }
355 break;
356 }
357 break;
358 case USB_REQTYPE_RECIPIENT_INTERFACE:
359 default:
360 break;
361 }
362
363 return ret;
364 }
365
std_request_to_device(struct usbd_context * const uds_ctx,struct net_buf * const buf)366 static int std_request_to_device(struct usbd_context *const uds_ctx,
367 struct net_buf *const buf)
368 {
369 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
370 int ret;
371
372 switch (setup->bRequest) {
373 case USB_SREQ_SET_ADDRESS:
374 ret = sreq_set_address(uds_ctx);
375 break;
376 case USB_SREQ_SET_CONFIGURATION:
377 ret = sreq_set_configuration(uds_ctx);
378 break;
379 case USB_SREQ_SET_INTERFACE:
380 ret = sreq_set_interface(uds_ctx);
381 break;
382 case USB_SREQ_CLEAR_FEATURE:
383 ret = sreq_clear_feature(uds_ctx);
384 break;
385 case USB_SREQ_SET_FEATURE:
386 ret = sreq_set_feature(uds_ctx);
387 break;
388 default:
389 errno = -ENOTSUP;
390 ret = 0;
391 break;
392 }
393
394 return ret;
395 }
396
sreq_get_status(struct usbd_context * const uds_ctx,struct net_buf * const buf)397 static int sreq_get_status(struct usbd_context *const uds_ctx,
398 struct net_buf *const buf)
399 {
400 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
401 uint8_t ep = setup->wIndex;
402 uint16_t response = 0;
403
404 if (setup->wLength != sizeof(response)) {
405 errno = -ENOTSUP;
406 return 0;
407 }
408
409 /* Not specified in default state, treat as error */
410 if (usbd_state_is_default(uds_ctx)) {
411 errno = -EPERM;
412 return 0;
413 }
414
415 if (usbd_state_is_address(uds_ctx) && setup->wIndex) {
416 errno = -EPERM;
417 return 0;
418 }
419
420 switch (setup->RequestType.recipient) {
421 case USB_REQTYPE_RECIPIENT_DEVICE:
422 if (setup->wIndex != 0) {
423 errno = -EPERM;
424 return 0;
425 }
426
427 response = uds_ctx->status.rwup ?
428 USB_GET_STATUS_REMOTE_WAKEUP : 0;
429 response |= uds_ctx->status.self_powered ?
430 USB_GET_STATUS_SELF_POWERED : 0;
431 break;
432 case USB_REQTYPE_RECIPIENT_ENDPOINT:
433 response = usbd_ep_is_halted(uds_ctx, ep) ? BIT(0) : 0;
434 break;
435 case USB_REQTYPE_RECIPIENT_INTERFACE:
436 /* Response is always reset to zero.
437 * TODO: add check if interface exist?
438 */
439 break;
440 default:
441 break;
442 }
443
444 if (net_buf_tailroom(buf) < setup->wLength) {
445 errno = -ENOMEM;
446 return 0;
447 }
448
449 LOG_DBG("Get Status response 0x%04x", response);
450 net_buf_add_le16(buf, response);
451
452 return 0;
453 }
454
455 /*
456 * This function handles configuration and USB2.0 other-speed-configuration
457 * descriptor type requests.
458 */
sreq_get_desc_cfg(struct usbd_context * const uds_ctx,struct net_buf * const buf,const uint8_t idx,const bool other_cfg)459 static int sreq_get_desc_cfg(struct usbd_context *const uds_ctx,
460 struct net_buf *const buf,
461 const uint8_t idx,
462 const bool other_cfg)
463 {
464 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
465 enum usbd_speed speed = usbd_bus_speed(uds_ctx);
466 struct usb_cfg_descriptor other_desc;
467 struct usb_cfg_descriptor *cfg_desc;
468 struct usbd_config_node *cfg_nd;
469 enum usbd_speed get_desc_speed;
470 struct usbd_class_node *c_nd;
471 uint16_t len;
472
473 /*
474 * If the other-speed-configuration-descriptor is requested and the
475 * controller does not support high speed, respond with an error.
476 */
477 if (other_cfg && usbd_caps_speed(uds_ctx) != USBD_SPEED_HS) {
478 errno = -ENOTSUP;
479 return 0;
480 }
481
482 if (other_cfg) {
483 if (speed == USBD_SPEED_FS) {
484 get_desc_speed = USBD_SPEED_HS;
485 } else {
486 get_desc_speed = USBD_SPEED_FS;
487 }
488 } else {
489 get_desc_speed = speed;
490 }
491
492 cfg_nd = usbd_config_get(uds_ctx, get_desc_speed, idx + 1);
493 if (cfg_nd == NULL) {
494 LOG_ERR("Configuration descriptor %u not found", idx + 1);
495 errno = -ENOTSUP;
496 return 0;
497 }
498
499 if (other_cfg) {
500 /* Copy the configuration descriptor and update the type */
501 memcpy(&other_desc, cfg_nd->desc, sizeof(other_desc));
502 other_desc.bDescriptorType = USB_DESC_OTHER_SPEED;
503 cfg_desc = &other_desc;
504 } else {
505 cfg_desc = cfg_nd->desc;
506 }
507
508 net_buf_add_mem(buf, cfg_desc, MIN(net_buf_tailroom(buf), cfg_desc->bLength));
509
510 SYS_SLIST_FOR_EACH_CONTAINER(&cfg_nd->class_list, c_nd, node) {
511 struct usb_desc_header **dhp;
512
513 dhp = usbd_class_get_desc(c_nd->c_data, get_desc_speed);
514 if (dhp == NULL) {
515 continue;
516 }
517
518 while (*dhp != NULL && (*dhp)->bLength != 0) {
519 len = MIN(net_buf_tailroom(buf), (*dhp)->bLength);
520 net_buf_add_mem(buf, *dhp, len);
521 dhp++;
522 }
523 }
524
525 if (buf->len > setup->wLength) {
526 net_buf_remove_mem(buf, buf->len - setup->wLength);
527 }
528
529 LOG_DBG("Get Configuration descriptor %u, len %u", idx, buf->len);
530
531 return 0;
532 }
533
534 #define USBD_HWID_SN_MAX 32U
535
536 /* Generate valid USB device serial number from hwid */
get_sn_from_hwid(uint8_t sn[static USBD_HWID_SN_MAX])537 static ssize_t get_sn_from_hwid(uint8_t sn[static USBD_HWID_SN_MAX])
538 {
539 static const char hex[] = "0123456789ABCDEF";
540 uint8_t hwid[USBD_HWID_SN_MAX / 2U];
541 ssize_t hwid_len = -ENOSYS;
542
543 if (IS_ENABLED(CONFIG_HWINFO)) {
544 hwid_len = hwinfo_get_device_id(hwid, sizeof(hwid));
545 }
546
547 if (hwid_len < 0) {
548 if (hwid_len == -ENOSYS) {
549 LOG_ERR("HWINFO not implemented or enabled");
550 }
551
552 return hwid_len;
553 }
554
555 for (ssize_t i = 0; i < hwid_len; i++) {
556 sn[i * 2] = hex[hwid[i] >> 4];
557 sn[i * 2 + 1] = hex[hwid[i] & 0xF];
558 }
559
560 return hwid_len * 2;
561 }
562
563 /* Copy and convert ASCII-7 string descriptor to UTF16-LE */
string_ascii7_to_utf16le(struct usbd_desc_node * const dn,struct net_buf * const buf,const uint16_t wLength)564 static void string_ascii7_to_utf16le(struct usbd_desc_node *const dn,
565 struct net_buf *const buf, const uint16_t wLength)
566 {
567 uint8_t hwid_sn[USBD_HWID_SN_MAX];
568 struct usb_desc_header head = {
569 .bDescriptorType = dn->bDescriptorType,
570 };
571 uint8_t *ascii7_str;
572 size_t len;
573 size_t i;
574
575 if (dn->str.utype == USBD_DUT_STRING_SERIAL_NUMBER && dn->str.use_hwinfo) {
576 ssize_t hwid_len = get_sn_from_hwid(hwid_sn);
577
578 if (hwid_len < 0) {
579 errno = -ENOTSUP;
580 return;
581 }
582
583 head.bLength = sizeof(head) + hwid_len * 2;
584 ascii7_str = hwid_sn;
585 } else {
586 head.bLength = dn->bLength;
587 ascii7_str = (uint8_t *)dn->ptr;
588 }
589
590 LOG_DBG("wLength %u, bLength %u, tailroom %u",
591 wLength, head.bLength, net_buf_tailroom(buf));
592
593 len = MIN(net_buf_tailroom(buf), MIN(head.bLength, wLength));
594
595 /* Add bLength and bDescriptorType */
596 net_buf_add_mem(buf, &head, MIN(len, sizeof(head)));
597 len -= MIN(len, sizeof(head));
598
599 for (i = 0; i < len / 2; i++) {
600 __ASSERT(ascii7_str[i] > 0x1F && ascii7_str[i] < 0x7F,
601 "Only printable ascii-7 characters are allowed in USB "
602 "string descriptors");
603 net_buf_add_le16(buf, ascii7_str[i]);
604 }
605
606 if (len & 1) {
607 net_buf_add_u8(buf, ascii7_str[i]);
608 }
609 }
610
sreq_get_desc_dev(struct usbd_context * const uds_ctx,struct net_buf * const buf)611 static int sreq_get_desc_dev(struct usbd_context *const uds_ctx,
612 struct net_buf *const buf)
613 {
614 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
615 struct usb_desc_header *head;
616 size_t len;
617
618 len = MIN(setup->wLength, net_buf_tailroom(buf));
619
620 switch (usbd_bus_speed(uds_ctx)) {
621 case USBD_SPEED_FS:
622 head = uds_ctx->fs_desc;
623 break;
624 case USBD_SPEED_HS:
625 head = uds_ctx->hs_desc;
626 break;
627 default:
628 errno = -ENOTSUP;
629 return 0;
630 }
631
632 net_buf_add_mem(buf, head, MIN(len, head->bLength));
633
634 return 0;
635 }
636
sreq_get_desc_str(struct usbd_context * const uds_ctx,struct net_buf * const buf,const uint8_t idx)637 static int sreq_get_desc_str(struct usbd_context *const uds_ctx,
638 struct net_buf *const buf, const uint8_t idx)
639 {
640 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
641 struct usbd_desc_node *d_nd;
642 size_t len;
643
644 /* Get string descriptor */
645 d_nd = usbd_get_descriptor(uds_ctx, USB_DESC_STRING, idx);
646 if (d_nd == NULL) {
647 errno = -ENOTSUP;
648 return 0;
649 }
650
651 if (usbd_str_desc_get_idx(d_nd) == 0U) {
652 /* Language ID string descriptor */
653 struct usb_string_descriptor langid = {
654 .bLength = d_nd->bLength,
655 .bDescriptorType = d_nd->bDescriptorType,
656 .bString = *(uint16_t *)d_nd->ptr,
657 };
658
659 len = MIN(setup->wLength, net_buf_tailroom(buf));
660 net_buf_add_mem(buf, &langid, MIN(len, langid.bLength));
661 } else {
662 /* String descriptors in ASCII7 format */
663 string_ascii7_to_utf16le(d_nd, buf, setup->wLength);
664 }
665
666 return 0;
667 }
668
sreq_get_dev_qualifier(struct usbd_context * const uds_ctx,struct net_buf * const buf)669 static int sreq_get_dev_qualifier(struct usbd_context *const uds_ctx,
670 struct net_buf *const buf)
671 {
672 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
673 /* At Full-Speed we want High-Speed descriptor and vice versa */
674 struct usb_device_descriptor *d_desc =
675 usbd_bus_speed(uds_ctx) == USBD_SPEED_FS ?
676 uds_ctx->hs_desc : uds_ctx->fs_desc;
677 struct usb_device_qualifier_descriptor q_desc = {
678 .bLength = sizeof(struct usb_device_qualifier_descriptor),
679 .bDescriptorType = USB_DESC_DEVICE_QUALIFIER,
680 .bcdUSB = d_desc->bcdUSB,
681 .bDeviceClass = d_desc->bDeviceClass,
682 .bDeviceSubClass = d_desc->bDeviceSubClass,
683 .bDeviceProtocol = d_desc->bDeviceProtocol,
684 .bMaxPacketSize0 = d_desc->bMaxPacketSize0,
685 .bNumConfigurations = d_desc->bNumConfigurations,
686 .bReserved = 0U,
687 };
688 size_t len;
689
690 /*
691 * If the Device Qualifier descriptor is requested and the controller
692 * does not support high speed, respond with an error.
693 */
694 if (usbd_caps_speed(uds_ctx) != USBD_SPEED_HS) {
695 errno = -ENOTSUP;
696 return 0;
697 }
698
699 LOG_DBG("Get Device Qualifier");
700 len = MIN(setup->wLength, net_buf_tailroom(buf));
701 net_buf_add_mem(buf, &q_desc, MIN(len, q_desc.bLength));
702
703 return 0;
704 }
705
desc_fill_bos_root(struct usbd_context * const uds_ctx,struct usb_bos_descriptor * const root)706 static void desc_fill_bos_root(struct usbd_context *const uds_ctx,
707 struct usb_bos_descriptor *const root)
708 {
709 struct usbd_desc_node *desc_nd;
710
711 root->bLength = sizeof(struct usb_bos_descriptor);
712 root->bDescriptorType = USB_DESC_BOS;
713 root->wTotalLength = root->bLength;
714 root->bNumDeviceCaps = 0;
715
716 SYS_DLIST_FOR_EACH_CONTAINER(&uds_ctx->descriptors, desc_nd, node) {
717 if (desc_nd->bDescriptorType == USB_DESC_BOS) {
718 root->wTotalLength += desc_nd->bLength;
719 root->bNumDeviceCaps++;
720 }
721 }
722 }
723
sreq_get_desc_bos(struct usbd_context * const uds_ctx,struct net_buf * const buf)724 static int sreq_get_desc_bos(struct usbd_context *const uds_ctx,
725 struct net_buf *const buf)
726 {
727 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
728 struct usb_device_descriptor *dev_dsc;
729 struct usb_bos_descriptor bos;
730 struct usbd_desc_node *desc_nd;
731 size_t len;
732
733 switch (usbd_bus_speed(uds_ctx)) {
734 case USBD_SPEED_FS:
735 dev_dsc = uds_ctx->fs_desc;
736 break;
737 case USBD_SPEED_HS:
738 dev_dsc = uds_ctx->hs_desc;
739 break;
740 default:
741 errno = -ENOTSUP;
742 return 0;
743 }
744
745 if (sys_le16_to_cpu(dev_dsc->bcdUSB) < 0x0201U) {
746 errno = -ENOTSUP;
747 return 0;
748 }
749
750 desc_fill_bos_root(uds_ctx, &bos);
751 len = MIN(net_buf_tailroom(buf), MIN(setup->wLength, bos.wTotalLength));
752
753 LOG_DBG("wLength %u, bLength %u, wTotalLength %u, tailroom %u",
754 setup->wLength, bos.bLength, bos.wTotalLength, net_buf_tailroom(buf));
755
756 net_buf_add_mem(buf, &bos, MIN(len, bos.bLength));
757
758 len -= MIN(len, sizeof(bos));
759 if (len == 0) {
760 return 0;
761 }
762
763 SYS_DLIST_FOR_EACH_CONTAINER(&uds_ctx->descriptors, desc_nd, node) {
764 if (desc_nd->bDescriptorType == USB_DESC_BOS) {
765 LOG_DBG("bLength %u, len %u, tailroom %u",
766 desc_nd->bLength, len, net_buf_tailroom(buf));
767 net_buf_add_mem(buf, desc_nd->ptr, MIN(len, desc_nd->bLength));
768
769 len -= MIN(len, desc_nd->bLength);
770 if (len == 0) {
771 break;
772 }
773 }
774 }
775
776 return 0;
777 }
778
sreq_get_descriptor(struct usbd_context * const uds_ctx,struct net_buf * const buf)779 static int sreq_get_descriptor(struct usbd_context *const uds_ctx,
780 struct net_buf *const buf)
781 {
782 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
783 uint8_t desc_type = USB_GET_DESCRIPTOR_TYPE(setup->wValue);
784 uint8_t desc_idx = USB_GET_DESCRIPTOR_INDEX(setup->wValue);
785
786 LOG_DBG("Get Descriptor request type %u index %u",
787 desc_type, desc_idx);
788
789 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE) {
790 /*
791 * If the recipient is not the device then it is probably a
792 * class specific request where wIndex is the interface
793 * number or endpoint and not the language ID. e.g. HID
794 * Class Get Descriptor request.
795 */
796 return nonstd_request(uds_ctx, buf);
797 }
798
799 switch (desc_type) {
800 case USB_DESC_DEVICE:
801 return sreq_get_desc_dev(uds_ctx, buf);
802 case USB_DESC_CONFIGURATION:
803 return sreq_get_desc_cfg(uds_ctx, buf, desc_idx, false);
804 case USB_DESC_OTHER_SPEED:
805 return sreq_get_desc_cfg(uds_ctx, buf, desc_idx, true);
806 case USB_DESC_STRING:
807 return sreq_get_desc_str(uds_ctx, buf, desc_idx);
808 case USB_DESC_DEVICE_QUALIFIER:
809 return sreq_get_dev_qualifier(uds_ctx, buf);
810 case USB_DESC_BOS:
811 return sreq_get_desc_bos(uds_ctx, buf);
812 case USB_DESC_INTERFACE:
813 case USB_DESC_ENDPOINT:
814 default:
815 break;
816 }
817
818 errno = -ENOTSUP;
819 return 0;
820 }
821
sreq_get_configuration(struct usbd_context * const uds_ctx,struct net_buf * const buf)822 static int sreq_get_configuration(struct usbd_context *const uds_ctx,
823 struct net_buf *const buf)
824
825 {
826 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
827 uint8_t cfg = usbd_get_config_value(uds_ctx);
828
829 /* Not specified in default state, treat as error */
830 if (usbd_state_is_default(uds_ctx)) {
831 errno = -EPERM;
832 return 0;
833 }
834
835 if (setup->wLength != sizeof(cfg)) {
836 errno = -ENOTSUP;
837 return 0;
838 }
839
840 if (net_buf_tailroom(buf) < setup->wLength) {
841 errno = -ENOMEM;
842 return 0;
843 }
844
845 net_buf_add_u8(buf, cfg);
846
847 return 0;
848 }
849
sreq_get_interface(struct usbd_context * const uds_ctx,struct net_buf * const buf)850 static int sreq_get_interface(struct usbd_context *const uds_ctx,
851 struct net_buf *const buf)
852 {
853 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
854 struct usb_cfg_descriptor *cfg_desc;
855 struct usbd_config_node *cfg_nd;
856 uint8_t cur_alt;
857
858 if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_INTERFACE) {
859 errno = -EPERM;
860 return 0;
861 }
862
863 /* Treat as error in default (not specified) and addressed states. */
864 cfg_nd = usbd_config_get_current(uds_ctx);
865 if (cfg_nd == NULL) {
866 errno = -EPERM;
867 return 0;
868 }
869
870 cfg_desc = cfg_nd->desc;
871
872 if (setup->wIndex > UINT8_MAX ||
873 setup->wIndex > cfg_desc->bNumInterfaces) {
874 errno = -ENOTSUP;
875 return 0;
876 }
877
878 if (usbd_get_alt_value(uds_ctx, setup->wIndex, &cur_alt)) {
879 errno = -ENOTSUP;
880 return 0;
881 }
882
883 LOG_DBG("Get Interfaces %u, alternate %u",
884 setup->wIndex, cur_alt);
885
886 if (setup->wLength != sizeof(cur_alt)) {
887 errno = -ENOTSUP;
888 return 0;
889 }
890
891 if (net_buf_tailroom(buf) < setup->wLength) {
892 errno = -ENOMEM;
893 return 0;
894 }
895
896 net_buf_add_u8(buf, cur_alt);
897
898 return 0;
899 }
900
std_request_to_host(struct usbd_context * const uds_ctx,struct net_buf * const buf)901 static int std_request_to_host(struct usbd_context *const uds_ctx,
902 struct net_buf *const buf)
903 {
904 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
905 int ret;
906
907 switch (setup->bRequest) {
908 case USB_SREQ_GET_STATUS:
909 ret = sreq_get_status(uds_ctx, buf);
910 break;
911 case USB_SREQ_GET_DESCRIPTOR:
912 ret = sreq_get_descriptor(uds_ctx, buf);
913 break;
914 case USB_SREQ_GET_CONFIGURATION:
915 ret = sreq_get_configuration(uds_ctx, buf);
916 break;
917 case USB_SREQ_GET_INTERFACE:
918 ret = sreq_get_interface(uds_ctx, buf);
919 break;
920 default:
921 errno = -ENOTSUP;
922 ret = 0;
923 break;
924 }
925
926 return ret;
927 }
928
vendor_device_request(struct usbd_context * const uds_ctx,struct net_buf * const buf)929 static int vendor_device_request(struct usbd_context *const uds_ctx,
930 struct net_buf *const buf)
931 {
932 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
933 struct usbd_vreq_node *vreq_nd;
934
935 vreq_nd = usbd_device_get_vreq(uds_ctx, setup->bRequest);
936 if (vreq_nd == NULL) {
937 errno = -ENOTSUP;
938 return 0;
939 }
940
941 if (reqtype_is_to_device(setup) && vreq_nd->to_dev != NULL) {
942 LOG_DBG("Vendor request 0x%02x to device", setup->bRequest);
943 errno = vreq_nd->to_dev(uds_ctx, setup, buf);
944 return 0;
945 }
946
947 if (reqtype_is_to_host(setup) && vreq_nd->to_host != NULL) {
948 LOG_DBG("Vendor request 0x%02x to host", setup->bRequest);
949 errno = vreq_nd->to_host(uds_ctx, setup, buf);
950 return 0;
951 }
952
953 errno = -ENOTSUP;
954 return 0;
955 }
956
nonstd_request(struct usbd_context * const uds_ctx,struct net_buf * const dbuf)957 static int nonstd_request(struct usbd_context *const uds_ctx,
958 struct net_buf *const dbuf)
959 {
960 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
961 struct usbd_class_node *c_nd = NULL;
962 int ret = 0;
963
964 switch (setup->RequestType.recipient) {
965 case USB_REQTYPE_RECIPIENT_ENDPOINT:
966 c_nd = usbd_class_get_by_ep(uds_ctx, setup->wIndex);
967 break;
968 case USB_REQTYPE_RECIPIENT_INTERFACE:
969 c_nd = usbd_class_get_by_iface(uds_ctx, setup->wIndex);
970 break;
971 case USB_REQTYPE_RECIPIENT_DEVICE:
972 c_nd = usbd_class_get_by_req(uds_ctx, setup->bRequest);
973 break;
974 default:
975 break;
976 }
977
978 if (c_nd != NULL) {
979 if (reqtype_is_to_device(setup)) {
980 ret = usbd_class_control_to_dev(c_nd->c_data, setup, dbuf);
981 } else {
982 ret = usbd_class_control_to_host(c_nd->c_data, setup, dbuf);
983 }
984 } else {
985 return vendor_device_request(uds_ctx, dbuf);
986 }
987
988 return ret;
989 }
990
handle_setup_request(struct usbd_context * const uds_ctx,struct net_buf * const buf)991 static int handle_setup_request(struct usbd_context *const uds_ctx,
992 struct net_buf *const buf)
993 {
994 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
995 int ret;
996
997 errno = 0;
998
999 switch (setup->RequestType.type) {
1000 case USB_REQTYPE_TYPE_STANDARD:
1001 if (reqtype_is_to_device(setup)) {
1002 ret = std_request_to_device(uds_ctx, buf);
1003 } else {
1004 ret = std_request_to_host(uds_ctx, buf);
1005 }
1006 break;
1007 case USB_REQTYPE_TYPE_CLASS:
1008 case USB_REQTYPE_TYPE_VENDOR:
1009 ret = nonstd_request(uds_ctx, buf);
1010 break;
1011 default:
1012 errno = -ENOTSUP;
1013 ret = 0;
1014 }
1015
1016 if (errno) {
1017 LOG_INF("protocol error:");
1018 LOG_HEXDUMP_INF(setup, sizeof(*setup), "setup:");
1019 if (errno == -ENOTSUP) {
1020 LOG_INF("not supported");
1021 }
1022 if (errno == -EPERM) {
1023 LOG_INF("not permitted in device state %u",
1024 uds_ctx->ch9_data.state);
1025 }
1026 }
1027
1028 return ret;
1029 }
1030
ctrl_xfer_get_setup(struct usbd_context * const uds_ctx,struct net_buf * const buf)1031 static int ctrl_xfer_get_setup(struct usbd_context *const uds_ctx,
1032 struct net_buf *const buf)
1033 {
1034 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
1035 struct net_buf *buf_b;
1036 struct udc_buf_info *bi, *bi_b;
1037
1038 if (buf->len < sizeof(struct usb_setup_packet)) {
1039 return -EINVAL;
1040 }
1041
1042 memcpy(setup, buf->data, sizeof(struct usb_setup_packet));
1043
1044 setup->wValue = sys_le16_to_cpu(setup->wValue);
1045 setup->wIndex = sys_le16_to_cpu(setup->wIndex);
1046 setup->wLength = sys_le16_to_cpu(setup->wLength);
1047
1048 bi = udc_get_buf_info(buf);
1049
1050 buf_b = buf->frags;
1051 if (buf_b == NULL) {
1052 LOG_ERR("Buffer for data|status is missing");
1053 return -ENODATA;
1054 }
1055
1056 bi_b = udc_get_buf_info(buf_b);
1057
1058 if (reqtype_is_to_device(setup)) {
1059 if (setup->wLength) {
1060 if (!bi_b->data) {
1061 LOG_ERR("%p is not data", buf_b);
1062 return -EINVAL;
1063 }
1064 } else {
1065 if (!bi_b->status) {
1066 LOG_ERR("%p is not status", buf_b);
1067 return -EINVAL;
1068 }
1069 }
1070 } else {
1071 if (!setup->wLength) {
1072 LOG_ERR("device-to-host with wLength zero");
1073 return -ENOTSUP;
1074 }
1075
1076 if (!bi_b->data) {
1077 LOG_ERR("%p is not data", buf_b);
1078 return -EINVAL;
1079 }
1080
1081 }
1082
1083 return 0;
1084 }
1085
spool_data_out(struct net_buf * const buf)1086 static struct net_buf *spool_data_out(struct net_buf *const buf)
1087 {
1088 struct net_buf *next_buf = buf;
1089 struct udc_buf_info *bi;
1090
1091 while (next_buf) {
1092 LOG_INF("spool %p", next_buf);
1093 next_buf = net_buf_frag_del(NULL, next_buf);
1094 if (next_buf) {
1095 bi = udc_get_buf_info(next_buf);
1096 if (bi->status) {
1097 return next_buf;
1098 }
1099 }
1100 }
1101
1102 return NULL;
1103 }
1104
usbd_handle_ctrl_xfer(struct usbd_context * const uds_ctx,struct net_buf * const buf,const int err)1105 int usbd_handle_ctrl_xfer(struct usbd_context *const uds_ctx,
1106 struct net_buf *const buf, const int err)
1107 {
1108 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
1109 struct udc_buf_info *bi;
1110 int ret = 0;
1111
1112 bi = udc_get_buf_info(buf);
1113 if (USB_EP_GET_IDX(bi->ep)) {
1114 LOG_ERR("Can only handle control requests");
1115 return -EIO;
1116 }
1117
1118 if (err && err != -ENOMEM && !bi->setup) {
1119 if (err == -ECONNABORTED) {
1120 LOG_INF("Transfer 0x%02x aborted (bus reset?)", bi->ep);
1121 net_buf_unref(buf);
1122 return 0;
1123 }
1124
1125 LOG_ERR("Control transfer for 0x%02x has error %d, halt",
1126 bi->ep, err);
1127 net_buf_unref(buf);
1128 return err;
1129 }
1130
1131 LOG_INF("Handle control %p ep 0x%02x, len %u, s:%u d:%u s:%u",
1132 buf, bi->ep, buf->len, bi->setup, bi->data, bi->status);
1133
1134 if (bi->setup && bi->ep == USB_CONTROL_EP_OUT) {
1135 struct net_buf *next_buf;
1136
1137 if (ctrl_xfer_get_setup(uds_ctx, buf)) {
1138 LOG_ERR("Malformed setup packet");
1139 net_buf_unref(buf);
1140 goto ctrl_xfer_stall;
1141 }
1142
1143 /* Remove setup packet buffer from the chain */
1144 next_buf = net_buf_frag_del(NULL, buf);
1145 if (next_buf == NULL) {
1146 LOG_ERR("Buffer for data|status is missing");
1147 goto ctrl_xfer_stall;
1148 }
1149
1150 /*
1151 * Handle request and data stage, next_buf is either
1152 * data+status or status buffers.
1153 */
1154 ret = handle_setup_request(uds_ctx, next_buf);
1155 if (ret) {
1156 net_buf_unref(next_buf);
1157 return ret;
1158 }
1159
1160 if (errno) {
1161 /*
1162 * Halt, only protocol errors are recoverable.
1163 * Free data stage and linked status stage buffer.
1164 */
1165 net_buf_unref(next_buf);
1166 goto ctrl_xfer_stall;
1167 }
1168
1169 ch9_set_ctrl_type(uds_ctx, CTRL_AWAIT_STATUS_STAGE);
1170 if (reqtype_is_to_device(setup) && setup->wLength) {
1171 /* Enqueue STATUS (IN) buffer */
1172 next_buf = spool_data_out(next_buf);
1173 if (next_buf == NULL) {
1174 LOG_ERR("Buffer for status is missing");
1175 goto ctrl_xfer_stall;
1176 }
1177
1178 ret = usbd_ep_ctrl_enqueue(uds_ctx, next_buf);
1179 } else {
1180 /* Enqueue DATA (IN) or STATUS (OUT) buffer */
1181 ret = usbd_ep_ctrl_enqueue(uds_ctx, next_buf);
1182 }
1183
1184 return ret;
1185 }
1186
1187 if (bi->status && bi->ep == USB_CONTROL_EP_OUT) {
1188 if (ch9_get_ctrl_type(uds_ctx) == CTRL_AWAIT_STATUS_STAGE) {
1189 LOG_INF("s-in-status finished");
1190 } else {
1191 LOG_WRN("Awaited s-in-status not finished");
1192 }
1193
1194 net_buf_unref(buf);
1195
1196 return 0;
1197 }
1198
1199 if (bi->status && bi->ep == USB_CONTROL_EP_IN) {
1200 net_buf_unref(buf);
1201
1202 if (ch9_get_ctrl_type(uds_ctx) == CTRL_AWAIT_STATUS_STAGE) {
1203 LOG_INF("s-(out)-status finished");
1204 if (unlikely(uds_ctx->ch9_data.post_status)) {
1205 ret = post_status_stage(uds_ctx);
1206 }
1207 } else {
1208 LOG_WRN("Awaited s-(out)-status not finished");
1209 }
1210
1211 return ret;
1212 }
1213
1214 ctrl_xfer_stall:
1215 /*
1216 * Halt only the endpoint over which the host expects
1217 * data or status stage. This facilitates the work of the drivers.
1218 *
1219 * In the case there is -ENOMEM for data OUT stage halt
1220 * control OUT endpoint.
1221 */
1222 if (reqtype_is_to_host(setup)) {
1223 ret = udc_ep_set_halt(uds_ctx->dev, USB_CONTROL_EP_IN);
1224 } else if (setup->wLength) {
1225 uint8_t ep = (err == -ENOMEM) ? USB_CONTROL_EP_OUT : USB_CONTROL_EP_IN;
1226
1227 ret = udc_ep_set_halt(uds_ctx->dev, ep);
1228 } else {
1229 ret = udc_ep_set_halt(uds_ctx->dev, USB_CONTROL_EP_IN);
1230 }
1231
1232 ch9_set_ctrl_type(uds_ctx, CTRL_AWAIT_SETUP_DATA);
1233
1234 return ret;
1235 }
1236
usbd_init_control_pipe(struct usbd_context * const uds_ctx)1237 int usbd_init_control_pipe(struct usbd_context *const uds_ctx)
1238 {
1239 uds_ctx->ch9_data.state = USBD_STATE_DEFAULT;
1240 ch9_set_ctrl_type(uds_ctx, CTRL_AWAIT_SETUP_DATA);
1241
1242 return 0;
1243 }
1244