-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathUSBMSD.cpp
935 lines (785 loc) · 23.8 KB
/
USBMSD.cpp
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
/*
* Copyright (c) 2018-2019, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Arduino.h"
#include <stdint.h>
#include <stdlib.h>
#include "PluggableUSBMSD.h"
#include "EndpointResolver.h"
#include "usb_phy_api.h"
using namespace arduino;
#define DISK_OK 0x00
#define NO_INIT 0x01
#define NO_DISK 0x02
#define WRITE_PROTECT 0x04
#define CBW_Signature 0x43425355
#define CSW_Signature 0x53425355
// SCSI Commands
#define TEST_UNIT_READY 0x00
#define REQUEST_SENSE 0x03
#define FORMAT_UNIT 0x04
#define INQUIRY 0x12
#define MODE_SELECT6 0x15
#define MODE_SENSE6 0x1A
#define START_STOP_UNIT 0x1B
#define MEDIA_REMOVAL 0x1E
#define READ_FORMAT_CAPACITIES 0x23
#define READ_CAPACITY 0x25
#define READ10 0x28
#define WRITE10 0x2A
#define VERIFY10 0x2F
#define READ12 0xA8
#define WRITE12 0xAA
#define MODE_SELECT10 0x55
#define MODE_SENSE10 0x5A
// MSC class specific requests
#define MSC_REQUEST_RESET 0xFF
#define MSC_REQUEST_GET_MAX_LUN 0xFE
#define DEFAULT_CONFIGURATION (1)
// max packet size
#define MAX_PACKET MSD_MAX_PACKET_SIZE
// CSW Status
enum Status {
CSW_PASSED,
CSW_FAILED,
CSW_ERROR,
};
USBMSD::USBMSD(mbed::BlockDevice *bd, bool connect_blocking, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
: arduino::internal::PluggableUSBModule(1), _in_task(&_queue), _out_task(&_queue),
_initialized(false), _media_removed(false), _bd(bd)
{
PluggableUSBD().plug(this);
}
USBMSD::USBMSD(USBPhy *phy, mbed::BlockDevice *bd, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
: arduino::internal::PluggableUSBModule(1), _in_task(&_queue), _out_task(&_queue),
_initialized(false), _media_removed(false), _bd(bd)
{
PluggableUSBD().plug(this);
}
static rtos::Thread _t(osPriorityNormal, 32 * 1024, NULL, "msd");
void USBMSD::init(EndpointResolver& resolver)
{
_bd->init();
_in_task = mbed::callback(this, &USBMSD::_in);
_out_task = mbed::callback(this, &USBMSD::_out);
_reset_task = mbed::callback(this, &USBMSD::_reset);
_configure_task = mbed::callback(this, &USBMSD::_configure);
_bulk_in = resolver.endpoint_in(USB_EP_TYPE_BULK, MAX_PACKET);
_bulk_out = resolver.endpoint_out(USB_EP_TYPE_BULK, MAX_PACKET);
MBED_ASSERT(resolver.valid());
_stage = READ_CBW;
memset((void *)&_cbw, 0, sizeof(CBW));
memset((void *)&_csw, 0, sizeof(CSW));
_page = NULL;
connect();
_t.start(mbed::callback(this, &USBMSD::process));
}
USBMSD::~USBMSD()
{
disconnect();
_bd->deinit();
//deinit();
}
bool USBMSD::connect()
{
//_mutex_init.lock();
//_mutex.lock();
// already initialized
if (_initialized) {
//_mutex.unlock();
//_mutex_init.unlock();
return false;
}
//disk initialization
if (disk_status() & NO_INIT) {
if (disk_initialize()) {
//_mutex.unlock();
//_mutex_init.unlock();
return false;
}
}
// get number of blocks
_block_count = disk_sectors();
// get memory size
_memory_size = disk_size();
if (_block_count > 0) {
_block_size = _memory_size / _block_count;
if (_block_size != 0) {
free(_page);
_page = (uint8_t *)malloc(_block_size * sizeof(uint8_t));
if (_page == NULL) {
//_mutex.unlock();
//_mutex_init.unlock();
return false;
}
}
} else {
//_mutex.unlock();
//_mutex_init.unlock();
return false;
}
//connect the device
//USBDevice::connect();
_initialized = true;
_media_removed = false;
//_mutex.unlock();
//_mutex_init.unlock();
return true;
}
void USBMSD::disconnect()
{
//_mutex_init.lock();
//_mutex.lock();
//USBDevice::disconnect();
_initialized = false;
//De-allocate MSD page size:
free(_page);
_page = NULL;
//_mutex.unlock();
//_mutex_init.unlock();
}
void USBMSD::process()
{
while (1) {
if (_initialized) {
_data_available.wait_any(0xFF, 10);
_queue.dispatch();
//yield();
}
}
}
void USBMSD::attach(mbed::Callback<void()> cb)
{
}
bool USBMSD::media_removed()
{
return _media_removed;
}
int USBMSD::disk_read(uint8_t *data, uint64_t block, uint8_t count)
{
// this operation must be executed in another thread
mbed::bd_addr_t addr = block * _bd->get_erase_size();
mbed::bd_size_t size = count * _bd->get_erase_size();
#ifdef NRF52840_XXAA
memcpy(data, (void*)(addr + 0x80000), size);
#else
_bd->read(data, addr, size);
#endif
return 0;
}
int USBMSD::disk_write(const uint8_t *data, uint64_t block, uint8_t count)
{
// this operation must be executed in another thread
mbed::bd_addr_t addr = block * _bd->get_erase_size();
mbed::bd_size_t size = count * _bd->get_erase_size();
int ret = _bd->erase(addr, size);
if (ret != 0) {
return ret;
}
return _bd->program(data, addr, size);
}
int USBMSD::disk_initialize()
{
return 0;
}
uint64_t USBMSD::disk_sectors()
{
return _bd->size() / _bd->get_erase_size();
}
uint64_t USBMSD::disk_size()
{
return _bd->size();
}
int USBMSD::disk_status()
{
return 0;
}
void USBMSD::_isr_out()
{
_data_available.set(1);
_out_task.call();
}
void USBMSD::_isr_in()
{
_data_available.set(1);
_in_task.call();
}
void USBMSD::callback_state_change(USBDevice::DeviceState new_state)
{
// called in ISR context
if (new_state != USBDevice::Configured) {
_reset();
}
}
uint32_t USBMSD::callback_request(const USBDevice::setup_packet_t *setup, USBDevice::RequestResult *result, uint8_t** data)
{
*result = USBDevice::PassThrough;
// called in ISR context
if (setup->wIndex != pluggedInterface) {
return 0;
}
if (setup->bmRequestType.Type == CLASS_TYPE) {
return _control(setup, result, data);
}
return 0;
}
bool USBMSD::callback_request_xfer_done(const USBDevice::setup_packet_t *setup, bool aborted)
{
// called in ISR context
if (setup->wIndex != pluggedInterface) {
return false;
}
bool success = setup->bRequest == MSC_REQUEST_GET_MAX_LUN;
return success;
}
bool USBMSD::callback_set_configuration(uint8_t configuration)
{
// called in ISR context
_configure();
return true;
}
void USBMSD::callback_set_interface(uint16_t interface, uint8_t alternate)
{
// called in ISR context
//bool success = (interface == 0) && (alternate == 0);
//complete_set_interface(success);
}
uint8_t USBMSD::getProductVersion()
{
return 8;
}
const uint8_t *USBMSD::string_iinterface_desc()
{
static const uint8_t string_iinterface_descriptor[] = {
0x08, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03
'M', 0, 'S', 0, 'D', 0 //bString iInterface - MSD
};
return string_iinterface_descriptor;
}
const uint8_t *USBMSD::string_iproduct_desc()
{
static const uint8_t string_iproduct_descriptor[] = {
0x12, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03
'M', 0, 'b', 0, 'e', 0, 'd', 0, ' ', 0, 'M', 0, 'S', 0, 'D', 0 //bString iProduct - Mbed Audio
};
return string_iproduct_descriptor;
}
const uint8_t *USBMSD::configuration_desc(uint8_t index)
{
uint8_t config_descriptor_temp[] = {
// Configuration 1
9, // bLength
2, // bDescriptorType
LSB(9 + 9 + 7 + 7), // wTotalLength
MSB(9 + 9 + 7 + 7),
0x01, // bNumInterfaces
0x01, // bConfigurationValue: 0x01 is used to select this configuration
0x00, // iConfiguration: no string to describe this configuration
0xC0, // bmAttributes
100, // bMaxPower, device power consumption is 100 mA
// Interface 0, Alternate Setting 0, MSC Class
9, // bLength
4, // bDescriptorType
pluggedInterface, // bInterfaceNumber
0x00, // bAlternateSetting
0x02, // bNumEndpoints
0x08, // bInterfaceClass
0x06, // bInterfaceSubClass
0x50, // bInterfaceProtocol
0x04, // iInterface
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
7, // bLength
5, // bDescriptorType
_bulk_in, // bEndpointAddress
0x02, // bmAttributes (0x02=bulk)
LSB(MAX_PACKET), // wMaxPacketSize (LSB)
MSB(MAX_PACKET), // wMaxPacketSize (MSB)
0, // bInterval
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
7, // bLength
5, // bDescriptorType
_bulk_out, // bEndpointAddress
0x02, // bmAttributes (0x02=bulk)
LSB(MAX_PACKET), // wMaxPacketSize (LSB)
MSB(MAX_PACKET), // wMaxPacketSize (MSB)
0 // bInterval
};
MBED_ASSERT(sizeof(config_descriptor_temp) == sizeof(_configuration_descriptor));
memcpy(_configuration_descriptor, config_descriptor_temp, sizeof(_configuration_descriptor));
return _configuration_descriptor;
}
void USBMSD::_out()
{
_mutex.lock();
_bulk_out_size = read_finish(_bulk_out);
_out_ready = true;
_process();
_mutex.unlock();
}
void USBMSD::_in()
{
_mutex.lock();
write_finish(_bulk_in);
_in_ready = true;
_process();
_mutex.unlock();
}
void USBMSD::_reset()
{
//_mutex.lock();
msd_reset();
//_mutex.unlock();
}
uint32_t USBMSD::_control(const USBDevice::setup_packet_t *setup, USBDevice::RequestResult *result, uint8_t** data)
{
////_mutex.lock();
static const uint8_t maxLUN[1] = {0};
*result = USBDevice::PassThrough;
uint32_t size = 0;
if (setup->bmRequestType.Type == CLASS_TYPE) {
switch (setup->bRequest) {
case MSC_REQUEST_RESET:
*result = USBDevice::Success;
msd_reset();
break;
case MSC_REQUEST_GET_MAX_LUN:
*result = USBDevice::Send;
*data = (uint8_t *)maxLUN;
size = 1;
break;
default:
break;
}
}
////_mutex.unlock();
return size;
}
void USBMSD::_configure()
{
//_mutex.lock();
// Configure endpoints > 0
PluggableUSBD().endpoint_add(_bulk_in, MAX_PACKET, USB_EP_TYPE_BULK, mbed::callback(this, &USBMSD::_isr_in));
PluggableUSBD().endpoint_add(_bulk_out, MAX_PACKET, USB_EP_TYPE_BULK, mbed::callback(this, &USBMSD::_isr_out));
MBED_ASSERT(sizeof(_bulk_out_buf) == MAX_PACKET);
MBED_ASSERT(sizeof(_bulk_in_buf) == MAX_PACKET);
_out_ready = false;
_in_ready = true;
//activate readings
read_start(_bulk_out, _bulk_out_buf, sizeof(_bulk_out_buf));
//_mutex.unlock();
}
void USBMSD::_process()
{
// //_mutex must be locked by caller
switch (_stage) {
// the device has to decode the CBW received
case READ_CBW:
if (!_out_ready) {
break;
}
CBWDecode(_bulk_out_buf, _bulk_out_size);
_read_next();
break;
case PROCESS_CBW:
switch (_cbw.CB[0]) {
// the device has to receive data from the host
case WRITE10:
case WRITE12:
if (!_out_ready) {
break;
}
memoryWrite(_bulk_out_buf, _bulk_out_size);
_read_next();
break;
case VERIFY10:
if (!_out_ready) {
break;
}
memoryVerify(_bulk_out_buf, _bulk_out_size);
_read_next();
break;
// the device has to send data to the host
case READ10:
case READ12:
if (!_in_ready) {
break;
}
memoryRead();
break;
}
break;
//the device has to send a CSW
case SEND_CSW:
if (!_in_ready) {
break;
}
sendCSW();
break;
// an error has occurred: stall endpoint and send CSW
default:
endpoint_stall(_bulk_out);
endpoint_stall(_bulk_in);
_csw.Status = CSW_ERROR;
sendCSW();
break;
}
}
void USBMSD::_write_next(uint8_t *data, uint32_t size)
{
lock();
MBED_ASSERT(size <= MAX_PACKET);
MBED_ASSERT(_in_ready);
uint32_t send_size = MAX_PACKET > size ? size : MAX_PACKET;
memcpy(_bulk_in_buf, data, send_size);
write_start(_bulk_in, _bulk_in_buf, send_size);
_in_ready = false;
unlock();
}
void USBMSD::_read_next()
{
lock();
MBED_ASSERT(_out_ready);
read_start(_bulk_out, _bulk_out_buf, sizeof(_bulk_out_buf));
_out_ready = false;
unlock();
}
void USBMSD::memoryWrite(uint8_t *buf, uint16_t size)
{
if ((_addr + size) > _memory_size) {
size = _memory_size - _addr;
_stage = ERROR;
endpoint_stall(_bulk_out);
}
// we fill an array in RAM of 1 block before writing it in memory
for (int i = 0; i < size; i++) {
_page[_addr % _block_size + i] = buf[i];
}
// if the array is filled, write it in memory
if (!((_addr + size) % _block_size)) {
if (!(disk_status() & WRITE_PROTECT)) {
disk_write(_page, _addr / _block_size, 1);
}
}
_addr += size;
_length -= size;
_csw.DataResidue -= size;
if ((!_length) || (_stage != PROCESS_CBW)) {
_csw.Status = (_stage == ERROR) ? CSW_FAILED : CSW_PASSED;
sendCSW();
}
}
void USBMSD::memoryVerify(uint8_t *buf, uint16_t size)
{
uint32_t n;
if ((_addr + size) > _memory_size) {
size = _memory_size - _addr;
_stage = ERROR;
endpoint_stall(_bulk_out);
}
// beginning of a new block -> load a whole block in RAM
if (!(_addr % _block_size)) {
disk_read(_page, _addr / _block_size, 1);
}
// info are in RAM -> no need to re-read memory
for (n = 0; n < size; n++) {
if (_page[_addr % _block_size + n] != buf[n]) {
_mem_ok = false;
break;
}
}
_addr += size;
_length -= size;
_csw.DataResidue -= size;
if (!_length || (_stage != PROCESS_CBW)) {
_csw.Status = (_mem_ok && (_stage == PROCESS_CBW)) ? CSW_PASSED : CSW_FAILED;
sendCSW();
}
}
bool USBMSD::inquiryRequest(void)
{
uint8_t inquiry[] = { 0x00, 0x80, 0x00, 0x01,
36 - 4, 0x80, 0x00, 0x00,
'M', 'B', 'E', 'D', '.', 'O', 'R', 'G',
'M', 'B', 'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 'K', ' ', ' ', ' ',
'1', '.', '0', ' ',
};
if (!write(inquiry, sizeof(inquiry))) {
return false;
}
return true;
}
bool USBMSD::readFormatCapacity()
{
uint8_t capacity[] = { 0x00, 0x00, 0x00, 0x08,
(uint8_t)((_block_count >> 24) & 0xff),
(uint8_t)((_block_count >> 16) & 0xff),
(uint8_t)((_block_count >> 8) & 0xff),
(uint8_t)((_block_count >> 0) & 0xff),
0x02,
(uint8_t)((_block_size >> 16) & 0xff),
(uint8_t)((_block_size >> 8) & 0xff),
(uint8_t)((_block_size >> 0) & 0xff),
};
if (!write(capacity, sizeof(capacity))) {
return false;
}
return true;
}
bool USBMSD::readCapacity(void)
{
uint8_t capacity[] = {
(uint8_t)(((_block_count - 1) >> 24) & 0xff),
(uint8_t)(((_block_count - 1) >> 16) & 0xff),
(uint8_t)(((_block_count - 1) >> 8) & 0xff),
(uint8_t)(((_block_count - 1) >> 0) & 0xff),
(uint8_t)((_block_size >> 24) & 0xff),
(uint8_t)((_block_size >> 16) & 0xff),
(uint8_t)((_block_size >> 8) & 0xff),
(uint8_t)((_block_size >> 0) & 0xff),
};
if (!write(capacity, sizeof(capacity))) {
return false;
}
return true;
}
bool USBMSD::write(uint8_t *buf, uint16_t size)
{
if (size >= _cbw.DataLength) {
size = _cbw.DataLength;
}
_stage = SEND_CSW;
_write_next(buf, size);
_csw.DataResidue -= size;
_csw.Status = CSW_PASSED;
return true;
}
bool USBMSD::modeSense6(void)
{
uint8_t sense6[] = { 0x03, 0x00, 0x00, 0x00 };
if (!write(sense6, sizeof(sense6))) {
return false;
}
return true;
}
bool USBMSD::modeSense10(void)
{
uint8_t sense10[] = { 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
if (!write(sense10, sizeof(sense10))) {
return false;
}
return true;
}
void USBMSD::sendCSW()
{
_csw.Signature = CSW_Signature;
_write_next((uint8_t *)&_csw, sizeof(CSW));
_stage = READ_CBW;
}
bool USBMSD::requestSense(void)
{
uint8_t request_sense[] = {
0x70,
0x00,
0x05, // Sense Key: illegal request
0x00,
0x00,
0x00,
0x00,
0x0A,
0x00,
0x00,
0x00,
0x00,
0x30,
0x01,
0x00,
0x00,
0x00,
0x00,
};
if (!write(request_sense, sizeof(request_sense))) {
return false;
}
return true;
}
void USBMSD::fail()
{
_csw.Status = CSW_FAILED;
sendCSW();
}
void USBMSD::CBWDecode(uint8_t *buf, uint16_t size)
{
if (size == sizeof(_cbw)) {
memcpy((uint8_t *)&_cbw, buf, size);
if (_cbw.Signature == CBW_Signature) {
_csw.Tag = _cbw.Tag;
_csw.DataResidue = _cbw.DataLength;
if ((_cbw.CBLength < 1) || (_cbw.CBLength > 16)) {
fail();
} else {
switch (_cbw.CB[0]) {
case TEST_UNIT_READY:
testUnitReady();
break;
case REQUEST_SENSE:
requestSense();
break;
case INQUIRY:
inquiryRequest();
break;
case MODE_SENSE6:
modeSense6();
break;
case READ_FORMAT_CAPACITIES:
readFormatCapacity();
break;
case READ_CAPACITY:
readCapacity();
break;
case READ10:
case READ12:
if (infoTransfer()) {
if ((_cbw.Flags & 0x80)) {
_stage = PROCESS_CBW;
memoryRead();
} else {
endpoint_stall(_bulk_out);
_csw.Status = CSW_ERROR;
sendCSW();
}
}
break;
case WRITE10:
case WRITE12:
if (infoTransfer()) {
if (!(_cbw.Flags & 0x80)) {
_stage = PROCESS_CBW;
} else {
endpoint_stall(_bulk_in);
_csw.Status = CSW_ERROR;
sendCSW();
}
}
break;
case VERIFY10:
if (!(_cbw.CB[1] & 0x02)) {
_csw.Status = CSW_PASSED;
sendCSW();
break;
}
if (infoTransfer()) {
if (!(_cbw.Flags & 0x80)) {
_stage = PROCESS_CBW;
_mem_ok = true;
} else {
endpoint_stall(_bulk_in);
_csw.Status = CSW_ERROR;
sendCSW();
}
}
break;
case MEDIA_REMOVAL:
_csw.Status = CSW_PASSED;
sendCSW();
_media_removed = true;
break;
case MODE_SENSE10:
modeSense10();
break;
default:
fail();
break;
}
}
}
}
}
void USBMSD::testUnitReady(void)
{
if (_cbw.DataLength != 0) {
if ((_cbw.Flags & 0x80) != 0) {
endpoint_stall(_bulk_in);
} else {
endpoint_stall(_bulk_out);
}
}
_csw.Status = CSW_PASSED;
sendCSW();
}
void USBMSD::memoryRead(void)
{
uint32_t n;
n = (_length > MAX_PACKET) ? MAX_PACKET : _length;
if ((_addr + n) > _memory_size) {
n = _memory_size - _addr;
_stage = ERROR;
}
// we read an entire block
if (!(_addr % _block_size)) {
disk_read(_page, _addr / _block_size, 1);
}
// write data which are in RAM
_write_next(&_page[_addr % _block_size], MAX_PACKET);
_addr += n;
_length -= n;
_csw.DataResidue -= n;
if (!_length || (_stage != PROCESS_CBW)) {
_csw.Status = (_stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;
_stage = (_stage == PROCESS_CBW) ? SEND_CSW : _stage;
}
}
bool USBMSD::infoTransfer(void)
{
uint32_t n;
// Logical Block Address of First Block
n = (_cbw.CB[2] << 24) | (_cbw.CB[3] << 16) | (_cbw.CB[4] << 8) | (_cbw.CB[5] << 0);
_addr = n * _block_size;
// Number of Blocks to transfer
switch (_cbw.CB[0]) {
case READ10:
case WRITE10:
case VERIFY10:
n = (_cbw.CB[7] << 8) | (_cbw.CB[8] << 0);
break;
case READ12:
case WRITE12:
n = (_cbw.CB[6] << 24) | (_cbw.CB[7] << 16) | (_cbw.CB[8] << 8) | (_cbw.CB[9] << 0);
break;
}
_length = n * _block_size;
if (!_cbw.DataLength) { // host requests no data
_csw.Status = CSW_FAILED;
sendCSW();
return false;
}
if (_cbw.DataLength != _length) {
if ((_cbw.Flags & 0x80) != 0) {
endpoint_stall(_bulk_in);
} else {
endpoint_stall(_bulk_out);
}
_csw.Status = CSW_FAILED;
sendCSW();
return false;
}
return true;
}
void USBMSD::msd_reset()
{
_stage = READ_CBW;
}