24
24
25
25
#define NUM_PACKETS_UNTIL_ABORT 2
26
26
#define NUM_PACKETS_AFTER_ABORT 8
27
+ #define EP_ABORT_BUFF_VALUE 0xff
27
28
28
- #define VENDOR_TEST_CTRL_IN 1
29
- #define VENDOR_TEST_CTRL_OUT 2
30
- #define VENDOR_TEST_CTRL_IN_SIZES 9
31
- #define VENDOR_TEST_CTRL_OUT_SIZES 10
32
- #define VENDOR_TEST_READ_START 11
29
+ /* If the host ever receives a payload with any byte set to this value,
30
+ * the device does not handle abort operation correctly. The buffer
31
+ * passed to aborted operation must not be used after call to abort().
32
+ */
33
+ #define FORBIDDEN_PAYLOAD_VALUE (NUM_PACKETS_AFTER_ABORT + 1 )
34
+
35
+ #define VENDOR_TEST_CTRL_IN 1
36
+ #define VENDOR_TEST_CTRL_OUT 2
37
+ #define VENDOR_TEST_CTRL_IN_SIZES 9
38
+ #define VENDOR_TEST_CTRL_OUT_SIZES 10
39
+ #define VENDOR_TEST_READ_START 11
40
+ #define VENDOR_TEST_ABORT_BUFF_CHECK 12
33
41
34
42
#define EVENT_READY (1 << 0 )
35
43
@@ -236,6 +244,12 @@ void USBEndpointTester::callback_request(const setup_packet_t *setup)
236
244
case VENDOR_TEST_READ_START:
237
245
result = (_request_read_start (setup)) ? Success : Failure;
238
246
break ;
247
+ case VENDOR_TEST_ABORT_BUFF_CHECK:
248
+ result = Send;
249
+ ctrl_buf[0 ] = _request_abort_buff_check (setup);
250
+ data = ctrl_buf;
251
+ size = 1 ;
252
+ break ;
239
253
default :
240
254
result = PassThrough;
241
255
break ;
@@ -273,20 +287,50 @@ bool USBEndpointTester::_request_read_start(const setup_packet_t *setup)
273
287
if (setup->bmRequestType .Recipient != ENDPOINT_RECIPIENT) {
274
288
return false ;
275
289
}
276
- size_t ep_index = NUM_ENDPOINTS + 1 ;
290
+ size_t ep_index = NUM_ENDPOINTS;
277
291
for (size_t i = 0 ; i < NUM_ENDPOINTS; i++) {
278
292
if (_endpoints[i] == setup->wIndex ) {
279
293
ep_index = i;
280
294
break ;
281
295
}
282
296
}
283
- if (ep_index > NUM_ENDPOINTS) {
297
+ if (ep_index == NUM_ENDPOINTS) {
298
+ return false ;
299
+ }
300
+ if (_endpoint_buffs[ep_index] == NULL ) {
284
301
return false ;
285
302
}
286
303
endpoint_abort (_endpoints[ep_index]);
287
304
return read_start (_endpoints[ep_index], _endpoint_buffs[ep_index], (*_endpoint_configs)[ep_index].max_packet );
288
305
}
289
306
307
+ bool USBEndpointTester::_request_abort_buff_check (const setup_packet_t *setup)
308
+ {
309
+ assert_locked ();
310
+ if (setup->bmRequestType .Recipient != ENDPOINT_RECIPIENT) {
311
+ return false ;
312
+ }
313
+ size_t ep_index = NUM_ENDPOINTS;
314
+ for (size_t i = 0 ; i < NUM_ENDPOINTS; i++) {
315
+ if (_endpoints[i] == setup->wIndex ) {
316
+ ep_index = i;
317
+ break ;
318
+ }
319
+ }
320
+ if (ep_index == NUM_ENDPOINTS) {
321
+ return false ;
322
+ }
323
+ if (_endpoint_buffs[ep_index] == NULL ) {
324
+ return false ;
325
+ }
326
+ for (size_t i = 0 ; i < (*_endpoint_configs)[ep_index].max_packet ; i++) {
327
+ if (_endpoint_buffs[ep_index][i] != EP_ABORT_BUFF_VALUE) {
328
+ return false ;
329
+ }
330
+ }
331
+ return true ;
332
+ }
333
+
290
334
void USBEndpointTester::callback_request_xfer_done (const setup_packet_t *setup, bool aborted)
291
335
{
292
336
if (aborted) {
@@ -309,6 +353,9 @@ void USBEndpointTester::callback_request_xfer_done(const setup_packet_t *setup,
309
353
case VENDOR_TEST_CTRL_IN_SIZES:
310
354
result = true ;
311
355
break ;
356
+ case VENDOR_TEST_ABORT_BUFF_CHECK:
357
+ result = true ;
358
+ break ;
312
359
default :
313
360
result = false ;
314
361
break ;
@@ -723,7 +770,6 @@ void USBEndpointTester::_cb_bulk_out()
723
770
{
724
771
_cnt_cb_bulk_out++;
725
772
uint32_t rx_size = read_finish (_endpoints[EP_BULK_OUT]);
726
-
727
773
if (_abort_transfer_test == false ) {
728
774
// Send data back to host using the IN endpoint.
729
775
memset (_endpoint_buffs[EP_BULK_IN], 0 , (*_endpoint_configs)[EP_BULK_IN].max_packet );
@@ -732,6 +778,10 @@ void USBEndpointTester::_cb_bulk_out()
732
778
} else {
733
779
// Abort the transfer if enough data was received.
734
780
_num_packets_bulk_out_abort++;
781
+ if (_num_packets_bulk_out_abort == NUM_PACKETS_UNTIL_ABORT) {
782
+ // Set every byte of the buffer to a known value.
783
+ memset (_endpoint_buffs[EP_BULK_OUT], EP_ABORT_BUFF_VALUE, (*_endpoint_configs)[EP_BULK_OUT].max_packet );
784
+ }
735
785
read_start (_endpoints[EP_BULK_OUT], _endpoint_buffs[EP_BULK_OUT], (*_endpoint_configs)[EP_BULK_OUT].max_packet );
736
786
if (_num_packets_bulk_out_abort == NUM_PACKETS_UNTIL_ABORT) {
737
787
endpoint_abort (_endpoints[EP_BULK_OUT]);
@@ -743,7 +793,6 @@ void USBEndpointTester::_cb_bulk_in()
743
793
{
744
794
_cnt_cb_bulk_in++;
745
795
write_finish (_endpoints[EP_BULK_IN]);
746
-
747
796
if (_abort_transfer_test == false ) {
748
797
// Receive more data from the host using the OUT endpoint.
749
798
read_start (_endpoints[EP_BULK_OUT], _endpoint_buffs[EP_BULK_OUT], (*_endpoint_configs)[EP_BULK_OUT].max_packet );
@@ -757,6 +806,10 @@ void USBEndpointTester::_cb_bulk_in()
757
806
write_start (_endpoints[EP_BULK_IN], _endpoint_buffs[EP_BULK_IN], (*_endpoint_configs)[EP_BULK_IN].max_packet );
758
807
if (_num_packets_bulk_in_abort == NUM_PACKETS_UNTIL_ABORT) {
759
808
endpoint_abort (_endpoints[EP_BULK_IN]);
809
+ // Verify that buffer given in write_start is not used after the
810
+ // call to endpoint_abort(), by changing the buffer contents.
811
+ // The test will fail if the host receives new buffer content.
812
+ memset (_endpoint_buffs[EP_BULK_IN], FORBIDDEN_PAYLOAD_VALUE, (*_endpoint_configs)[EP_BULK_IN].max_packet );
760
813
}
761
814
}
762
815
}
@@ -773,6 +826,10 @@ void USBEndpointTester::_cb_int_out()
773
826
} else {
774
827
// Abort the transfer if enough data was received.
775
828
_num_packets_int_out_abort++;
829
+ if (_num_packets_int_out_abort == NUM_PACKETS_UNTIL_ABORT) {
830
+ // Set every byte of the buffer to a known value.
831
+ memset (_endpoint_buffs[EP_INT_OUT], EP_ABORT_BUFF_VALUE, (*_endpoint_configs)[EP_INT_OUT].max_packet );
832
+ }
776
833
read_start (_endpoints[EP_INT_OUT], _endpoint_buffs[EP_INT_OUT], (*_endpoint_configs)[EP_INT_OUT].max_packet );
777
834
if (_num_packets_int_out_abort == NUM_PACKETS_UNTIL_ABORT) {
778
835
endpoint_abort (_endpoints[EP_INT_OUT]);
@@ -797,6 +854,10 @@ void USBEndpointTester::_cb_int_in()
797
854
write_start (_endpoints[EP_INT_IN], _endpoint_buffs[EP_INT_IN], (*_endpoint_configs)[EP_INT_IN].max_packet );
798
855
if (_num_packets_int_in_abort == NUM_PACKETS_UNTIL_ABORT) {
799
856
endpoint_abort (_endpoints[EP_INT_IN]);
857
+ // Verify that buffer given in write_start is not used after the
858
+ // call to endpoint_abort(), by changing the buffer contents.
859
+ // The test will fail if the host receives new buffer content.
860
+ memset (_endpoint_buffs[EP_INT_IN], FORBIDDEN_PAYLOAD_VALUE, (*_endpoint_configs)[EP_INT_IN].max_packet );
800
861
}
801
862
}
802
863
}
0 commit comments