summaryrefslogtreecommitdiff
path: root/tmk_core/protocol/arm_atsam/usb/udc.c
blob: 12444d305968e58814fa156397a048ad82ee0cde (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
/**
 * \file
 *
 * \brief USB Device Controller (UDC)
 *
 * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */

#include "conf_usb.h"
#include "usb_protocol.h"
#include "udd.h"
#include "udc_desc.h"
#include "udi_device_conf.h"
#include "udi.h"
#include "udc.h"
#include "md_bootloader.h"

/**
 * \ingroup udc_group
 * \defgroup udc_group_interne Implementation of UDC
 *
 * Internal implementation
 * @{
 */

//! \name Internal variables to manage the USB device
//! @{

//! Device status state (see enum usb_device_status in usb_protocol.h)
static le16_t udc_device_status;

COMPILER_WORD_ALIGNED
//! Device interface setting value
static uint8_t udc_iface_setting = 0;

//! Device Configuration number selected by the USB host
COMPILER_WORD_ALIGNED
static uint8_t udc_num_configuration = 0;

//! Pointer on the selected speed device configuration
static udc_config_speed_t UDC_DESC_STORAGE *udc_ptr_conf;

//! Pointer on interface descriptor used by SETUP request.
static usb_iface_desc_t UDC_DESC_STORAGE *udc_ptr_iface;

//! @}


//! \name Internal structure to store the USB device main strings
//! @{

/**
 * \brief Language ID of USB device (US ID by default)
 */
COMPILER_WORD_ALIGNED
static UDC_DESC_STORAGE usb_str_lgid_desc_t udc_string_desc_languageid = {
    .desc.bLength = sizeof(usb_str_lgid_desc_t),
    .desc.bDescriptorType = USB_DT_STRING,
    .string = {LE16(USB_LANGID_EN_US)}
};

/**
 * \brief USB device manufacture name storage
 * String is allocated only if USB_DEVICE_MANUFACTURE_NAME is declared
 * by usb application configuration
 */
#ifdef USB_DEVICE_MANUFACTURE_NAME
static uint8_t udc_string_manufacturer_name[] = USB_DEVICE_MANUFACTURE_NAME;
#define USB_DEVICE_MANUFACTURE_NAME_SIZE (sizeof(udc_string_manufacturer_name)-1)
#else
#define USB_DEVICE_MANUFACTURE_NAME_SIZE 0
#endif

/**
 * \brief USB device product name storage
 * String is allocated only if USB_DEVICE_PRODUCT_NAME is declared
 * by usb application configuration
 */
#ifdef USB_DEVICE_PRODUCT_NAME
static uint8_t udc_string_product_name[] = USB_DEVICE_PRODUCT_NAME;
#define USB_DEVICE_PRODUCT_NAME_SIZE (sizeof(udc_string_product_name)-1)
#else
#define USB_DEVICE_PRODUCT_NAME_SIZE 0
#endif

#if defined USB_DEVICE_SERIAL_NAME
#define USB_DEVICE_SERIAL_NAME_SIZE (sizeof(USB_DEVICE_SERIAL_NAME)-1)
#else
#define USB_DEVICE_SERIAL_NAME_SIZE 0
#endif

uint8_t usb_device_serial_name_size = 0;
#if defined USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL
uint8_t bootloader_serial_number[BOOTLOADER_SERIAL_MAX_SIZE+1]="";
#endif
static const uint8_t *udc_get_string_serial_name(void)
{
#if defined USB_DEVICE_SERIAL_USE_BOOTLOADER_SERIAL
    uint32_t serial_ptrloc = (uint32_t)&_srom - 4;
    uint32_t serial_address = *(uint32_t *)serial_ptrloc; //Address of bootloader's serial number if available

    if (serial_address != 0xFFFFFFFF && serial_address < serial_ptrloc) //Check for factory programmed serial address
    {
        if ((serial_address & 0xFF) % 4 == 0) //Check alignment
        {
            uint16_t *serial_use = (uint16_t *)(serial_address); //Point to address of string in rom
            uint8_t serial_length = 0;

            while ((*(serial_use + serial_length) > 32 && *(serial_use + serial_length) < 127) &&
                   serial_length < BOOTLOADER_SERIAL_MAX_SIZE)
            {
                bootloader_serial_number[serial_length] = *(serial_use + serial_length) & 0xFF;
                serial_length++;
            }
            bootloader_serial_number[serial_length] = 0;

            usb_device_serial_name_size = serial_length;

            return bootloader_serial_number; //Use serial programmed into bootloader rom
        }
    }
#endif

    usb_device_serial_name_size = USB_DEVICE_SERIAL_NAME_SIZE;

#if defined USB_DEVICE_SERIAL_NAME
    return (const uint8_t *)USB_DEVICE_SERIAL_NAME; //Use serial supplied by keyboard's config.h
#else
    return 0; //No serial supplied
#endif
}

/**
 * \brief USB device string descriptor
 * Structure used to transfer ASCII strings to USB String descriptor structure.
 */
#ifndef BOOTLOADER_SERIAL_MAX_SIZE
#define BOOTLOADER_SERIAL_MAX_SIZE 0
#endif //BOOTLOADER_SERIAL_MAX_SIZE
struct udc_string_desc_t {
    usb_str_desc_t header;
    le16_t string[Max(Max(Max(USB_DEVICE_MANUFACTURE_NAME_SIZE, \
                  USB_DEVICE_PRODUCT_NAME_SIZE), USB_DEVICE_SERIAL_NAME_SIZE), \
                  BOOTLOADER_SERIAL_MAX_SIZE)];
};
COMPILER_WORD_ALIGNED
static UDC_DESC_STORAGE struct udc_string_desc_t udc_string_desc = {
    .header.bDescriptorType = USB_DT_STRING
};
//! @}

usb_iface_desc_t UDC_DESC_STORAGE *udc_get_interface_desc(void)
{
    return udc_ptr_iface;
}

/**
 * \brief Returns a value to check the end of USB Configuration descriptor
 *
 * \return address after the last byte of USB Configuration descriptor
 */
static usb_conf_desc_t UDC_DESC_STORAGE *udc_get_eof_conf(void)
{
    return (UDC_DESC_STORAGE usb_conf_desc_t *) ((uint8_t *)
            udc_ptr_conf->desc +
            le16_to_cpu(udc_ptr_conf->desc->wTotalLength));
}

#if (0!=USB_DEVICE_MAX_EP)
/**
 * \brief Search specific descriptor in global interface descriptor
 *
 * \param desc       Address of interface descriptor
 *                   or previous specific descriptor found
 * \param desc_id    Descriptor ID to search
 *
 * \return address of specific descriptor found
 * \return NULL if it is the end of global interface descriptor
 */
static usb_conf_desc_t UDC_DESC_STORAGE *udc_next_desc_in_iface(usb_conf_desc_t
        UDC_DESC_STORAGE * desc, uint8_t desc_id)
{
    usb_conf_desc_t UDC_DESC_STORAGE *ptr_eof_desc;

    ptr_eof_desc = udc_get_eof_conf();
    // Go to next descriptor
    desc = (UDC_DESC_STORAGE usb_conf_desc_t *) ((uint8_t *) desc +
            desc->bLength);
    // Check the end of configuration descriptor
    while (ptr_eof_desc > desc) {
        // If new interface descriptor is found,
        // then it is the end of the current global interface descriptor
        if (USB_DT_INTERFACE == desc->bDescriptorType) {
            break; // End of global interface descriptor
        }
        if (desc_id == desc->bDescriptorType) {
            return desc; // Specific descriptor found
        }
        // Go to next descriptor
        desc = (UDC_DESC_STORAGE usb_conf_desc_t *) ((uint8_t *) desc +
                desc->bLength);
    }
    return NULL; // No specific descriptor found
}
#endif

/**
 * \brief Search an interface descriptor
 * This routine updates the internal pointer udc_ptr_iface.
 *
 * \param iface_num     Interface number to find in Configuration Descriptor
 * \param setting_num   Setting number of interface to find
 *
 * \return 1 if found or 0 if not found
 */
static bool udc_update_iface_desc(uint8_t iface_num, uint8_t setting_num)
{
    usb_conf_desc_t UDC_DESC_STORAGE *ptr_end_desc;

    if (0 == udc_num_configuration) {
        return false;
    }

    if (iface_num >= udc_ptr_conf->desc->bNumInterfaces) {
        return false;
    }

    // Start at the beginning of configuration descriptor
    udc_ptr_iface = (UDC_DESC_STORAGE usb_iface_desc_t *)
            udc_ptr_conf->desc;

    // Check the end of configuration descriptor
    ptr_end_desc = udc_get_eof_conf();
    while (ptr_end_desc >
            (UDC_DESC_STORAGE usb_conf_desc_t *) udc_ptr_iface) {
        if (USB_DT_INTERFACE == udc_ptr_iface->bDescriptorType) {
            // A interface descriptor is found
            // Check interface and alternate setting number
            if ((iface_num == udc_ptr_iface->bInterfaceNumber) &&
                    (setting_num ==
                    udc_ptr_iface->bAlternateSetting)) {
                return true; // Interface found
            }
        }
        // Go to next descriptor
        udc_ptr_iface = (UDC_DESC_STORAGE usb_iface_desc_t *) (
                (uint8_t *) udc_ptr_iface +
                udc_ptr_iface->bLength);
    }
    return false; // Interface not found
}

/**
 * \brief Disables an usb device interface (UDI)
 * This routine call the UDI corresponding to interface number
 *
 * \param iface_num     Interface number to disable
 *
 * \return 1 if it is done or 0 if interface is not found
 */
static bool udc_iface_disable(uint8_t iface_num)
{
    udi_api_t UDC_DESC_STORAGE *udi_api;

    // Select first alternate setting of the interface
    // to update udc_ptr_iface before call iface->getsetting()
    if (!udc_update_iface_desc(iface_num, 0)) {
        return false;
    }

    // Select the interface with the current alternate setting
    udi_api = udc_ptr_conf->udi_apis[iface_num];

#if (0!=USB_DEVICE_MAX_EP)
    if (!udc_update_iface_desc(iface_num, udi_api->getsetting())) {
        return false;
    }

    // Start at the beginning of interface descriptor
    {
        usb_ep_desc_t UDC_DESC_STORAGE *ep_desc;
        ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *) udc_ptr_iface;
        while (1) {
            // Search Endpoint descriptor included in global interface descriptor
            ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *)
                    udc_next_desc_in_iface((UDC_DESC_STORAGE
                    usb_conf_desc_t *)
                    ep_desc, USB_DT_ENDPOINT);
            if (NULL == ep_desc) {
                break;
            }
            // Free the endpoint used by the interface
            udd_ep_free(ep_desc->bEndpointAddress);
        }
    }
#endif

    // Disable interface
    udi_api->disable();
    return true;
}

/**
 * \brief Enables an usb device interface (UDI)
 * This routine calls the UDI corresponding
 * to the interface and setting number.
 *
 * \param iface_num     Interface number to enable
 * \param setting_num   Setting number to enable
 *
 * \return 1 if it is done or 0 if interface is not found
 */
static bool udc_iface_enable(uint8_t iface_num, uint8_t setting_num)
{
    // Select the interface descriptor
    if (!udc_update_iface_desc(iface_num, setting_num)) {
        return false;
    }

#if (0!=USB_DEVICE_MAX_EP)
    usb_ep_desc_t UDC_DESC_STORAGE *ep_desc;

    // Start at the beginning of the global interface descriptor
    ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *) udc_ptr_iface;
    while (1) {
        // Search Endpoint descriptor included in the global interface descriptor
        ep_desc = (UDC_DESC_STORAGE usb_ep_desc_t *)
                udc_next_desc_in_iface((UDC_DESC_STORAGE
                        usb_conf_desc_t *) ep_desc,
                USB_DT_ENDPOINT);
        if (NULL == ep_desc)
            break;
        // Alloc the endpoint used by the interface
        if (!udd_ep_alloc(ep_desc->bEndpointAddress,
                ep_desc->bmAttributes,
                le16_to_cpu
                (ep_desc->wMaxPacketSize))) {
            return false;
        }
    }
#endif
    // Enable the interface
    return udc_ptr_conf->udi_apis[iface_num]->enable();
}

/*! \brief Start the USB Device stack
 */
void udc_start(void)
{
    udd_enable();
}

/*! \brief Stop the USB Device stack
 */
void udc_stop(void)
{
    udd_disable();
    udc_reset();
}

/**
 * \brief Reset the current configuration of the USB device,
 * This routines can be called by UDD when a RESET on the USB line occurs.
 */
void udc_reset(void)
{
    uint8_t iface_num;

    if (udc_num_configuration) {
        for (iface_num = 0;
                iface_num < udc_ptr_conf->desc->bNumInterfaces;
                iface_num++) {
            udc_iface_disable(iface_num);
        }
    }
    udc_num_configuration = 0;
#if (USB_CONFIG_ATTR_REMOTE_WAKEUP \
    == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP))
    if (CPU_TO_LE16(USB_DEV_STATUS_REMOTEWAKEUP) & udc_device_status) {
        // Remote wakeup is enabled then disable it
        UDC_REMOTEWAKEUP_DISABLE();
    }
#endif
    udc_device_status =
#if (USB_DEVICE_ATTR & USB_CONFIG_ATTR_SELF_POWERED)
            CPU_TO_LE16(USB_DEV_STATUS_SELF_POWERED);
#else
            CPU_TO_LE16(USB_DEV_STATUS_BUS_POWERED);
#endif
}

void udc_sof_notify(void)
{
    uint8_t iface_num;

    if (udc_num_configuration) {
        for (iface_num = 0;
                iface_num < udc_ptr_conf->desc->bNumInterfaces;
                iface_num++) {
            if (udc_ptr_conf->udi_apis[iface_num]->sof_notify != NULL) {
                udc_ptr_conf->udi_apis[iface_num]->sof_notify();
            }
        }
    }
}

/**
 * \brief Standard device request to get device status
 *
 * \return true if success
 */
static bool udc_req_std_dev_get_status(void)
{
    if (udd_g_ctrlreq.req.wLength != sizeof(udc_device_status)) {
        return false;
    }

    udd_set_setup_payload( (uint8_t *) & udc_device_status,
            sizeof(udc_device_status));
    return true;
}

#if (0!=USB_DEVICE_MAX_EP)
/**
 * \brief Standard endpoint request to get endpoint status
 *
 * \return true if success
 */
static bool udc_req_std_ep_get_status(void)
{
    static le16_t udc_ep_status;

    if (udd_g_ctrlreq.req.wLength != sizeof(udc_ep_status)) {
        return false;
    }

    udc_ep_status = udd_ep_is_halted(udd_g_ctrlreq.req.
            wIndex & 0xFF) ? CPU_TO_LE16(USB_EP_STATUS_HALTED) : 0;

    udd_set_setup_payload( (uint8_t *) & udc_ep_status,
            sizeof(udc_ep_status));
    return true;
}
#endif

/**
 * \brief Standard device request to change device status
 *
 * \return true if success
 */
static bool udc_req_std_dev_clear_feature(void)
{
    if (udd_g_ctrlreq.req.wLength) {
        return false;
    }

    if (udd_g_ctrlreq.req.wValue == USB_DEV_FEATURE_REMOTE_WAKEUP) {
        udc_device_status &= CPU_TO_LE16(~(uint32_t)USB_DEV_STATUS_REMOTEWAKEUP);
#if (USB_CONFIG_ATTR_REMOTE_WAKEUP \
    == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP))
        UDC_REMOTEWAKEUP_DISABLE();
#endif
        return true;
    }
    return false;
}

#if (0!=USB_DEVICE_MAX_EP)
/**
 * \brief Standard endpoint request to clear endpoint feature
 *
 * \return true if success
 */
static bool udc_req_std_ep_clear_feature(void)
{
    if (udd_g_ctrlreq.req.wLength) {
        return false;
    }

    if (udd_g_ctrlreq.req.wValue == USB_EP_FEATURE_HALT) {
        return udd_ep_clear_halt(udd_g_ctrlreq.req.wIndex & 0xFF);
    }
    return false;
}
#endif

/**
 * \brief Standard device request to set a feature
 *
 * \return true if success
 */
static bool udc_req_std_dev_set_feature(void)
{
    if (udd_g_ctrlreq.req.wLength) {
        return false;
    }

    switch (udd_g_ctrlreq.req.wValue) {

    case USB_DEV_FEATURE_REMOTE_WAKEUP:
#if (USB_CONFIG_ATTR_REMOTE_WAKEUP \
    == (USB_DEVICE_ATTR & USB_CONFIG_ATTR_REMOTE_WAKEUP))
        udc_device_status |= CPU_TO_LE16(USB_DEV_STATUS_REMOTEWAKEUP);
        UDC_REMOTEWAKEUP_ENABLE();
        return true;
#else
        return false;
#endif

#ifdef USB_DEVICE_HS_SUPPORT
    case USB_DEV_FEATURE_TEST_MODE:
        if (!udd_is_high_speed()) {
            break;
        }
        if (udd_g_ctrlreq.req.wIndex & 0xff) {
            break;
        }
        // Unconfigure the device, terminating all ongoing requests
        udc_reset();
        switch ((udd_g_ctrlreq.req.wIndex >> 8) & 0xFF) {
        case USB_DEV_TEST_MODE_J:
            udd_g_ctrlreq.callback = udd_test_mode_j;
            return true;

        case USB_DEV_TEST_MODE_K:
            udd_g_ctrlreq.callback = udd_test_mode_k;
            return true;

        case USB_DEV_TEST_MODE_SE0_NAK:
            udd_g_ctrlreq.callback = udd_test_mode_se0_nak;
            return true;

        case USB_DEV_TEST_MODE_PACKET:
            udd_g_ctrlreq.callback = udd_test_mode_packet;
            return true;

        case USB_DEV_TEST_MODE_FORCE_ENABLE: // Only for downstream facing hub ports
        default:
            break;
        }
        break;
#endif
    default:
        break;
    }
    return false;
}

/**
 * \brief Standard endpoint request to halt an endpoint
 *
 * \return true if success
 */
#if (0!=USB_DEVICE_MAX_EP)
static bool udc_req_std_ep_set_feature(void)
{
    if (udd_g_ctrlreq.req.wLength) {
        return false;
    }
    if (udd_g_ctrlreq.req.wValue == USB_EP_FEATURE_HALT) {
        udd_ep_abort(udd_g_ctrlreq.req.wIndex & 0xFF);
        return udd_ep_set_halt(udd_g_ctrlreq.req.wIndex & 0xFF);
    }
    return false;
}
#endif

/**
 * \brief Change the address of device
 * Callback called at the end of request set address
 */
static void udc_valid_address(void)
{
    udd_set_address(udd_g_ctrlreq.req.wValue & 0x7F);
}

/**
 * \brief Standard device request to set device address
 *
 * \return true if success
 */
static bool udc_req_std_dev_set_address(void)
{
    if (udd_g_ctrlreq.req.wLength) {
        return false;
    }

    // The address must be changed at the end of setup request after the handshake
    // then we use a callback to change address
    udd_g_ctrlreq.callback = udc_valid_address;
    return true;
}

/**
 * \brief Standard device request to get device string descriptor
 *
 * \return true if success
 */
static bool udc_req_std_dev_get_str_desc(void)
{
    uint8_t i;
    const uint8_t *str;
    uint8_t str_length = 0;

    // Link payload pointer to the string corresponding at request
    switch (udd_g_ctrlreq.req.wValue & 0xff) {
    case 0:
        udd_set_setup_payload((uint8_t *) &udc_string_desc_languageid,
                sizeof(udc_string_desc_languageid));
        break;

#ifdef USB_DEVICE_MANUFACTURE_NAME
    case 1:
        str_length = USB_DEVICE_MANUFACTURE_NAME_SIZE;
        str = udc_string_manufacturer_name;
        break;
#endif
#ifdef USB_DEVICE_PRODUCT_NAME
    case 2:
        str_length = USB_DEVICE_PRODUCT_NAME_SIZE;
        str = udc_string_product_name;
        break;
#endif
    case 3:
        str = udc_get_string_serial_name();
        str_length = usb_device_serial_name_size;
        break;
    default:
#ifdef UDC_GET_EXTRA_STRING
        if (UDC_GET_EXTRA_STRING()) {
            break;
        }
#endif
        return false;
    }

    if (str_length) {
        for(i = 0; i < str_length; i++) {
            udc_string_desc.string[i] = cpu_to_le16((le16_t)str[i]);
        }

        udc_string_desc.header.bLength = 2 + (str_length) * 2;
        udd_set_setup_payload(
            (uint8_t *) &udc_string_desc,
            udc_string_desc.header.bLength);
    }

    return true;
}

/**
 * \brief Standard device request to get descriptors about USB device
 *
 * \return true if success
 */
static bool udc_req_std_dev_get_descriptor(void)
{
    uint8_t conf_num;

    conf_num = udd_g_ctrlreq.req.wValue & 0xff;

    // Check descriptor ID
    switch ((uint8_t) (udd_g_ctrlreq.req.wValue >> 8)) {
    case USB_DT_DEVICE:
        // Device descriptor requested
#ifdef USB_DEVICE_HS_SUPPORT
        if (!udd_is_high_speed()) {
            udd_set_setup_payload(
                (uint8_t *) udc_config.confdev_hs,
                udc_config.confdev_hs->bLength);
        } else
#endif
        {
            udd_set_setup_payload(
                (uint8_t *) udc_config.confdev_lsfs,
                udc_config.confdev_lsfs->bLength);
        }
        break;

    case USB_DT_CONFIGURATION:
        // Configuration descriptor requested
#ifdef USB_DEVICE_HS_SUPPORT
        if (udd_is_high_speed()) {
            // HS descriptor
            if (conf_num >= udc_config.confdev_hs->bNumConfigurations) {
                return false;
            }
            udd_set_setup_payload(
                (uint8_t *)udc_config.conf_hs[conf_num].desc,
                le16_to_cpu(udc_config.conf_hs[conf_num].desc->wTotalLength));
        } else
#endif
        {
            // FS descriptor
            if (conf_num >= udc_config.confdev_lsfs->bNumConfigurations) {
                return false;
            }
            udd_set_setup_payload(
                (uint8_t *)udc_config.conf_lsfs[conf_num].desc,
                le16_to_cpu(udc_config.conf_lsfs[conf_num].desc->wTotalLength));
        }
        ((usb_conf_desc_t *) udd_g_ctrlreq.payload)->bDescriptorType =
                USB_DT_CONFIGURATION;
        break;

#ifdef USB_DEVICE_HS_SUPPORT
    case USB_DT_DEVICE_QUALIFIER:
        // Device qualifier descriptor requested
        udd_set_setup_payload( (uint8_t *) udc_config.qualifier,
                udc_config.qualifier->bLength);
        break;

    case USB_DT_OTHER_SPEED_CONFIGURATION:
        // Other configuration descriptor requested
        if (!udd_is_high_speed()) {
            // HS descriptor
            if (conf_num >= udc_config.confdev_hs->bNumConfigurations) {
                return false;
            }
            udd_set_setup_payload(
                (uint8_t *)udc_config.conf_hs[conf_num].desc,
                le16_to_cpu(udc_config.conf_hs[conf_num].desc->wTotalLength));
        } else {
            // FS descriptor
            if (conf_num >= udc_config.confdev_lsfs->bNumConfigurations) {
                return false;
            }
            udd_set_setup_payload(
                (uint8_t *)udc_config.conf_lsfs[conf_num].desc,
                le16_to_cpu(udc_config.conf_lsfs[conf_num].desc->wTotalLength));
        }
        ((usb_conf_desc_t *) udd_g_ctrlreq.payload)->bDescriptorType =
                USB_DT_OTHER_SPEED_CONFIGURATION;
        break;
#endif

    case USB_DT_BOS:
        // Device BOS descriptor requested
        if (udc_config.conf_bos == NULL) {
            return false;
        }
        udd_set_setup_payload( (uint8_t *) udc_config.conf_bos,
                udc_config.conf_bos->wTotalLength);
        break;

    case USB_DT_STRING:
        // String descriptor requested
        if (!udc_req_std_dev_get_str_desc()) {
            return false;
        }
        break;

    default:
        // Unknown descriptor requested
        return false;
    }
    // if the descriptor is larger than length requested, then reduce it
    if (udd_g_ctrlreq.req.wLength < udd_g_ctrlreq.payload_size) {
        udd_g_ctrlreq.payload_size = udd_g_ctrlreq.req.wLength;
    }
    return true;
}

/**
 * \brief Standard device request to get configuration number
 *
 * \return true if success
 */
static bool udc_req_std_dev_get_configuration(void)
{
    if (udd_g_ctrlreq.req.wLength != 1) {
        return false;
    }

    udd_set_setup_payload(&udc_num_configuration,1);
    return true;
}

/**
 * \brief Standard device request to enable a configuration
 *
 * \return true if success
 */
static bool udc_req_std_dev_set_configuration(void)
{
    uint8_t iface_num;

    // Check request length
    if (udd_g_ctrlreq.req.wLength) {
        return false;
    }
    // Authorize configuration only if the address is valid
    if (!udd_getaddress()) {
        return false;
    }
    // Check the configuration number requested
#ifdef USB_DEVICE_HS_SUPPORT
    if (udd_is_high_speed()) {
        // HS descriptor
        if ((udd_g_ctrlreq.req.wValue & 0xFF) >
                udc_config.confdev_hs->bNumConfigurations) {
            return false;
        }
    } else
#endif
    {
        // FS descriptor
        if ((udd_g_ctrlreq.req.wValue & 0xFF) >
                udc_config.confdev_lsfs->bNumConfigurations) {
            return false;
        }
    }

    // Reset current configuration
    udc_reset();

    // Enable new configuration
    udc_num_configuration = udd_g_ctrlreq.req.wValue & 0xFF;
    if (udc_num_configuration == 0) {
        return true; // Default empty configuration requested
    }
    // Update pointer of the configuration descriptor
#ifdef USB_DEVICE_HS_SUPPORT
    if (udd_is_high_speed()) {
        // HS descriptor
        udc_ptr_conf = &udc_config.conf_hs[udc_num_configuration - 1];
    } else
#endif
    {
        // FS descriptor
        udc_ptr_conf = &udc_config.conf_lsfs[udc_num_configuration - 1];
    }
    // Enable all interfaces of the selected configuration
    for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces;
            iface_num++) {
        if (!udc_iface_enable(iface_num, 0)) {
            return false;
        }
    }
    return true;
}

/**
 * \brief Standard interface request
 * to get the alternate setting number of an interface
 *
 * \return true if success
 */
static bool udc_req_std_iface_get_setting(void)
{
    uint8_t iface_num;
    udi_api_t UDC_DESC_STORAGE *udi_api;

    if (udd_g_ctrlreq.req.wLength != 1) {
        return false; // Error in request
    }
    if (!udc_num_configuration) {
        return false; // The device is not is configured state yet
    }

    // Check the interface number included in the request
    iface_num = udd_g_ctrlreq.req.wIndex & 0xFF;
    if (iface_num >= udc_ptr_conf->desc->bNumInterfaces) {
        return false;
    }

    // Select first alternate setting of the interface to update udc_ptr_iface
    // before call iface->getsetting()
    if (!udc_update_iface_desc(iface_num, 0)) {
        return false;
    }
    // Get alternate setting from UDI
    udi_api = udc_ptr_conf->udi_apis[iface_num];
    udc_iface_setting = udi_api->getsetting();

    // Link value to payload pointer of request
    udd_set_setup_payload(&udc_iface_setting,1);
    return true;
}

/**
 * \brief Standard interface request
 * to set an alternate setting of an interface
 *
 * \return true if success
 */
static bool udc_req_std_iface_set_setting(void)
{
    uint8_t iface_num, setting_num;

    if (udd_g_ctrlreq.req.wLength) {
        return false; // Error in request
    }
    if (!udc_num_configuration) {
        return false; // The device is not is configured state yet
    }

    iface_num = udd_g_ctrlreq.req.wIndex & 0xFF;
    setting_num = udd_g_ctrlreq.req.wValue & 0xFF;

    // Disable current setting
    if (!udc_iface_disable(iface_num)) {
        return false;
    }

    // Enable new setting
    return udc_iface_enable(iface_num, setting_num);
}

/**
 * \brief Main routine to manage the standard USB SETUP request
 *
 * \return true if the request is supported
 */
static bool udc_reqstd(void)
{
    if (Udd_setup_is_in()) {
        // GET Standard Requests
        if (udd_g_ctrlreq.req.wLength == 0) {
            return false; // Error for USB host
        }

        if (USB_REQ_RECIP_DEVICE == Udd_setup_recipient()) {
            // Standard Get Device request
            switch (udd_g_ctrlreq.req.bRequest) {
            case USB_REQ_GET_STATUS:
                return udc_req_std_dev_get_status();
            case USB_REQ_GET_DESCRIPTOR:
                return udc_req_std_dev_get_descriptor();
            case USB_REQ_GET_CONFIGURATION:
                return udc_req_std_dev_get_configuration();
            default:
                break;
            }
        }

        if (USB_REQ_RECIP_INTERFACE == Udd_setup_recipient()) {
            // Standard Get Interface request
            switch (udd_g_ctrlreq.req.bRequest) {
            case USB_REQ_GET_INTERFACE:
                return udc_req_std_iface_get_setting();
            default:
                break;
            }
        }
#if (0!=USB_DEVICE_MAX_EP)
        if (USB_REQ_RECIP_ENDPOINT == Udd_setup_recipient()) {
            // Standard Get Endpoint request
            switch (udd_g_ctrlreq.req.bRequest) {
            case USB_REQ_GET_STATUS:
                return udc_req_std_ep_get_status();
            default:
                break;
            }
        }
#endif
    } else {
        // SET Standard Requests
        if (USB_REQ_RECIP_DEVICE == Udd_setup_recipient()) {
            // Standard Set Device request
            switch (udd_g_ctrlreq.req.bRequest) {
            case USB_REQ_SET_ADDRESS:
                return udc_req_std_dev_set_address();
            case USB_REQ_CLEAR_FEATURE:
                return udc_req_std_dev_clear_feature();
            case USB_REQ_SET_FEATURE:
                return udc_req_std_dev_set_feature();
            case USB_REQ_SET_CONFIGURATION:
                return udc_req_std_dev_set_configuration();
            case USB_REQ_SET_DESCRIPTOR:
                /* Not supported (defined as optional by the USB 2.0 spec) */
                break;
            default:
                break;
            }
        }

        if (USB_REQ_RECIP_INTERFACE == Udd_setup_recipient()) {
            // Standard Set Interface request
            switch (udd_g_ctrlreq.req.bRequest) {
            case USB_REQ_SET_INTERFACE:
                return udc_req_std_iface_set_setting();
            default:
                break;
            }
        }
#if (0!=USB_DEVICE_MAX_EP)
        if (USB_REQ_RECIP_ENDPOINT == Udd_setup_recipient()) {
            // Standard Set Endpoint request
            switch (udd_g_ctrlreq.req.bRequest) {
            case USB_REQ_CLEAR_FEATURE:
                return udc_req_std_ep_clear_feature();
            case USB_REQ_SET_FEATURE:
                return udc_req_std_ep_set_feature();
            default:
                break;
            }
        }
#endif
    }
    return false;
}

/**
 * \brief Send the SETUP interface request to UDI
 *
 * \return true if the request is supported
 */
static bool udc_req_iface(void)
{
    uint8_t iface_num;
    udi_api_t UDC_DESC_STORAGE *udi_api;

    if (0 == udc_num_configuration) {
        return false; // The device is not is configured state yet
    }
    // Check interface number
    iface_num = udd_g_ctrlreq.req.wIndex & 0xFF;
    if (iface_num >= udc_ptr_conf->desc->bNumInterfaces) {
        return false;
    }

    //* To update udc_ptr_iface with the selected interface in request
    // Select first alternate setting of interface to update udc_ptr_iface
    // before calling udi_api->getsetting()
    if (!udc_update_iface_desc(iface_num, 0)) {
        return false;
    }
    // Select the interface with the current alternate setting
    udi_api = udc_ptr_conf->udi_apis[iface_num];
    if (!udc_update_iface_desc(iface_num, udi_api->getsetting())) {
        return false;
    }

    // Send the SETUP request to the UDI corresponding to the interface number
    return udi_api->setup();
}

/**
 * \brief Send the SETUP interface request to UDI
 *
 * \return true if the request is supported
 */
static bool udc_req_ep(void)
{
    uint8_t iface_num;
    udi_api_t UDC_DESC_STORAGE *udi_api;

    if (0 == udc_num_configuration) {
        return false; // The device is not is configured state yet
    }
    // Send this request on all enabled interfaces
    iface_num = udd_g_ctrlreq.req.wIndex & 0xFF;
    for (iface_num = 0; iface_num < udc_ptr_conf->desc->bNumInterfaces;
            iface_num++) {
        // Select the interface with the current alternate setting
        udi_api = udc_ptr_conf->udi_apis[iface_num];
        if (!udc_update_iface_desc(iface_num, udi_api->getsetting())) {
            return false;
        }

        // Send the SETUP request to the UDI
        if (udi_api->setup()) {
            return true;
        }
    }
    return false;
}

/**
 * \brief Main routine to manage the USB SETUP request.
 *
 * This function parses a USB SETUP request and submits an appropriate
 * response back to the host or, in the case of SETUP OUT requests
 * with data, sets up a buffer for receiving the data payload.
 *
 * The main standard requests defined by the USB 2.0 standard are handled
 * internally. The interface requests are sent to UDI, and the specific request
 * sent to a specific application callback.
 *
 * \return true if the request is supported, else the request is stalled by UDD
 */
bool udc_process_setup(void)
{
    // By default no data (receive/send) and no callbacks registered
    udd_g_ctrlreq.payload_size = 0;
    udd_g_ctrlreq.callback = NULL;
    udd_g_ctrlreq.over_under_run = NULL;

    if (Udd_setup_is_in()) {
        if (udd_g_ctrlreq.req.wLength == 0) {
            return false; // Error from USB host
        }
    }

    // If standard request then try to decode it in UDC
    if (Udd_setup_type() == USB_REQ_TYPE_STANDARD) {
        if (udc_reqstd()) {
            return true;
        }
    }

    // If interface request then try to decode it in UDI
    if (Udd_setup_recipient() == USB_REQ_RECIP_INTERFACE) {
        if (udc_req_iface()) {
            return true;
        }
    }

    // If endpoint request then try to decode it in UDI
    if (Udd_setup_recipient() == USB_REQ_RECIP_ENDPOINT) {
        if (udc_req_ep()) {
            return true;
        }
    }

    // Here SETUP request unknown by UDC and UDIs
#ifdef USB_DEVICE_SPECIFIC_REQUEST
    // Try to decode it in specific callback
    return USB_DEVICE_SPECIFIC_REQUEST(); // Ex: Vendor request,...
#else
    return false;
#endif
}

//! @}