1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "usbd_core.h"
7 #include "usbd_msc.h"
8 #include "bootuf2.h"
9 
10 #define MSC_IN_EP  0x81
11 #define MSC_OUT_EP 0x02
12 
13 #define USBD_VID           0xFFFF
14 #define USBD_PID           0xFFFF
15 #define USBD_MAX_POWER     100
16 #define USBD_LANGID_STRING 1033
17 
18 #define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN)
19 
20 #ifdef CONFIG_USB_HS
21 #define MSC_MAX_MPS 512
22 #else
23 #define MSC_MAX_MPS 64
24 #endif
25 
26 #ifdef CONFIG_USBDEV_ADVANCE_DESC
27 static const uint8_t device_descriptor[] = {
28     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01)
29 };
30 
31 static const uint8_t config_descriptor[] = {
32     USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
33     MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02)
34 };
35 
36 static const uint8_t device_quality_descriptor[] = {
37     ///////////////////////////////////////
38     /// device qualifier descriptor
39     ///////////////////////////////////////
40     0x0a,
41     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
42     0x00,
43     0x02,
44     0x00,
45     0x00,
46     0x00,
47     0x40,
48     0x00,
49     0x00,
50 };
51 
52 static const char *string_descriptors[] = {
53     (const char[]){ 0x09, 0x04 }, /* Langid */
54     "CherryUSB",                  /* Manufacturer */
55     "CherryUSB UF2 DEMO",         /* Product */
56     "2022123456",                 /* Serial Number */
57 };
58 
device_descriptor_callback(uint8_t speed)59 static const uint8_t *device_descriptor_callback(uint8_t speed)
60 {
61     return device_descriptor;
62 }
63 
config_descriptor_callback(uint8_t speed)64 static const uint8_t *config_descriptor_callback(uint8_t speed)
65 {
66     return config_descriptor;
67 }
68 
device_quality_descriptor_callback(uint8_t speed)69 static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
70 {
71     return device_quality_descriptor;
72 }
73 
string_descriptor_callback(uint8_t speed,uint8_t index)74 static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
75 {
76     if (index > 3) {
77         return NULL;
78     }
79     return string_descriptors[index];
80 }
81 
82 const struct usb_descriptor msc_bootuf2_descriptor = {
83     .device_descriptor_callback = device_descriptor_callback,
84     .config_descriptor_callback = config_descriptor_callback,
85     .device_quality_descriptor_callback = device_quality_descriptor_callback,
86     .string_descriptor_callback = string_descriptor_callback
87 };
88 #else
89 const uint8_t msc_bootuf2_descriptor[] = {
90     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
91     USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
92     MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02),
93     ///////////////////////////////////////
94     /// string0 descriptor
95     ///////////////////////////////////////
96     USB_LANGID_INIT(USBD_LANGID_STRING),
97     ///////////////////////////////////////
98     /// string1 descriptor
99     ///////////////////////////////////////
100     0x14,                       /* bLength */
101     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
102     'C', 0x00,                  /* wcChar0 */
103     'h', 0x00,                  /* wcChar1 */
104     'e', 0x00,                  /* wcChar2 */
105     'r', 0x00,                  /* wcChar3 */
106     'r', 0x00,                  /* wcChar4 */
107     'y', 0x00,                  /* wcChar5 */
108     'U', 0x00,                  /* wcChar6 */
109     'S', 0x00,                  /* wcChar7 */
110     'B', 0x00,                  /* wcChar8 */
111     ///////////////////////////////////////
112     /// string2 descriptor
113     ///////////////////////////////////////
114     0x26,                       /* bLength */
115     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
116     'C', 0x00,                  /* wcChar0 */
117     'h', 0x00,                  /* wcChar1 */
118     'e', 0x00,                  /* wcChar2 */
119     'r', 0x00,                  /* wcChar3 */
120     'r', 0x00,                  /* wcChar4 */
121     'y', 0x00,                  /* wcChar5 */
122     'U', 0x00,                  /* wcChar6 */
123     'S', 0x00,                  /* wcChar7 */
124     'B', 0x00,                  /* wcChar8 */
125     ' ', 0x00,                  /* wcChar9 */
126     'U', 0x00,                  /* wcChar10 */
127     'F', 0x00,                  /* wcChar11 */
128     '2', 0x00,                  /* wcChar12 */
129     ' ', 0x00,                  /* wcChar13 */
130     'D', 0x00,                  /* wcChar14 */
131     'E', 0x00,                  /* wcChar15 */
132     'M', 0x00,                  /* wcChar16 */
133     'O', 0x00,                  /* wcChar17 */
134     ///////////////////////////////////////
135     /// string3 descriptor
136     ///////////////////////////////////////
137     0x16,                       /* bLength */
138     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
139     '2', 0x00,                  /* wcChar0 */
140     '0', 0x00,                  /* wcChar1 */
141     '2', 0x00,                  /* wcChar2 */
142     '2', 0x00,                  /* wcChar3 */
143     '1', 0x00,                  /* wcChar4 */
144     '2', 0x00,                  /* wcChar5 */
145     '3', 0x00,                  /* wcChar6 */
146     '4', 0x00,                  /* wcChar7 */
147     '5', 0x00,                  /* wcChar8 */
148     '6', 0x00,                  /* wcChar9 */
149 #ifdef CONFIG_USB_HS
150     ///////////////////////////////////////
151     /// device qualifier descriptor
152     ///////////////////////////////////////
153     0x0a,
154     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
155     0x00,
156     0x02,
157     0x00,
158     0x00,
159     0x00,
160     0x40,
161     0x00,
162     0x00,
163 #endif
164     0x00
165 };
166 #endif
167 
usbd_event_handler(uint8_t busid,uint8_t event)168 static void usbd_event_handler(uint8_t busid, uint8_t event)
169 {
170     switch (event) {
171         case USBD_EVENT_RESET:
172             break;
173         case USBD_EVENT_CONNECTED:
174             break;
175         case USBD_EVENT_DISCONNECTED:
176             break;
177         case USBD_EVENT_RESUME:
178             break;
179         case USBD_EVENT_SUSPEND:
180             break;
181         case USBD_EVENT_CONFIGURED:
182             bootuf2_init();
183             break;
184         case USBD_EVENT_SET_REMOTE_WAKEUP:
185             break;
186         case USBD_EVENT_CLR_REMOTE_WAKEUP:
187             break;
188 
189         default:
190             break;
191     }
192 }
193 
usbd_msc_get_cap(uint8_t busid,uint8_t lun,uint32_t * block_num,uint32_t * block_size)194 void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
195 {
196     *block_num = bootuf2_get_sector_count();
197     *block_size = bootuf2_get_sector_size();
198 
199     USB_LOG_INFO("sector count:%d, sector size:%d\n", (unsigned int)*block_num, (unsigned int)*block_size);
200 }
usbd_msc_sector_read(uint8_t busid,uint8_t lun,uint32_t sector,uint8_t * buffer,uint32_t length)201 int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
202 {
203     boot2uf2_read_sector(sector, buffer, length / bootuf2_get_sector_size());
204     return 0;
205 }
206 
usbd_msc_sector_write(uint8_t busid,uint8_t lun,uint32_t sector,uint8_t * buffer,uint32_t length)207 int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
208 {
209     bootuf2_write_sector(sector, buffer, length / bootuf2_get_sector_size());
210     return 0;
211 }
212 
213 static struct usbd_interface intf0;
214 
msc_bootuf2_init(uint8_t busid,uintptr_t reg_base)215 void msc_bootuf2_init(uint8_t busid, uintptr_t reg_base)
216 {
217     boot2uf2_flash_init();
218 #ifdef CONFIG_USBDEV_ADVANCE_DESC
219     usbd_desc_register(busid, &msc_bootuf2_descriptor);
220 #else
221     usbd_desc_register(busid, msc_bootuf2_descriptor);
222 #endif
223     usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
224 
225     usbd_initialize(busid, reg_base, usbd_event_handler);
226 }
227 
boot2uf2_flash_init(void)228 void boot2uf2_flash_init(void)
229 {
230 }
231 
bootuf2_flash_write(uint32_t address,const uint8_t * data,size_t size)232 int bootuf2_flash_write(uint32_t address, const uint8_t *data, size_t size)
233 {
234     USB_LOG_INFO("address:%08x, size:%d\n", (unsigned int)address, (unsigned int)size);
235     return 0;
236 }