| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Petr Manek
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup drvusbdiag
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Code for executing diagnostic tests.
|
|---|
| 35 | */
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 | #include <usbdiag_iface.h>
|
|---|
| 40 | #include <time.h>
|
|---|
| 41 | #include "device.h"
|
|---|
| 42 | #include "tests.h"
|
|---|
| 43 |
|
|---|
| 44 | #define NAME "usbdiag"
|
|---|
| 45 |
|
|---|
| 46 | static int burst_in_test(usb_pipe_t *pipe, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 47 | {
|
|---|
| 48 | if (!pipe)
|
|---|
| 49 | return EBADMEM;
|
|---|
| 50 |
|
|---|
| 51 | char *buffer = (char *) malloc(size);
|
|---|
| 52 | if (!buffer)
|
|---|
| 53 | return ENOMEM;
|
|---|
| 54 |
|
|---|
| 55 | // TODO: Are we sure that no other test is running on this endpoint?
|
|---|
| 56 |
|
|---|
| 57 | usb_log_info("Performing %s IN burst test.", usb_str_transfer_type(pipe->desc.transfer_type));
|
|---|
| 58 |
|
|---|
| 59 | int rc = EOK;
|
|---|
| 60 | struct timeval start_time;
|
|---|
| 61 | gettimeofday(&start_time, NULL);
|
|---|
| 62 |
|
|---|
| 63 | for (int i = 0; i < cycles; ++i) {
|
|---|
| 64 | // Read device's response.
|
|---|
| 65 | size_t remaining = size;
|
|---|
| 66 | size_t transferred;
|
|---|
| 67 |
|
|---|
| 68 | while (remaining > 0) {
|
|---|
| 69 | if ((rc = usb_pipe_read(pipe, buffer + size - remaining, remaining, &transferred))) {
|
|---|
| 70 | usb_log_error("Read of %s IN endpoint failed with error: %s\n", usb_str_transfer_type(pipe->desc.transfer_type), str_error(rc));
|
|---|
| 71 | break;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (transferred > remaining) {
|
|---|
| 75 | usb_log_error("Read of %s IN endpoint returned more data than expected.\n", usb_str_transfer_type(pipe->desc.transfer_type));
|
|---|
| 76 | rc = EINVAL;
|
|---|
| 77 | break;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | remaining -= transferred;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | if (rc)
|
|---|
| 84 | break;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | struct timeval final_time;
|
|---|
| 88 | gettimeofday(&final_time, NULL);
|
|---|
| 89 | usbdiag_dur_t in_duration = ((final_time.tv_usec - start_time.tv_usec) / 1000) +
|
|---|
| 90 | ((final_time.tv_sec - start_time.tv_sec) * 1000);
|
|---|
| 91 |
|
|---|
| 92 | usb_log_info("Burst test on %s IN endpoint completed in %lu ms.", usb_str_transfer_type(pipe->desc.transfer_type), in_duration);
|
|---|
| 93 |
|
|---|
| 94 | free(buffer);
|
|---|
| 95 | if (duration)
|
|---|
| 96 | *duration = in_duration;
|
|---|
| 97 |
|
|---|
| 98 | return rc;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static int burst_out_test(usb_pipe_t *pipe, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 102 | {
|
|---|
| 103 | if (!pipe)
|
|---|
| 104 | return EBADMEM;
|
|---|
| 105 |
|
|---|
| 106 | char *buffer = (char *) malloc(size);
|
|---|
| 107 | if (!buffer)
|
|---|
| 108 | return ENOMEM;
|
|---|
| 109 |
|
|---|
| 110 | memset(buffer, 42, size);
|
|---|
| 111 |
|
|---|
| 112 | // TODO: Are we sure that no other test is running on this endpoint?
|
|---|
| 113 |
|
|---|
| 114 | usb_log_info("Performing %s OUT burst test.", usb_str_transfer_type(pipe->desc.transfer_type));
|
|---|
| 115 |
|
|---|
| 116 | int rc = EOK;
|
|---|
| 117 | struct timeval start_time;
|
|---|
| 118 | gettimeofday(&start_time, NULL);
|
|---|
| 119 |
|
|---|
| 120 | for (int i = 0; i < cycles; ++i) {
|
|---|
| 121 | // Write buffer to device.
|
|---|
| 122 | if ((rc = usb_pipe_write(pipe, buffer, size))) {
|
|---|
| 123 | usb_log_error("Write to %s OUT endpoint failed with error: %s\n", usb_str_transfer_type(pipe->desc.transfer_type), str_error(rc));
|
|---|
| 124 | break;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | struct timeval final_time;
|
|---|
| 129 | gettimeofday(&final_time, NULL);
|
|---|
| 130 | usbdiag_dur_t in_duration = ((final_time.tv_usec - start_time.tv_usec) / 1000) +
|
|---|
| 131 | ((final_time.tv_sec - start_time.tv_sec) * 1000);
|
|---|
| 132 |
|
|---|
| 133 | usb_log_info("Burst test on %s OUT endpoint completed in %ld ms.", usb_str_transfer_type(pipe->desc.transfer_type), in_duration);
|
|---|
| 134 |
|
|---|
| 135 | free(buffer);
|
|---|
| 136 | if (duration)
|
|---|
| 137 | *duration = in_duration;
|
|---|
| 138 |
|
|---|
| 139 | return rc;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | int usbdiag_burst_test_intr_in(ddf_fun_t *fun, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 143 | {
|
|---|
| 144 | usbdiag_dev_t *dev = ddf_fun_to_usbdiag_dev(fun);
|
|---|
| 145 | if (!dev)
|
|---|
| 146 | return EBADMEM;
|
|---|
| 147 |
|
|---|
| 148 | return burst_in_test(dev->intr_in, cycles, size, duration);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | int usbdiag_burst_test_intr_out(ddf_fun_t *fun, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 152 | {
|
|---|
| 153 | usbdiag_dev_t *dev = ddf_fun_to_usbdiag_dev(fun);
|
|---|
| 154 | if (!dev)
|
|---|
| 155 | return EBADMEM;
|
|---|
| 156 |
|
|---|
| 157 | return burst_out_test(dev->intr_out, cycles, size, duration);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | int usbdiag_burst_test_bulk_in(ddf_fun_t *fun, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 161 | {
|
|---|
| 162 | usbdiag_dev_t *dev = ddf_fun_to_usbdiag_dev(fun);
|
|---|
| 163 | if (!dev)
|
|---|
| 164 | return EBADMEM;
|
|---|
| 165 |
|
|---|
| 166 | return burst_in_test(dev->bulk_in, cycles, size, duration);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | int usbdiag_burst_test_bulk_out(ddf_fun_t *fun, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 170 | {
|
|---|
| 171 | usbdiag_dev_t *dev = ddf_fun_to_usbdiag_dev(fun);
|
|---|
| 172 | if (!dev)
|
|---|
| 173 | return EBADMEM;
|
|---|
| 174 |
|
|---|
| 175 | return burst_out_test(dev->bulk_out, cycles, size, duration);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | int usbdiag_burst_test_isoch_in(ddf_fun_t *fun, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 179 | {
|
|---|
| 180 | usbdiag_dev_t *dev = ddf_fun_to_usbdiag_dev(fun);
|
|---|
| 181 | if (!dev)
|
|---|
| 182 | return EBADMEM;
|
|---|
| 183 |
|
|---|
| 184 | return burst_in_test(dev->isoch_in, cycles, size, duration);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | int usbdiag_burst_test_isoch_out(ddf_fun_t *fun, int cycles, size_t size, usbdiag_dur_t *duration)
|
|---|
| 188 | {
|
|---|
| 189 | usbdiag_dev_t *dev = ddf_fun_to_usbdiag_dev(fun);
|
|---|
| 190 | if (!dev)
|
|---|
| 191 | return EBADMEM;
|
|---|
| 192 |
|
|---|
| 193 | return burst_out_test(dev->isoch_out, cycles, size, duration);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * @}
|
|---|
| 198 | */
|
|---|