usb.c 9.07 KB
Newer Older
swym's avatar
swym committed
1
/**
Rahix's avatar
Rahix committed
2
 *
swym's avatar
swym committed
3
 * Implementation of public epic_usb_ interface
Rahix's avatar
Rahix committed
4
 *
swym's avatar
swym committed
5
6
 * Configuration and FLASH-specific implementation of USB mass storage device
 * Configuration of USB CDCACM device
Rahix's avatar
Rahix committed
7
 *
swym's avatar
swym committed
8
 * USB descriptors for both are defined here.
Rahix's avatar
Rahix committed
9
 *
swym's avatar
swym committed
10
11
12
13
14
 */

#include "epicardium.h"

#include "modules/filesystem.h"
15
#include "modules/config.h"
swym's avatar
swym committed
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

#include "usb/cdcacm.h"
#include "usb/mass_storage.h"
#include "usb/descriptors.h"
#include "usb/epc_usb.h"

#include "modules/log.h"

#include "mx25lba.h"
#include "msc.h"

/* memory access callbacks for the mass storage (FLASH) device */
static int mscmem_init();
static uint32_t mscmem_size(void);
static int mscmem_read(uint32_t lba, uint8_t *buffer);
static int mscmem_write(uint32_t lba, uint8_t *buffer);
static int mscmem_start();
static int mscmem_stop();
static int mscmem_ready();

/* USB descriptors, definition at the end of this file */
static usb_device_descriptor_t device_descriptor_cdcacm
	__attribute__((aligned(4)));
static struct config_descriptor_cdcacm config_descriptor_cdcacm
	__attribute__((aligned(4)));
static usb_device_descriptor_t device_descriptor_msc
	__attribute__((aligned(4)));
static struct config_descriptor_msc config_descriptor_msc
	__attribute__((aligned(4)));

/* definitions of config structs */
static const msc_mem_t s_mscCallbacks = {
	mscmem_init, mscmem_start, mscmem_stop,  mscmem_ready,
	mscmem_size, mscmem_read,  mscmem_write,
};

static struct esb_device_descriptors s_dd_cdcacm = {
	.device = &device_descriptor_cdcacm, .cdcacm = &config_descriptor_cdcacm
};

static struct esb_device_descriptors s_dd_msc = {
	.device = &device_descriptor_msc, .msc = &config_descriptor_msc
};

static struct esb_config s_cfg_cdcacm = {
	.name        = "cdcacm",
	.init        = esb_cdc_init,
	.configure   = esb_cdc_configure,
	.deconfigure = esb_cdc_deconfigure,
	.deinit      = NULL,

	.descriptors = &s_dd_cdcacm,
	.deviceData  = NULL,
};

static struct esb_config s_cfg_msc = {
	.name        = "msc FLASH",
	.init        = esb_msc_init,
	.configure   = esb_msc_configure,
	.deconfigure = esb_msc_deconfigure,
	.deinit      = NULL,

	.descriptors = &s_dd_msc,
	.deviceData  = (void *)&s_mscCallbacks,
};

/* function implementations */

static int dirty = 0;
static int mscmem_init()
{
	LOG_DEBUG("msc", "mem.init");
	fatfs_detach();
	return mx25_init();
}

static uint32_t mscmem_size(void)
{
	return mx25_size();
}

static int mscmem_read(uint32_t lba, uint8_t *buffer)
{
	return mx25_read(lba, buffer);
}

static int mscmem_write(uint32_t lba, uint8_t *buffer)
{
	if (dirty == 0) {
		//bootloader_dirty();
	}
	dirty = 2;
	return mx25_write(lba, buffer);
}

static int mscmem_start()
{
	return mx25_start();
}

static int mscmem_stop()
{
	LOG_DEBUG("msc", "mem.stop");
	int ret = mx25_stop();
	fatfs_schedule_attach();
	return ret;
}

static int mscmem_ready()
{
	if (dirty) {
		dirty--;
		if (dirty == 0) {
			LOG_DEBUG("msc", "sync");
			mx25_sync();
			//bootloader_clean();
		}
	}
	return mx25_ready();
}

static bool s_fsDetached = false;
int epic_usb_shutdown(void)
{
	esb_deinit();
	if (s_fsDetached) {
		fatfs_attach();
143
		load_config();
swym's avatar
swym committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
	}
	return 0;
}

int epic_usb_storage(void)
{
	esb_deinit();
	s_fsDetached = true;
	return esb_init(&s_cfg_msc);
}

int epic_usb_cdcacm(void)
{
	esb_deinit();
	if (s_fsDetached) {
		fatfs_attach();
160
		load_config();
swym's avatar
swym committed
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
	}
	return esb_init(&s_cfg_cdcacm);
}

/* clang-format off */
static usb_device_descriptor_t device_descriptor_cdcacm = {
	.bLength            = 0x12,
	.bDescriptorType    = DT_DEVICE,
	.bcdUSB             = 0x0110,
	.bDeviceClass       = CLS_COMM,
	.bDeviceSubClass    = 0x00,
	.bDeviceProtocol    = 0x00,
	.bMaxPacketSize     = 0x40,
	.idVendor           = VNDR_MAXIM,
	.idProduct          = 0x003C,
	.bcdDevice          = 0x0100,
	.iManufacturer      = 0x01,
	.iProduct           = 0x02,
	.iSerialNumber      = 0x00,
	.bNumConfigurations = 0x01
};

static struct config_descriptor_cdcacm config_descriptor_cdcacm = {
    .config = {
        .bLength            = 0x09,
        .bDescriptorType    = DT_CONFIG,
        .wTotalLength       = 0x0043,     /*  sizeof(struct config_descriptor_cdcacm) */
        .bNumInterfaces     = 0x02,
        .bConfigurationValue= 0x01,
        .iConfiguration     = 0x00,
        .bmAttributes       = 0xE0,       /*  (self-powered, remote wakeup) */
        .bMaxPower          = 0x01,       /*  MaxPower is 2ma (units are 2ma/bit) */
    },
    .comm_interface = { /*  First Interface Descriptor For Comm Class Interface */
        .bLength            = 0x09,
        .bDescriptorType    = DT_INTERFACE,
        .bInterfaceNumber   = 0x00,
        .bAlternateSetting  = 0x00,
        .bNumEndpoints      = 0x01,
        .bInterfaceClass    = CLS_COMM,
        .bInterfaceSubClass = SCLS_ACM,
202
        .bInterfaceProtocol = 0x00,
swym's avatar
swym committed
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
        .iInterface         = 0x00,
    },
    .header_functional = {
        0x05,         /*  bFunctionalLength = 5           */
        DT_FUNCTIONAL,/*  bDescriptorType                 */
        0x00,         /*  bDescriptorSubtype              */
        0x10, 0x01,   /*  bcdCDC                          */
    },
    .call_management = {
        0x05,         /*  bFunctionalLength = 5           */
        DT_FUNCTIONAL,/*  bDescriptorType                 */
        0x01,         /*  bDescriptorSubtype              */
        0x03,         /*  bmCapabilities = Device handles call management itself (0x01), management over data class (0x02) */
        0x01,         /*  bmDataInterface                 */
    },
    .acm_functional = {
        0x04,         /*  bFunctionalLength = 4           */
        DT_FUNCTIONAL,/*  bDescriptorType                 */
        0x02,         /*  bDescriptorSubtype              */
        0x02,         /*  bmCapabilities                  */
    },
    .union_functional = {
        0x05,         /*  bFunctionalLength = 5           */
        DT_FUNCTIONAL,/*  bDescriptorType                 */
        0x06,         /*  bDescriptorSubtype              */
        0x00,         /*  bmMasterInterface               */
        0x01,         /*  bmSlaveInterface0               */
    },
    .endpoint_notify = {
        .bLength            = 0x07,
        .bDescriptorType    = DT_ENDPOINT,
        .bEndpointAddress   = 0x80 | ESB_ENDPOINT_CDCACM_NOTIFY,
        .bmAttributes       = ATTR_INTERRUPT,
        .wMaxPacketSize     = 0x0040,
        .bInterval          = 0xff,
    },
    .data_interface = {
        .bLength            = 0x09,
        .bDescriptorType    = DT_INTERFACE,
        .bInterfaceNumber   = 0x01,
        .bAlternateSetting  = 0x00,
        .bNumEndpoints      = 0x02,
        .bInterfaceClass    = CLS_DATA,
        .bInterfaceSubClass = 0x00,
        .bInterfaceProtocol = 0x00,
        .iInterface         = 0x00, //not 1?
    },
    .endpoint_out = {
        .bLength            = 0x07,
        .bDescriptorType    = DT_ENDPOINT,
        .bEndpointAddress   = ESB_ENDPOINT_CDCACM_OUT,
        .bmAttributes       = ATTR_BULK,
        .wMaxPacketSize     = 0x0040,
        .bInterval          = 0x00,
    },
    .endpoint_in = {
        .bLength            = 0x07,
        .bDescriptorType    = DT_ENDPOINT,
        .bEndpointAddress   = 0x80 | ESB_ENDPOINT_CDCACM_IN,
        .bmAttributes       = ATTR_BULK,
        .wMaxPacketSize     = 0x0040,
        .bInterval          = 0x00
    }
};

static usb_device_descriptor_t device_descriptor_msc = {
	.bLength            = 0x12,
	.bDescriptorType    = DT_DEVICE,
	.bcdUSB             = 0x0200,
	.bDeviceClass       = CLS_UNSPECIFIED,
	.bDeviceSubClass    = 0x00,
	.bDeviceProtocol    = 0x00,
	.bMaxPacketSize     = 0x40,
	.idVendor           = VNDR_MAXIM,
	.idProduct          = 0x4402,
	.bcdDevice          = 0x0100,
	.iManufacturer      = 0x01,
	.iProduct           = 0x02,
	.iSerialNumber      = 0x03,
	.bNumConfigurations = 0x01
};

static struct config_descriptor_msc config_descriptor_msc =
{
    .config = {
        .bLength            = 0x09,
        .bDescriptorType    = DT_CONFIG,
        .wTotalLength       = 0x0020,
        .bNumInterfaces     = 0x01,
        .bConfigurationValue= 0x01,
        .iConfiguration     = 0x00,
        .bmAttributes       = 0xC0,       /* (self-powered, no remote wakeup) */
        .bMaxPower          = 0x32,
    },
    .msc_interface = {
        .bLength                = 0x09,
        .bDescriptorType        = DT_INTERFACE,
        .bInterfaceNumber       = 0x00,
        .bAlternateSetting      = 0x00,
        .bNumEndpoints          = 0x02,
        .bInterfaceClass        = CLS_MASS_STOR,
        .bInterfaceSubClass     = SCLS_SCSI_CMDS,
        .bInterfaceProtocol     = PROT_BULK_TRANS,
        .iInterface             = 0x00,
    },
    .endpoint_out = {
        .bLength            = 0x07,
        .bDescriptorType    = DT_ENDPOINT,
        .bEndpointAddress   = ESB_ENDPOINT_MSC_OUT,
        .bmAttributes       = ATTR_BULK,
        .wMaxPacketSize     = 0x0040,
        .bInterval          = 0x00,
    },
    .endpoint_in = {
        .bLength            = 0x07,
        .bDescriptorType    = DT_ENDPOINT,
        .bEndpointAddress   = 0x80 | ESB_ENDPOINT_MSC_IN,
        .bmAttributes       = ATTR_BULK,
        .wMaxPacketSize     = 0x0040,
        .bInterval          = 0x00
    }
};
325
/* clang-format on */