| 1 |
/* |
| 2 |
* This file is part of the flashrom project. |
| 3 |
* |
| 4 |
* Copyright 2022 Alexander Goncharov <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; version 2 of the License. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <stdlib.h> |
| 17 |
|
| 18 |
#include "lifecycle.h" |
| 19 |
|
| 20 |
#if CONFIG_CH341A_SPI == 1 |
| 21 |
|
| 22 |
/* Same macro as in ch341a_spi.c programmer. */ |
| 23 |
#define WRITE_EP 0x02 |
| 24 |
#define READ_EP 0x82 |
| 25 |
|
| 26 |
struct ch341a_spi_io_state { |
| 27 |
struct libusb_transfer *transfer_out; |
| 28 |
/* |
| 29 |
* Since the test transfers a data that fits in one CH341 packet, we |
| 30 |
* don't need an array of these transfers (as is done in the driver code). |
| 31 |
*/ |
| 32 |
struct libusb_transfer *transfer_in; |
| 33 |
}; |
| 34 |
|
| 35 |
static struct libusb_transfer *ch341a_libusb_alloc_transfer(void *state, int iso_packets) |
| 36 |
{ |
| 37 |
return calloc(1, sizeof(struct libusb_transfer)); |
| 38 |
} |
| 39 |
|
| 40 |
/* |
| 41 |
* The libusb code stores submitted transfers in their own context. But this |
| 42 |
* function doesn't require a context pointer because libusb stores context |
| 43 |
* pointers in libusb_transfer instances. Since our ch341 driver is using |
| 44 |
* the default context, we store the transfer in our own. |
| 45 |
*/ |
| 46 |
static int ch341a_libusb_submit_transfer(void *state, struct libusb_transfer *transfer) |
| 47 |
{ |
| 48 |
struct ch341a_spi_io_state *io_state = state; |
| 49 |
|
| 50 |
assert_true(transfer->endpoint == WRITE_EP || transfer->endpoint == READ_EP); |
| 51 |
|
| 52 |
if (transfer->endpoint == WRITE_EP) { |
| 53 |
assert_null(io_state->transfer_out); |
| 54 |
io_state->transfer_out = transfer; |
| 55 |
} else if (transfer->endpoint == READ_EP) { |
| 56 |
assert_null(io_state->transfer_in); |
| 57 |
io_state->transfer_in = transfer; |
| 58 |
} |
| 59 |
|
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
static void ch341a_libusb_free_transfer(void *state, struct libusb_transfer *transfer) |
| 64 |
{ |
| 65 |
free(transfer); |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Handle submitted transfers by pretending that a transfer is completed and |
| 70 |
* invoking its callback (that is the flashrom code). |
| 71 |
*/ |
| 72 |
static int ch341a_libusb_handle_events_timeout(void *state, libusb_context *ctx, struct timeval *tv) |
| 73 |
{ |
| 74 |
struct ch341a_spi_io_state *io_state = state; |
| 75 |
|
| 76 |
if (io_state->transfer_out) { |
| 77 |
io_state->transfer_out->status = LIBUSB_TRANSFER_COMPLETED; |
| 78 |
io_state->transfer_out->actual_length = io_state->transfer_out->length; |
| 79 |
io_state->transfer_out->callback(io_state->transfer_out); |
| 80 |
io_state->transfer_out = NULL; |
| 81 |
} |
| 82 |
|
| 83 |
if (io_state->transfer_in) { |
| 84 |
io_state->transfer_in->buffer[1] = reverse_byte(0xEF); /* WINBOND_NEX_ID */ |
| 85 |
io_state->transfer_in->buffer[2] = reverse_byte(0x40); /* WINBOND_NEX_W25Q128_V left byte */ |
| 86 |
io_state->transfer_in->buffer[3] = reverse_byte(0x18); /* WINBOND_NEX_W25Q128_V right byte */ |
| 87 |
|
| 88 |
io_state->transfer_in->status = LIBUSB_TRANSFER_COMPLETED; |
| 89 |
io_state->transfer_in->actual_length = io_state->transfer_in->length; |
| 90 |
io_state->transfer_in->callback(io_state->transfer_in); |
| 91 |
io_state->transfer_in = NULL; |
| 92 |
} |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
void ch341a_spi_basic_lifecycle_test_success(void **state) |
| 98 |
{ |
| 99 |
struct ch341a_spi_io_state ch341a_spi_io_state = { 0 }; |
| 100 |
struct io_mock_fallback_open_state ch341a_spi_fallback_open_state = { |
| 101 |
.noc = 0, |
| 102 |
.paths = { NULL }, |
| 103 |
}; |
| 104 |
const struct io_mock ch341a_spi_io = { |
| 105 |
.state = &ch341a_spi_io_state, |
| 106 |
.libusb_alloc_transfer = &ch341a_libusb_alloc_transfer, |
| 107 |
.libusb_submit_transfer = &ch341a_libusb_submit_transfer, |
| 108 |
.libusb_free_transfer = &ch341a_libusb_free_transfer, |
| 109 |
.libusb_handle_events_timeout = &ch341a_libusb_handle_events_timeout, |
| 110 |
.fallback_open_state = &ch341a_spi_fallback_open_state, |
| 111 |
}; |
| 112 |
|
| 113 |
run_basic_lifecycle(state, &ch341a_spi_io, &programmer_ch341a_spi, ""); |
| 114 |
} |
| 115 |
|
| 116 |
void ch341a_spi_probe_lifecycle_test_success(void **state) |
| 117 |
{ |
| 118 |
struct ch341a_spi_io_state ch341a_spi_io_state = { 0 }; |
| 119 |
struct io_mock_fallback_open_state ch341a_spi_fallback_open_state = { |
| 120 |
.noc = 0, |
| 121 |
.paths = { NULL }, |
| 122 |
}; |
| 123 |
const struct io_mock ch341a_spi_io = { |
| 124 |
.state = &ch341a_spi_io_state, |
| 125 |
.libusb_alloc_transfer = &ch341a_libusb_alloc_transfer, |
| 126 |
.libusb_submit_transfer = &ch341a_libusb_submit_transfer, |
| 127 |
.libusb_free_transfer = &ch341a_libusb_free_transfer, |
| 128 |
.libusb_handle_events_timeout = &ch341a_libusb_handle_events_timeout, |
| 129 |
.fallback_open_state = &ch341a_spi_fallback_open_state, |
| 130 |
}; |
| 131 |
|
| 132 |
const char *expected_matched_names[1] = {"W25Q128.V"}; |
| 133 |
run_probe_v2_lifecycle(state, &ch341a_spi_io, &programmer_ch341a_spi, "", "W25Q128.V", |
| 134 |
expected_matched_names, 1); |
| 135 |
} |
| 136 |
|
| 137 |
#else |
| 138 |
SKIP_TEST(ch341a_spi_basic_lifecycle_test_success) |
| 139 |
SKIP_TEST(ch341a_spi_probe_lifecycle_test_success) |
| 140 |
#endif /* CONFIG_CH341A_SPI */ |