1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Utility                                                             */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_utility_descriptor_pack                         PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function will pack an application structure into a USB         */
43 /*    descriptor.                                                         */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    descriptor                            Pointer to the unpacked       */
48 /*                                            descriptor                  */
49 /*    descriptor_structure                  Components of the descriptor  */
50 /*    descriptor_entries                    Number of entries in the      */
51 /*                                            descriptor                  */
52 /*    raw_descriptor                        Pointer to packed descriptor  */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_utility_long_put                  Put 32-bit value              */
61 /*    _ux_utility_short_put                 Put 16-bit value              */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    USBX Components                                                     */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            optimized USB descriptors,  */
76 /*                                            resulting in version 6.3.0  */
77 /*                                                                        */
78 /**************************************************************************/
_ux_utility_descriptor_pack(UCHAR * descriptor,UCHAR * descriptor_structure,UINT descriptor_entries,UCHAR * raw_descriptor)79 VOID  _ux_utility_descriptor_pack(UCHAR * descriptor, UCHAR * descriptor_structure,
80                         UINT descriptor_entries, UCHAR * raw_descriptor)
81 {
82 
83     /* Loop on all the entries in this descriptor.  */
84     while(descriptor_entries--)
85     {
86 
87         /* Get the length of that component.  */
88         switch(*descriptor_structure++)
89         {
90 
91         /* Check the size then build the component from the source and
92            insert it into the target descriptor.  */
93         case 4:
94 
95             /* Increase address so it's aligned.  */
96             while((ALIGN_TYPE)descriptor & 3u)
97                 descriptor++;
98 
99             /* Put the DW.  */
100             _ux_utility_long_put(raw_descriptor, *((ULONG *) descriptor));
101             raw_descriptor +=  4;
102             descriptor += 4;
103             break;
104 
105         case 2:
106 
107             /* Increase address so it's aligned.  */
108             while((ALIGN_TYPE)descriptor & 1u)
109                 descriptor++;
110 
111             /* Put the Word.  */
112             _ux_utility_short_put(raw_descriptor, (USHORT)*((USHORT *) descriptor));
113             raw_descriptor += 2;
114             descriptor += 2;
115             break;
116 
117         default:
118 
119             /* Put the byte.  */
120             *raw_descriptor =  (UCHAR) *((UCHAR *) descriptor);
121             raw_descriptor++;
122             descriptor++;
123         }
124     }
125 
126     /* Return to caller.  */
127     return;
128 }
129