1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 #include "esp32s2/rom/usb/usb_common.h"
17 #include "soc/soc.h"
18 #include "soc/efuse_reg.h"
19 
20 /* USB CDC descriptor.
21  * Note that we aren't using the one in ROM since it doesn't
22  * set "serial" to the MAC address.
23  * However overriding the descriptor is cheap - we can reuse most
24  * of its components from ROM.
25  */
26 
27 /* This is not const, since the MAC address is only known at run time */
28 static struct string_descriptor s_str_serial_descr = {
29     .bLength = 2 + 2 * 17,
30     .bDescriptorType = 0x03,
31     .bString={'0', '0', ':', '0', '0', ':', '0', '0', ':', '0', '0', ':', '0', '0', ':', '0', '0'}
32 };
33 
34 static const struct rom_usb_descriptors s_acm_usb_descriptors_override = {
35     .device_descr = &general_device_descr,
36     .config_descr = { &acm_config_descr },
37     .string_count = 4,
38     .string0_descr = &string0_descr,
39     .string_descrs = {
40         &str_manu_descr,
41         &str_prod_descr,
42         &s_str_serial_descr
43     }
44 };
45 
nibble_to_hex_u16(uint8_t b)46 static inline uint16_t nibble_to_hex_u16(uint8_t b)
47 {
48     if (b < 0xa) {
49         return '0' + b;
50     } else {
51         return 'a' + b - 0xa;
52     }
53 }
54 
rom_usb_cdc_set_descriptor_patch(void)55 void rom_usb_cdc_set_descriptor_patch(void)
56 {
57     /* Get the MAC address */
58     const uint32_t mac0 = REG_GET_FIELD(EFUSE_RD_MAC_SPI_SYS_0_REG, EFUSE_MAC_0);
59     const uint32_t mac1 = REG_GET_FIELD(EFUSE_RD_MAC_SPI_SYS_1_REG, EFUSE_MAC_1);
60     uint8_t mac_bytes[6];
61     memcpy(mac_bytes, &mac0, 4);
62     memcpy(mac_bytes + 4, &mac1, 2);
63 
64     /* Convert to UTF16 string */
65     for (int i = 0; i < 6; ++i) {
66         uint8_t b = mac_bytes[5 - i]; /* printing from the MSB */
67 		uint16_t nb_h = nibble_to_hex_u16(b >> 4);
68 		uint16_t nb_l = nibble_to_hex_u16(b & 0xf);
69 		s_str_serial_descr.bString[i] = (nb_h << 8) | nb_l;
70     }
71 
72     /* Override the pointer to descriptors structure */
73     rom_usb_curr_desc = &s_acm_usb_descriptors_override;
74 }
75